美文网首页
Android8 通知适配

Android8 通知适配

作者: 鹅鹅鹅_ | 来源:发表于2019-01-09 14:23 被阅读0次

Android8中引入了通知渠道,大意就是将通知分组管理。
适配起来其实就是创建通知渠道,然后创建通知的时候将通知与通知渠道关联起来。
步骤

  1. 根据应用通知的种类分组,创建不同的channel注册到系统中
  2. 创建具体的通知时将通知与其所属的通知渠道通过channel id关联起来

所以我写了一个通知类工具,用来注册通知channel。在这里不创建Group也可以。

package com.xiaoxiaoqiquan.client.util;

import android.annotation.TargetApi;
import android.app.Notification;
import android.app.NotificationChannel;
import android.app.NotificationChannelGroup;
import android.app.NotificationManager;
import android.content.Context;
import android.graphics.Color;
import android.os.Build;


import com.xiaoxiaoqiquan.client.IApplication;
import java.util.concurrent.atomic.AtomicInteger;


public class NotificationUtil {
    public static String XQW_CHANNEL_ID = "xqw_channel_id";
    public static String XQW_CHANNEL_NAME = "xqw_channel_name";
    public static String XQW_CHANNEL_GROUP_ID = "XQW_GROPU_ID";
    public static String XQW_CHANNEL_GROUP_NAME = "小期旺通知组";
    private static AtomicInteger notityID = new AtomicInteger(1);

    @TargetApi(26)
    public static void channelInit() {
        if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.O) {
            NotificationManager manager = (NotificationManager) IApplication.getContext().getSystemService(Context.NOTIFICATION_SERVICE);
            manager.createNotificationChannelGroup(new NotificationChannelGroup(XQW_CHANNEL_GROUP_ID, XQW_CHANNEL_GROUP_NAME));

            NotificationChannel channel = new NotificationChannel(XQW_CHANNEL_ID, XQW_CHANNEL_NAME, NotificationManager.IMPORTANCE_HIGH);
            channel.setGroup(XQW_CHANNEL_GROUP_ID);
            channel.setShowBadge(true);
            channel.setBypassDnd(true);    //设置绕过免打扰模式
            channel.canBypassDnd();       //检测是否绕过免打扰模式
            channel.setLockscreenVisibility(Notification.VISIBILITY_SECRET);//设置在锁屏界面上显示这条通知
            channel.setDescription("小期旺信息通知");
            channel.setLightColor(Color.GREEN);
            channel.setName("小期旺通知");
            channel.setShowBadge(true);
            channel.setVibrationPattern(new long[]{100, 200, 300, 400, 500, 400, 300, 200, 400});
            channel.enableVibration(true);

            manager.createNotificationChannel(channel);
        }
    }

    public static int nextNotifyId() {
        return notityID.getAndIncrement();
    }
}

在MainActiviy或者Application中调用注册通知渠道

NotificationUtil.channelInit();

之后发送通知就可以使用channel id绑定渠道了

NotificationCompat.Builder builder = new NotificationCompat.Builder(this, NotificationUtil.XQW_CHANNEL_ID);
效果图

相关文章

  • Android 角标适配

    Android 角标适配 小米的角标消息需要通过通知来实现,并且需要设置通知级别适配android8 参考文章:h...

  • Android8 通知适配

    Android8中引入了通知渠道,大意就是将通知分组管理。适配起来其实就是创建通知渠道,然后创建通知的时候将通知与...

  • CircleCI构建Android SDK升级

    最近在适配Android8,把targetSdkVersion升级到27以支持android8.1了 适配完后的一...

  • android 8.0必要适配

    android 8.0适配 配置修改 必要的适配项 通知栏 通知栏在target升级到26之后,如果不适配按照以前...

  • Android8 后台定时提醒通知

    众所周知,谷歌A8开始对安卓后台任务进行了严格限制,什么隐式系统广播,后台常驻Service,AlarmManag...

  • 通知栏适配

    android8.0通知栏适配要求必须设置通道,即 并且构建通知对象时,必须指定channelId:

  • Android8 避免startForeground方法弹出通知

    在A8中谷歌对后台service进行了严格限制,不允许默默无闻的后台service存在,若想用service,必须...

  • Android 8.0 Notification

    Android 8.0 通知适配: Android Api 26 Notification Builder 构建...

  • 安卓推送弹出通知栏适配

    适配安卓8.0以上推送通知栏1.推送弹出通知栏NotificationManager manager = (Not...

  • 新特性与行为变更 -- 代码3

    通知渠道 Android 8.0+ 通知栏适配 通知侦听器 前台服务 强制执行 FLAG_ACTIVITY_NEW...

网友评论

      本文标题:Android8 通知适配

      本文链接:https://www.haomeiwen.com/subject/mtxgrqtx.html