Android 消息通知欄用法詳解(二) 適配8.0

8.0 以下的通知欄講解,請參考:
Android 消息通知欄用法詳解(一)

上篇中,我們學習了8.0 以下的 通知欄常用用法。但8.0之後,google 又引入了 channel 的概念來管理通知欄,避免一拿起手機一大堆通知佔着屏幕,誰看誰難受。

什麼是channel 呢,意思就是每條通知都對應一個channel,每個 app 都能創建,但這些管理管理權限又都在用戶手上。現在讓我們一起來學習一下,如何適配 8.0 吧。

一、 適配8.0

如果要適配 Android 8.0 ,還需要添加 NitificationChannel,根據文檔,我們可以這樣寫:


String CHANNEL_ID = "chat";
    //適配8.0
    if (Build.VERSION.SDK_INT >=  Build.VERSION_CODES.O){
        NotificationChannel channel = new NotificationChannel(CHANNEL_ID,
                "聊天信息",
                NotificationManager.IMPORTANCE_DEFAULT);
        NotificationManager manager = getSystemService(NotificationManager.class);
        manager.createNotificationChannel(channel);
    }

NotificationChannel 有三個參數是必填的。

  • id : channel_id,這個是channel 的唯一標識
  • name : 在 appinfo 中給用戶看的
  • importance : 重要程度,這裏選擇默認的即可

這段代碼寫在哪裏都行,只有在 mannager.notiify() 之前即可,以後每次創建代碼系統都會去檢測該通知的通道是否存在了,因此不會重複創建。

同樣,因爲上面的屬性,當我們設置完channel之後,下次如果想修改一些屬性,比如震動、聲音,就需要先刪掉這個 channel_id,重新創建纔會生效。不然 系統會把它重新撈起來,你設置的屬性並沒有成功。你可以直接刪掉或者改 channel_id 也可以的。

接着說說 importance ,它的常見4個優先級如下:

  • MPORTANCE_HIGH :緊急,發出提示音,並以浮動通知的形式顯示,讓用戶立即看到
  • IMPORTANCE_DEFAUL:高,發出提示音
  • IMPORTANCE_LOW:中,不發出提示音
  • IMPORTANCE_MIN:最小,不發出提示音,且不會在狀態欄顯示

這樣,最終我們的代碼修改爲

  mNotificationManager = (NotificationManager) getSystemService(Context.NOTIFICATION_SERVICE);
  String CHANNEL_ID = "chat";
  //適配8.0
  if (Build.VERSION.SDK_INT >=  Build.VERSION_CODES.O){
      NotificationChannel channel = new NotificationChannel(CHANNEL_ID,
              "聊天信息",
              NotificationManager.IMPORTANCE_DEFAULT);
      NotificationManager manager = getSystemService(NotificationManager.class);
      manager.createNotificationChannel(channel);
  }

  /**
   * 注意寫上 channel_id,適配8.0,不用擔心8.0以下的,找不到 channel_id 不影響程序
   */
  mBuilder = new NotificationCompat.Builder(this,CHANNEL_ID)
          .setContentTitle("這是標題")
          .setContentText("我是內容,我是demo")
          .setWhen(System.currentTimeMillis())
          .setSmallIcon(R.mipmap.ic_launcher)
          .setLargeIcon(BitmapFactory.decodeResource(getResources(),R.mipmap.ic_launcher));
  //通過 builder.build() 拿到 notification
  mNotificationManager.notify(1, mBuilder.build());

也可以通過

 channel.setDescription("需要開啓此權限才能與他人聊天");

設置 channel 的描述信息。

可以看到,在 8.0 的手機上顯示出來了。如何知道,channel 的信息呢,長按app 的圖標,進入 appInfo,找到 notification 就可以看到了:
在這裏插入圖片描述

二、設置渠道

上面說到,渠道一旦設置了,用戶有權去改動它,比如關閉聲音通知,那我們怎麼知道呢?

如果想了解用戶對您的通知渠道所應用的設置,請按以下步驟操作:

  1. 通過調用 getNotificationChannel() 或 getNotificationChannels() 獲取 NotificationChannel 對象。
  2. 查詢特定的渠道設置,例如 getVibrationPattern()、getSound() 和 getImportance()。

比如,我們把剛纔聊天的通知,長按關閉。
在這裏插入圖片描述
接着,我們使用 getImportance() 拿到 它的importance。

 NotificationChannel channel = mNotificationManager.getNotificationChannel(CHANNEL_ID);
 int importance = channel.getImportance();

當檢測到 用戶關閉時,可以通過 ACTION_CHANNEL_NOTIFICATION_SETTINGS 操作的 Intent 打開通知渠道的系統設置。如:

 //如果被關閉了,
 if (importance == NotificationManager.IMPORTANCE_NONE) {
     Intent intent = new Intent(Settings.ACTION_CHANNEL_NOTIFICATION_SETTINGS);
     intent.putExtra(Settings.EXTRA_APP_PACKAGE, getPackageName());
     intent.putExtra(Settings.EXTRA_CHANNEL_ID, channel.getId());
     startActivity(intent);
 }

如:
在這裏插入圖片描述

刪除渠道

可以通過調用 deleteNotificationChannel() 刪除通知渠道。
注意:通知設置屏幕會顯示已刪除渠道的數量,以此作爲一項垃圾內容防範機制。

發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章