Notification.Builder() Android 8.0报过时错误

Notification

通知(Notification)是Android系统中比较有特色的一个功能,当某个应用程序希望用户发出一些提示信息,而该应用又不在前台运行时,就可以借助通知来实现。

Notification8.0以前的基本用法

在Android8.0以前你可能创建的一个Notification是这样的:

/*
首先需要一个NotificationManager来对通知进行管理
调用Context的getSystemService()方法获取到。
getSystemService()方法接受的一个字符串参数用于确定系统的的哪一个服务。
 */
NotificationManager notificationManager =
        (NotificationManager) getSystemService(Context.NOTIFICATION_SERVICE);
/*
使用Builder构造器来创建Notification对象
*/
Notification notification = new NotificationCompat.Builder(MainActivity.this,id)
        //指定通知的标题内容
        .setContentTitle("This is content title")
        //设置通知的内容
        .setContentText("This is content text")
        //指定通知被创建的时间
        .setWhen(System.currentTimeMillis())
        //设置通知的小图标
        .setSmallIcon(R.drawable.ic_launcher_foreground)
        //设置通知的大图标
        .setLargeIcon(BitmapFactory.decodeResource(getResources(),
                R.drawable.ic_launcher_background))
        //添加点击跳转通知跳转
        .setContentIntent(pendingIntent)
        //实现点击跳转后关闭通知
        .setAutoCancel(true)
        .build();

/*
调用NotificationManager的notify()方法将通知显示出来
传入的第一个参数是通知的id
传入的第二个参数是notification对象
 */
notificationManager.notify(1,notification);

Notification8.0以前的方法在在8.0中实现时出现的状况

出现这样的原因是Budlier()方法中添加一个参数:

第一个参数是上下文对象

第二个参数是通知渠道的代码

public Builder(@NonNull Context context, @NonNull String channelId)

你可能会不定义它使用default去实现它。像这样:

你会发现它没有报错了,你以为它可以实现了。但是当你去运行它的时候,它会会给你个这样的提示:

Notification8.0中的基本用法

Android O 引入了 通知渠道(Notification Channels),以提供统一的系统来帮助用户管理通知,如果是针对 android O 为目标平台时,必须实现一个或者多个通知渠道,以向用户显示通知。

因此你必须去实现一个通知渠道:

/*
首先需要一个NotificationManager来对通知进行管理
调用Context的getSystemService()方法获取到。
getSystemService()方法接受的一个字符串参数用于确定系统的的哪一个服务。
 */
NotificationManager notificationManager =
        (NotificationManager) getSystemService(Context.NOTIFICATION_SERVICE);
/*
调用NotificationChannel创建通知渠道实例
并为它设置属性
 */
//通知渠道的ID
String id = "channel_01";
//用户可以看到的通知渠道的名字
CharSequence name = getString(R.string.channel_name);
//用户可看到的通知描述
String description = getString(R.string.channel_description);
//构建NotificationChannel实例
NotificationChannel notificationChannel =
        new NotificationChannel(id,name,NotificationManager.IMPORTANCE_HIGH);
//配置通知渠道的属性
notificationChannel.setDescription(description);
//设置通知出现时的闪光灯
notificationChannel.enableLights(true);
notificationChannel.setLightColor(Color.RED);
//设置通知出现时的震动
notificationChannel.enableVibration(true);
notificationChannel.setVibrationPattern(new long[]{100,200,300,400,500,400,300,200,100});
//在notificationManager中创建通知渠道
notificationManager.createNotificationChannel(notificationChannel);
Notification notification = new NotificationCompat.Builder(MainActivity.this,id)
        //指定通知的标题内容
        .setContentTitle("This is content title")
        //设置通知的内容
        .setContentText("This is content text")
        //指定通知被创建的时间
        .setWhen(System.currentTimeMillis())
        //设置通知的小图标
        .setSmallIcon(R.drawable.ic_launcher_foreground)
        //设置通知的大图标
        .setLargeIcon(BitmapFactory.decodeResource(getResources(),
                R.drawable.ic_launcher_background))
        //添加点击跳转通知跳转
        .setContentIntent(pendingIntent)
        //实现点击跳转后关闭通知
        .setAutoCancel(true)
        .build();

/*
调用NotificationManager的notify()方法将通知显示出来
传入的第一个参数是通知的id
传入的第二个参数是notification对象
 */
notificationManager.notify(1,notification);

并且将它的Id传入到Notification中:

你才能看到你的通知:

猜你喜欢

转载自blog.csdn.net/qq_36607515/article/details/81393794