Android Bad notification for startForeground: java.lang.RuntimeException: invalid channel for servic
2650 ワード
試験機:小米8 SE,MIUI 11.0.2
多くの資料を調べたところ、Android 8であることが分かった.0サービス通知の変更
8.0以降はstartserviceを直接呼び出すことができません.
通知を開くとき
mainfestに追加
参照先:https://www.jianshu.com/p/7691957536a2 https://blog.csdn.net/qq_15527709/article/details/78853048
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 defaults=0x0 flags=0x62 color=0x00000000 vis=PRIVATE)
多くの資料を調べたところ、Android 8であることが分かった.0サービス通知の変更
8.0以降はstartserviceを直接呼び出すことができません.
Intent intet1 = new Intent(MainActivity.this, LocationUtils.class);
intet1.setAction("android.intent.action.RESPOND_VIA_MESSAGE");
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.O) {
//android8.0 startForegroundService service
startForegroundService(intet1);
} else {
startService(intet1);
}
通知を開くとき
Intent nfIntent = new Intent(this, MainActivity.class);
Notification.Builder builder = new Notification.Builder(this.getApplicationContext())
.setContentIntent(PendingIntent.getActivity(this, 0, nfIntent, 0)) // PendingIntent
.setSmallIcon(R.mipmap.ic_launcher) //
.setContentTitle(getResources().getString(R.string.app_name))
.setContentText(" ...") //
.setWhen(System.currentTimeMillis()); //
String CHANNEL_ONE_ID = "com.primedu.cn";
String CHANNEL_ONE_NAME = "Channel One";
if (android.os.Build.VERSION.SDK_INT >= android.os.Build.VERSION_CODES.O) {
// 8.1
NotificationChannel notificationChannel = new NotificationChannel(CHANNEL_ONE_ID, CHANNEL_ONE_NAME, NotificationManager.IMPORTANCE_MIN);
notificationChannel.enableLights(false);// ,
notificationChannel.setShowBadge(false);//
notificationChannel.setLockscreenVisibility(Notification.VISIBILITY_SECRET);
NotificationManager manager = (NotificationManager) getSystemService(NOTIFICATION_SERVICE);
manager.createNotificationChannel(notificationChannel);
builder.setChannelId(CHANNEL_ONE_ID);
}
Notification notification = builder.build(); // Notification
notification.defaults = Notification.DEFAULT_SOUND; //
startForeground(1, notification);
mainfestに追加
参照先:https://www.jianshu.com/p/7691957536a2 https://blog.csdn.net/qq_15527709/article/details/78853048