Android O notification 适配

NotificationManager notificationManager = (NotificationManager) getSystemService(NOTIFICATION_SERVICE);
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.O) {
    int notificationId = 0x1234;
    Notification.Builder builder = new Notification.Builder(this, "1");
    //与channelId对应
    // icon title text必须包含,不然影响桌面图标小红点的展示
    builder.setContentTitle("我是标题")
            //设置内容
            .setContentText("我是内容")
            // 设置大图标
            .setLargeIcon(BitmapFactory.decodeResource(getResources(), R.mipmap.ic_launcher))
            // 设置小图标
            .setSmallIcon(R.mipmap.ic_launcher_round)
            // 设置通知时间
            .setWhen(System.currentTimeMillis())
            // 首次进入时显示效果
            .setTicker("我是测试内容")
            // 设置通知方式,声音,震动,呼吸灯等效果,这里通知方式为声音
            .setDefaults(Notification.DEFAULT_SOUND);
    //久按桌面图标时允许的此条通知的数量
    notificationManager.notify(notificationId, builder.build());
    NotificationChannel channel = new NotificationChannel("1", "Channel1", NotificationManager.IMPORTANCE_DEFAULT);
    channel.enableLights(true);
    //是否在桌面icon右上角展示小红点
    channel.setLightColor(Color.GREEN);
    //小红点颜色
    channel.setShowBadge(true);
    //是否在久按桌面图标时显示此渠道的通知
    notificationManager.createNotificationChannel(channel);
} else {
    NotificationCompat.Builder mBuilder = new NotificationCompat.Builder(this);
    //设置标题
    mBuilder.setContentTitle("我是标题")
            //设置内容
            .setContentText("我是内容")
            // 设置大图标
            .setLargeIcon(BitmapFactory.decodeResource(getResources(), R.mipmap.ic_launcher))
            // 设置小图标
            .setSmallIcon(R.mipmap.ic_launcher_round)
            // 设置通知时间
            .setWhen(System.currentTimeMillis())
            // 首次进入时显示效果
            .setTicker("我是测试内容")
            // 设置通知方式,声音,震动,呼吸灯等效果,这里通知方式为声音
            .setDefaults(Notification.DEFAULT_SOUND);
    // 发送通知请求
    notificationManager.notify(10, mBuilder.build());
}

猜你喜欢

转载自blog.csdn.net/Star_cmx/article/details/88874593