Error prompt:
android.app.RemoteServiceException: Bad notification for startForeground: java.lang.RuntimeException: invalid channel for service notification: Notification(channel=null
After 8.0, you need to add channel to distinguish notifications, so it needs to be written as follows:
/**
* Set the service to be visible in the foreground
*/
private void startForeground(){
/**
* Notification bar click to jump the intent
*/
PendingIntent pendingIntent = PendingIntent.getActivity(this,0, new Intent(this, WelcomeActivity.class), PendingIntent.FLAG_CANCEL_CURRENT);
/**
* Create Notification
*/
NotificationChannel notificationChannel;
Notification notification;
if(Build.VERSION.SDK_INT>=Build.VERSION_CODES.O){
notificationChannel= new NotificationChannel(CHANNEL_ID,
CHANNEL_NAME, NotificationManager.IMPORTANCE_HIGH);
notificationChannel.enableLights(true);
notificationChannel.setLightColor(Color.RED);
notificationChannel.setShowBadge(true);
notificationChannel.setLockscreenVisibility(Notification.VISIBILITY_PUBLIC);
NotificationManager manager= (NotificationManager) getSystemService(NOTIFICATION_SERVICE);
manager.createNotificationChannel(notificationChannel);
notification = new NotificationCompat.Builder(this, CHANNEL_ID)
.setContentTitle(mMusicModel.getName())
.setContentText(mMusicModel.getAuthor())
.setSmallIcon(R.mipmap.logo)
.setContentIntent(pendingIntent)
.build();
}else{
notification = new Notification.Builder(this)
.setContentTitle(mMusicModel.getName())
.setContentText(mMusicModel.getAuthor())
.setSmallIcon(R.mipmap.logo)
.setContentIntent(pendingIntent)
.build();
}
/**
* Set notification to be displayed in the foreground
*/
startForeground(NOTIFICATION_ID, notification);
}
Similar Posts:
- Android studio reports an error using startservice: IllegalStateException
- How to Solve Android Error: java.lang.SecurityException: Permission Denial
- java.lang.IllegalArgumentException: Service Intent must be explicit: Intent { act=com.ba.cal
- Service Intent must be explicit solution: continued
- [How to Solve] Eclipse: Errors occurred during the build
- [Solved] The activity must be exported or contain an intent-filter
- Object not locked by thread before notify() in onPostExecute
- [azure environment] notification hub failed to create policy: 500 internal server error occurred
- How to Solve error: com.android.ide.common.process.ProcessException: Failed to execute aapt
- Android 7.0 Photo Problem: file:///storage/emulated/0/photo.jpeg exposed beyond app through ClipData.Item.getUri