Android O 適配 Notification Channel
code小生,一個 Android 領域技術分享平臺
作者:許渺
連結:https://www.jianshu.com/p/b529e61d220a
宣告:本文是 許渺 原創,轉發等請聯絡原作者授權。
背景
從Android 8.0(API 26)開始,所有的 Notification 都要指定 Channel(通道),對於每一個 Channel 你都可以單獨去設定它;比如通知開關、提示音、是否震動或者是重要程度等;這樣每個應用程式的通知在使用者面前都是透明的。
下面我們來看一下通知的設定頁面和 Channel 的設定介面
Notification Setting
這邊通知設定介面中的類別指的就是 Channel,你必須要建立一個或者多個Channel;這邊需要注意的是如果你的 tartgetSdkVersion>=26,如果你釋出通知不指定 Channel 的話,通知是不會顯示的(系統會自動記錄錯誤)。
Channel Setting
建立 Notification Channel
建立 NotificationChannel 物件,指定 Channel 的 id、name 和通知的重要程度。
setDescription 可以指定設定中 Channel 的描述,如上圖中的(this is default channel!)
使用NotificationMannager的createNotificationChannel方法來新增Channel。
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.O)
{
mNotificationChannel = new NotificationChannel(CHANNEL_ID, CHANNEL_NAME, NotificationManager.IMPORTANCE_DEFAULT);
mNotificationChannel.setDescription(CHANNEL_DESCRIPTION);
getNotificationManager().createNotificationChannel(mNotificationChannel);
}
設定通知重要性級別
該級別必須要在 NotificationChannel 的建構函式中指定,總共要五個級別;範圍是從 NotificationManager.IMPORTANCE_NONE(0) ~ NotificationManager.IMPORTANCE_HIGH(4)
,如果要支援 Android 7.1(API 25)及以下的裝置,還得呼叫NotificationCompat 的 setPriority 方法來設定,如下所示
builder.setPriority(NotificationCompat.PRIORITY_DEFAULT);
我們總結一下;Android 8.0 及以上是使用NotificationManager.IMPORTANCE_,Android 7.1 及以下是使用NotificationCompat.PRIORITY_它們都是定義的常量;下面我們以表格的形式更好的展示出來。
對於上面這些通知級別使用者都是可以在 Channel 設定中更改的,嗯就是這樣!
開啟 Channel 設定
為了讓使用者能夠輕鬆訪問 Channel 設定,我們可以通過下面的程式碼在 APP 中加入設定入口點,這樣使用者體驗可能會更好!
public void openChannelSetting(String channelId)
{
Intent intent = new Intent(Settings.ACTION_CHANNEL_NOTIFICATION_SETTINGS);
intent.putExtra(Settings.EXTRA_APP_PACKAGE, getPackageName());
intent.putExtra(Settings.EXTRA_CHANNEL_ID, channelId);
if (getPackageManager().resolveActivity(intent, PackageManager.MATCH_DEFAULT_ONLY) != null)
startActivity(intent);
}
開啟通知設定
public void openNotificationSetting()
{
Intent intent = new Intent(Settings.ACTION_APP_NOTIFICATION_SETTINGS);
intent.putExtra(Settings.EXTRA_APP_PACKAGE, getPackageName());
if (getPackageManager().resolveActivity(intent, PackageManager.MATCH_DEFAULT_ONLY) != null)
startActivity(intent);
}
最後貼一下封裝的程式碼(封裝的不是很好)
/**
* @author xuyj
*/
public class NotificationHelper extends ContextWrapper
{
private NotificationManager mNotificationManager;
private NotificationChannel mNotificationChannel;
public static final String CHANNEL_ID = "default";
private static final String CHANNEL_NAME = "Default Channel";
private static final String CHANNEL_DESCRIPTION = "this is default channel!";
public NotificationHelper(Context base)
{
super(base);
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.O)
{
mNotificationChannel = new NotificationChannel(CHANNEL_ID, CHANNEL_NAME, NotificationManager.IMPORTANCE_DEFAULT);
mNotificationChannel.setDescription(CHANNEL_DESCRIPTION);
getNotificationManager().createNotificationChannel(mNotificationChannel);
}
}
public NotificationCompat.Builder getNotification(String title, String content)
{
NotificationCompat.Builder builder = null;
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.O)
{
builder = new NotificationCompat.Builder(this, CHANNEL_ID);
} else
{
builder = new NotificationCompat.Builder(this);
builder.setPriority(NotificationCompat.PRIORITY_DEFAULT);
}
builder.setContentTitle(title);
builder.setContentText(content);
builder.setSmallIcon(R.mipmap.comments);
builder.setLargeIcon(BitmapFactory.decodeResource(getResources(), R.mipmap.comments));
//點選自動刪除通知
builder.setAutoCancel(true);
return builder;
}
public void notify(int id, NotificationCompat.Builder builder)
{
if (getNotificationManager() != null)
{
getNotificationManager().notify(id, builder.build());
}
}
public void openChannelSetting(String channelId)
{
Intent intent = new Intent(Settings.ACTION_CHANNEL_NOTIFICATION_SETTINGS);
intent.putExtra(Settings.EXTRA_APP_PACKAGE, getPackageName());
intent.putExtra(Settings.EXTRA_CHANNEL_ID, channelId);
if (getPackageManager().resolveActivity(intent, PackageManager.MATCH_DEFAULT_ONLY) != null)
startActivity(intent);
}
public void openNotificationSetting()
{
Intent intent = new Intent(Settings.ACTION_APP_NOTIFICATION_SETTINGS);
intent.putExtra(Settings.EXTRA_APP_PACKAGE, getPackageName());
if (getPackageManager().resolveActivity(intent, PackageManager.MATCH_DEFAULT_ONLY) != null)
startActivity(intent);
}
private NotificationManager getNotificationManager()
{
if (mNotificationManager == null)
mNotificationManager = (NotificationManager) this.getSystemService(this.NOTIFICATION_SERVICE);
return mNotificationManager;
}
}
Demo地址
https://link.jianshu.com/?t=https%3A%2F%2Fgithub.com%2FXuMiaoLee%2FAndroidNotificationChannel.git
參考
https://developer.android.google.cn/training/notify-user/channels.html
https://github.com/googlesamples/android-NotificationChannels/#readme