NotificationChannel


 
通知渠道的引入可以很方便的管理,和歸納同一種類型的Notification。以前多個通知 ,無論是不是同一個應用的通知,逐個排列下來,占滿了屏幕,不太友好。
1. 相同通知渠道的通知已經被合並,而不是一一全部展開。 2. 如果你的項目TargetSDK26以上,那么你在使用Notification的時候必須指定一個ChannelId,否則當然會報錯 3. 下面是Android 8.0上通知渠道NotificationChannel 的使用代碼段,注意點需要傳入CHANNEL_ID(隨意指定),CHANNEL_NAME(隨意指定)
    public static NotificationManager notificationManager;
    public static String CHANNEL_1 = "channel1";

    public static void init(Context context){
        notificationManager = (NotificationManager) context.getSystemService(Context.NOTIFICATION_SERVICE);
        if (android.os.Build.VERSION.SDK_INT >= android.os.Build.VERSION_CODES.O) {
       ////配置通知渠道id,渠道名稱(用戶可以看到),渠道優先級 NotificationChannel channel1
= new NotificationChannel(CHANNEL_1,"通知渠道1", NotificationManager.IMPORTANCE_HIGH); channel1.setDescription("通知渠道的描述1"); ///配置通知出現時的閃燈(如果 android 設備支持的話)
       channel1.enableLights(
true); channel1.setLightColor(Color.WHITE);
       
//配置通知出現時的震動(如果 android 設備支持的話)
channel1.enableVibration(true);
channel1.setVibrationPattern(new long[]{100, 200, 100, 200});
//channel.setBypassDnd(true); //設置繞過免打擾模式
//channel.canBypassDnd(); //檢測是否繞過免打擾模式
//channel.setLockscreenVisibility(Notification.VISIBILITY_SECRET);//設置在鎖屏界面上顯示這條通知

          notificationManager.createNotificationChannel(channel1);

        }

 }

可以通過notificationManager.deleteNotificationChannel(String channelId)去刪除指定channelId的渠道。

更多設置可百度相關需求


免責聲明!

本站轉載的文章為個人學習借鑒使用,本站對版權不負任何法律責任。如果侵犯了您的隱私權益,請聯系本站郵箱yoyou2525@163.com刪除。



 
粵ICP備18138465號   © 2018-2025 CODEPRJ.COM