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);
}