我有这样的碰撞中产生,我无法重现,而且不知道为什么它的发生。
android.app.RemoteServiceException: (Bad notification for startForeground: java.lang.RuntimeException: invalid channel for service notification: Notification(channel=null pri=0 contentView=null vibrate=null sound=null tick defaults=0x0 flags=0x4a color=0x00000000 vis=PRIVATE))
at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1771)
at android.os.Handler.dispatchMessage(Handler.java:106)
at android.os.Looper.loop(Looper.java:164)
at android.app.ActivityThread.main(ActivityThread.java:6518)
at java.lang.reflect.Method.invoke(Native Method)
at com.android.internal.os.RuntimeInit$MethodAndArgsCaller.run(RuntimeInit.java:438)
at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:807)
这听起来好像是没有建立渠道,但我每次创建通道之前创建的通知。
我什至不知道发生了什么事在这部分代码,但是这就是我开始一个前台服务的唯一地方,所以我认为我是在正确的轨道上
下面的代码
String channelId = NotificationChannelConstants.MAIN_NOTIFICATION_CHANNEL_ID;
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.O) {
String channelName = NotificationChannelConstants.MAIN_NOTIFICATION_CHANNEL_NAME;
NotificationChannel chan = new NotificationChannel(channelId,
channelName, NotificationManager.IMPORTANCE_LOW);
chan.setLockscreenVisibility(Notification.VISIBILITY_PRIVATE);
NotificationManager service = (NotificationManager) context.getSystemService(Context.NOTIFICATION_SERVICE);
service.createNotificationChannel(chan);
}
notificationBuilder = new NotificationCompat.Builder(this, channelId)
.setWhen(System.currentTimeMillis())
.setSmallIcon(R.drawable.sb_unknown)
.setContentTitle(getString(R.string.app_name))
.setTicker(getString(R.string.app_name)).setContentIntent(broadcastIntent)
.setContentText(mNotificationText)
.setAutoCancel(false)
.setOngoing(true)
.setOnlyAlertOnce(true);
startForeground(R.id.notification, notificationBuilder.build());
您可以使用此版本为奥利奥和更高
如果你使用的服务类,你可以把这个代码
@Override
public void onCreate(){
super.onCreate();
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.O)
startMyOwnForeground();
else
startForeground(1, new Notification());
}
private void startMyOwnForeground(){
String NOTIFICATION_CHANNEL_ID = "com.example.simpleapp";
String channelName = "My Background 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(this, NOTIFICATION_CHANNEL_ID);
Notification notification = notificationBuilder.setOngoing(true)
.setSmallIcon(R.drawable.icon_1)
.setContentTitle("App is running in background")
.setPriority(NotificationManager.IMPORTANCE_MIN)
.setCategory(Notification.CATEGORY_SERVICE)
.build();
startForeground(2, notification);
}
当然,不要忘记填写ForeGroundService权限的AndroidManifest.xml
<uses-permission android:name="android.permission.FOREGROUND_SERVICE" />