gpt4 book ai didi

java - startForeground 的错误通知

转载 作者:搜寻专家 更新时间:2023-11-01 08:19:08 29 4
gpt4 key购买 nike

我创建了一个服务。我启动了这个服务前台,但是当我午餐我的应用程序时,一个应用程序崩溃了。我添加了 startServiceOreoCondition 我读到了这个但是它对我没有帮助

@Override
public void onCreate() {

mdb = ManagerDB.getInstance(getApplicationContext(), "name");
context = getApplicationContext();

if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.O) {
NotificationManager nm = (NotificationManager) getSystemService(NOTIFICATION_SERVICE);
nm.createNotificationChannel(new NotificationChannel(NOTIFICATION_CHANNEL_ID_SERVICE, "*****", NotificationManager.IMPORTANCE_DEFAULT));
nm.createNotificationChannel(new NotificationChannel(NOTIFICATION_CHANNEL_ID_INFO, "********", NotificationManager.IMPORTANCE_DEFAULT));
}

startServiceOreoCondition();
sp = getSharedPreferences("pfref", Activity.MODE_PRIVATE);
editor = sp.edit();
executeSomething();
timer = new Timer();
}




private void startServiceOreoCondition() {
if (Build.VERSION.SDK_INT >= 26) {

String NOTIFICATION_CHANNEL_ID = "pl.***.***";
String channelName = "Communication Service";
NotificationChannel chan = new NotificationChannel(NOTIFICATION_CHANNEL_ID, channelName, NotificationManager.IMPORTANCE_NONE);
chan.setLightColor(Color.BLUE);
chan.setLockscreenVisibility(Notification.VISIBILITY_PRIVATE);

NotificationManager manager = (NotificationManager) getSystemService(Context.NOTIFICATION_SERVICE);
assert manager != null;
manager.createNotificationChannel(chan);

NotificationCompat.Builder notificationBuilder = new NotificationCompat.Builder(context);
Notification notification = notificationBuilder.setOngoing(true)
.setContentTitle(context.getString(R.string.app_name))
.setPriority(NotificationManager.IMPORTANCE_MIN)
.setCategory(Notification.CATEGORY_SERVICE)
.build();
startForeground(4, notification);
}else{
startForeground(3, new Notification());
}
}

但是当我在 andorid 8.1 上启动这个应用程序时,我看到了这个错误:

android.app.RemoteServiceException: Bad notification for startForeground: java.lang.RuntimeException: invalid channel for service notification: Notification(channel=null pri=1 contentView=null vibrate=null sound=null defaults=0x0 flags=0x42 color=0x00000000 category=service vis=PRIVATE)

而且我还添加了这个权限:

<uses-permission android:name="android.permission.FOREGROUND_SERVICE" />

最佳答案

NotificationChannel chan = new NotificationChannel(NOTIFICATION_CHANNEL_ID, channelName, NotificationManager.IMPORTANCE_NONE);
chan.setDescription(<Add channel Description>);

您刚刚创建了通知 channel 对象,但是您需要使用 channel 对象创建 channel 。

    nm.createNotificationChannel(new NotificationChannel(NOTIFICATION_CHANNEL_ID, "********", NotificationManager.IMPORTANCE_DEFAULT));

并且在创建 Notification Builder 时传递创建的 channel ID。

    NotificationCompat.Builder notificationBuilder = new 
NotificationCompat.Builder(context,NOTIFICATION_CHANNEL_ID);
Notification notification = notificationBuilder.setOngoing(true)
.setContentTitle(context.getString(R.string.app_name))
.setPriority(NotificationManager.IMPORTANCE_MIN)
.setCategory(Notification.CATEGORY_SERVICE)
.build();

关于java - startForeground 的错误通知,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/53815261/

29 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com