android 通知写法_android.app.RemoteServiceException: Bad notification for startForeground

错误提示:

android.app.RemoteServiceException: Bad notification for startForeground: java.lang.RuntimeException: invalid channel for service notification: Notification(channel=null

8.0后要加上channel来区分通知,故需要改为如下的写法:

/**

* 设置服务在前台可见

*/

private void startForeground(){

/**

* 通知栏点击跳转的intent

*/

PendingIntent pendingIntent = PendingIntent.getActivity(this,0, new Intent(this, WelcomeActivity.class), PendingIntent.FLAG_CANCEL_CURRENT);

/**

* 创建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();

}

/**

* 设置notification在前台展示

*/

startForeground(NOTIFICATION_ID, notification);

}

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值