Android 8 0 通知相容

2021-08-21 14:37:10 字數 1789 閱讀 9230

android 8.0 引入了通知渠道,其允許您為要顯示的每種通知型別建立使用者可自定義的渠道。使用者介面將通知渠道稱之為通知類別。targesdk公升級到26之後,所有的通知的實現都需要提供通知渠道,如果不提供通知渠道的話,所有通知在8.0系統上面都不能正常展示,下面來看**。

既然是相容,當然不能少了判斷系統版本了:

if (build.version.sdk_int >= build.version_codes.o) else
針對8.0以上的版本 注意 建立 notificationchannel , 並設定其建立同一 channelid,注意 這個id不能過長, 過長可能被截斷;

private final string notification_channel_name = "service";

private final string notification_description = "讀取位置資訊";

private notificationmanager notificationmanager = null;

private notificationchannel notificationchannel = null;

notificationcompat.builder mbuilder = new notificationcompat.builder(this);

if (build.version.sdk_int >= build.version_codes.o)

string channelid = "channel";

if (notificationchannel == null)

mbuilder.setchannel(channelid);

} mbuilder.setpriority(0);// 設定該通知優先順序

mbuilder.setsmallicon(r.mipmap.logo);

mbuilder.setcontentintent(intent);

notification = mbuilder.build();

startforeground(serviceid.id, notification);

在這裡 我所相容的8.0以上機型, 通過這樣設定channel 還是會彈出 錯誤資訊:「failed to post notification on channel "null"」;

這裡都是通過 v7包下的 notificationcompat 建立,怎麼還會報錯並且報錯 指代的是 notification on channel null????

於是我們這樣更新一下**,針對8.0的 做另一種設定 channel方式:

if (build.version.sdk_int >= build.version_codes.o) 

string channelid = "channel";

if (notificationchannel == null)

// 這裡 針對8.0 我們如下設定 channel

.setsmallicon(r.mipmap.logo)

}else

startforeground(serviceid.id, notification);

因為跟很多帖子描述的問題不同,在此記錄讓大家參考一下。 8.0一下 通過 notificationcompat 設定, 8.0以及以上 通過notification 設定, 然後大家也發現 notification 在 8.0 以後 廢棄了 乙個引數的構造方法, 取代的就是多了乙個 channel 的構造方法。

Android 8 0通知欄推送及適配

首先我們判斷手機版本號,android版本大於8.0的時候呢,我們需要進行一下通道的操作才可 判斷版本號 接好 此處判斷安卓版本號是否大於或者等於android8.0 if build.version.sdk int build.version codes.o elseandroid8.0 適配 t...

Andoid8 0通知機制變化

需要說明一點 notificationchannel不同使用者下是通過uid來區分的,所以存在多使用者時系統在尋找channel時是以pkg uid為key來進行查詢的。在應用啟動時,需要建立兩個通知管道,並對管道設定一些初始狀態,包括管道名稱 鈴聲 振動 led提醒 圓點通知等,如下所示,為新資訊...

安卓8 0通知欄適配

通知渠道的建立 requiresapi build.version codes.o public void createchannelid string channelid 1.使用系統sdk裡面的notification物件建立通知 public void send notification co...