然后在Pixel XL和Nexus 6P上运行了项目,提示出:
然后看了官方文档:notification channel
以及stackoverflow:stackoverflow
这是我之前的代码:
Notification.Builder builder = new Notification.Builder(getApplicationContext());
builder.setContentTitle("")
.setContentText("正在运行中")
.setLargeIcon(BitmapFactory.decodeResource(getResources(), R.mipmap.so_logo))
.setSmallIcon(R.mipmap.so_logo)
.setOngoing(true)//设置是否是一个正在执行的通知
.setContentIntent(PendingIntent.getActivity(this, 0, intent, PendingIntent.FLAG_CANCEL_CURRENT));
// 获取构建好的Notification
Notification notification = builder.build();
//设置为前台服务,必须绑定一个notification对象,实际上也就是说如果你想做持久化的Service就得让用户知道,PID是自定义的整数表明notification的ID
startForeground(Notification_ID, notification);
然后加入了:
NotificationChannel channel = new NotificationChannel(String.valueOf(Notification_ID), MsgService.class.getName(), NotificationManager.IMPORTANCE_LOW);
NotificationManager manager = (NotificationManager) getSystemService(Context.NOTIFICATION_SERVICE);
manager.createNotificationChannel(channel);
和
.setChannelId(String.valueOf(Notification_ID))//该句适配android 8.0 版本
NotificationChannel channel = new NotificationChannel(String.valueOf(Notification_ID), MsgService.class.getName(), NotificationManager.IMPORTANCE_LOW);
NotificationManager manager = (NotificationManager) getSystemService(Context.NOTIFICATION_SERVICE);
manager.createNotificationChannel(channel);
Notification.Builder builder = new Notification.Builder(getApplicationContext());
builder.setContentTitle("")
.setContentText("正在运行中")
.setChannelId(String.valueOf(Notification_ID))//该句适配android 8.0 版本
.setLargeIcon(BitmapFactory.decodeResource(getResources(), R.mipmap.so_logo))
.setSmallIcon(R.mipmap.so_logo)
.setOngoing(true)//设置是否是一个正在执行的通知
.setContentIntent(PendingIntent.getActivity(this, 0, intent, PendingIntent.FLAG_CANCEL_CURRENT));
// 获取构建好的Notification
Notification notification = builder.build();
//设置为前台服务,必须绑定一个notification对象,实际上也就是说如果你想做持久化的Service就得让用户知道,PID是自定义的整数表明notification的ID
startForeground(Notification_ID, notification);