Android O(8.0)基本通知适配

        Android在8.0引入了一个叫NotificationChannel(通知渠道)的概念,使得同一个渠道的通知可以进行统一管理,用户可以通过屏蔽某个通知渠道来达到屏蔽该渠道下所有的通知。同一个通知渠道的通知都被折叠在通知下拉框中,极大节省了的空间。之前项目中遇到需要发简单通知的需求,在此做一个简单的8.0版本的通知适配。工具类使用单例来编写,在最后会给出所有代码,可以根据自身需求进行完善。

NotificationChannel channel = new NotificationChannel("channel_id"," channel_name", NotificationManager.IMPORTANCE_DEFAULT);  

传入的参数前两个显而易见,第三个是通知的优先级,8.0之前的通知优先级的设置是在Notification.Builder中的setPriority()方法来设置。
NotificationChannel还有很多其他API可以来调用,如下代码:

        channel.setDescription("通道描述");
        //可否绕过系统的 请勿打扰模式
        channel.canBypassDnd();
        channel.setBypassDnd(true);
        //锁屏是否显示通知(系统应用可用,基本用不到)
        channel.setLockscreenVisibility(VISIBILITY_SECRET);
        //通知时是否有闪光灯(需要手机硬件支持)
        channel.enableLights(true);
        channel.shouldShowLights();
        //设置闪光时的颜色
        channel.setLightColor(Color.BLUE);
        //是否显示桌面图标角标
        channel.canShowBadge();
        channel.setShowBadge(true);
        //是否允许震动
        channel.enableVibration(true);
        //获取系统通知响铃的配置参数(一般用系统默认的即可)
        channel.getAudioAttributes();
        //channel.setSound(); 设置通知铃声
        //设置震动频率(100ms,100ms,100ms 三次震动)
        channel.setVibrationPattern(new long[]{500, 500, 500});

最重要的一步就是给通知设置渠道,这一步必不可少,不然8.0以上机型不生效

     //给8.0以上的通知设定 渠道
    getNotificationManager().createNotificationChannel(channel);  

Notification.Build没有什么好叙述的了,直接贴代码

    private void setBuilder() {
    builder = new Notification.Builder(this)
            .setSmallIcon(smallIcon)
            .setContentTitle(title)
            .setContentText(content)
            .setAutoCancel(true);
    if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.O) {
        builder.setChannelId(channelId);
    }
}  

大体上8.0的适配就是这样,给通知增加一个通知渠道即可。如下贴出所有代码

public class NotificationUtils extends ContextWrapper {

private String title;
private String content;
private int smallIcon;
private String channelName;
private String channelId;
private Notification.Builder builder;
private NotificationManager notificationManager;

private NotificationManager getNotificationManager() {
    if (notificationManager == null) {
        return notificationManager = (NotificationManager) getSystemService(NOTIFICATION_SERVICE);
    } else {
        return notificationManager;
    }
}

/**
 * @param context
 * @param channelName 通道名
 * @param channelId   通道ID
 * @param title       通知标题
 * @param content     通知内容
 * @param smallIcon   通知栏显示的图标
 */
public NotificationUtils(Context context, String channelName, String channelId, String title, String content, int smallIcon) {
    super(context);
    this.channelName = channelName;
    this.channelId = channelId;
    this.title = title;
    this.content = content;
    this.smallIcon = smallIcon;
    setBuilder();
    setChannel();
}

/**
 * 构造Notification.Builder
 */
private void setBuilder() {
    builder = new Notification.Builder(this)
            .setSmallIcon(smallIcon)
            .setContentTitle(title)
            .setContentText(content)
            .setAutoCancel(true);
    if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.O) {
        builder.setChannelId(channelId);
    }
}

/**
 * 8.0 以上给通知设置通道
 */
private void setChannel() {
    if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.O) {
        NotificationChannel channel = new NotificationChannel(channelId, channelName, NotificationManager.IMPORTANCE_DEFAULT);
        channel.setDescription("通道描述");
        //可否绕过系统的 请勿打扰模式
        channel.canBypassDnd();
        channel.setBypassDnd(true);
        //锁屏是否显示通知(系统应用可用,基本用不到)
        channel.setLockscreenVisibility(VISIBILITY_SECRET);
        //通知时是否有闪光灯(需要手机硬件支持)
        channel.enableLights(true);
        channel.shouldShowLights();
        //设置闪光时的颜色
        channel.setLightColor(Color.BLUE);
        //是否显示桌面图标角标
        channel.canShowBadge();
        channel.setShowBadge(true);
        //是否允许震动
        channel.enableVibration(true);
        //获取系统通知响铃的配置参数(一般用系统默认的即可)
        channel.getAudioAttributes();
        //channel.setSound(); 设置通知铃声
        //设置震动频率(100ms,100ms,100ms 三次震动)
        channel.setVibrationPattern(new long[]{500, 500, 500});

        //给8.0以上的通知设定 渠道
        getNotificationManager().createNotificationChannel(channel);
    }
}

/**
 * 显示通知
 *
 * @param notifyId
 */
public void notify(int notifyId) {
    getNotificationManager().notify(notifyId, builder.build());
}

} 

使用如下

public class MainActivity extends AppCompatActivity {

private NotificationUtils notificationUtils;

@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_main);
    notificationUtils = new NotificationUtils
            (this, "渠道名称", "渠道ID", "标题", "内容", R.mipmap.ic_launcher);
    notificationUtils.notify(1);
}
}

如果你需要意图,可以重写notify方法,把PendingIntent传入给build设置一下即可。

你可能感兴趣的:(Android O(8.0)基本通知适配)