Notification注意要点
1、在匿名类中使用,出现错误
Notification notification = new NotificationCompat.Builder(this,"default")
错误提示如下:
Builder(android.content.Context,String)in Builder cannot be appliedto
(anonymous android.view.View.OnClickListener,String)
单词:anonymous n.匿名
2、已弃用 : NotificationCompat.Builder(this)更改如下 :
NotificationCompat.Builder(Context context, String channeIId)
即新增一个String参数即可,因此,简单notification可以直接加一个String参数:
NotificationManager manager
= (NotificationManager)getSystemService(NOTIFICATION_SERVICE);
Notification notification = new NotificationCompat.Builder(this,"default")
.setContentTitle("测试notification")
.setContentText("This is content text")
.setWhen(System.currentTimeMillis())
.setSmallIcon(R.mipmap.ic_launcher_round)
.build();
manager.notify(1,notification);
channelId的实际作用是将notification进行分类,如设置不同优先级等。也可使用NotificationChannel.getId()设置channelId