NotificationCompat.Builder()过时,失效

NotificationCompat.Builder()过时,失效_第1张图片

原因是升级到Android O 版本后,该方法被以下方法取代:

NotificationCompat.Builder(Context context, String channelId)

即新增一个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,欲获取 NotificationChannel的详细知识请参考以下链接:
关于NotificationChannel的详细知识

你可能感兴趣的:(问题汇总)