從Android 8.0(API 26)開(kāi)始,所有的Notification都要指定Channel(通道),對(duì)于每一個(gè)Channel你都可以單獨(dú)去設(shè)置它;比如通知開(kāi)關(guān)、提示音、是否震動(dòng)或者是重要程度等;這樣每個(gè)應(yīng)用程序的通知在用戶(hù)面前都是透明的。
下面我們來(lái)看一下通知的設(shè)置頁(yè)面和Channel的設(shè)置界面

這邊通知設(shè)置界面中的類(lèi)別指的就是Channel,你必須要?jiǎng)?chuàng)建一個(gè)或者多個(gè)Channel;這邊需要注意的是如果你的tartgetSdkVersion>=26,如果你發(fā)布通知不指定Channel的話(huà),通知是不會(huì)顯示的(系統(tǒng)會(huì)自動(dòng)記錄錯(cuò)誤)。

創(chuàng)建Notification Channel
創(chuàng)建NotificationChannel對(duì)象,指定Channel的id、name和通知的重要程度。
setDescription可以指定設(shè)置中Channel的描述,如上圖中的(this is default channel!)使用
NotificationMannager的createNotificationChannel方法來(lái)添加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);
}
設(shè)置通知重要性級(jí)別
該級(jí)別必須要在NotificationChannel的構(gòu)造函數(shù)中指定,總共要五個(gè)級(jí)別;范圍是從 NotificationManager.IMPORTANCE_NONE(0) ~ NotificationManager.IMPORTANCE_HIGH(4)
,如果要支持Android 7.1(API 25)及以下的設(shè)備,還得調(diào)用NotificationCompat的setPriority方法來(lái)設(shè)置,如下所示
builder.setPriority(NotificationCompat.PRIORITY_DEFAULT);
我們總結(jié)一下;Android 8.0及以上是使用NotificationManager.IMPORTANCE_*,Android 7.1及以下是使用NotificationCompat.PRIORITY_*它們都是定義的常量;下面我們以表格的形式更好的展示出來(lái)。
| 用戶(hù)通知級(jí)別 | Android 8.0及以上 | Android 7.1及以下 |
|---|---|---|
| 緊急級(jí)別 (發(fā)出通知聲音并顯示為提示通知) | IMPORTANCE_HIGH |
PRIORITY_HIGH或者PRIORITY_MAX
|
| 高級(jí)別(發(fā)出通知聲音,并且通知欄有通知) | IMPORTANCE_DEFAULT |
PRIORITY_DEFAULT |
| 中等級(jí)別(沒(méi)有通知聲音,但是通知欄有通知) | IMPORTANCE_LOW |
PRIORITY_LOW |
| 低級(jí)別(沒(méi)有通知聲音,也不會(huì)出現(xiàn)在狀態(tài)欄上) | IMPORTANCE_MIN |
PRIORITY_MIN |
對(duì)于上面這些通知級(jí)別用戶(hù)都是可以在Channel設(shè)置中更改的,嗯就是這樣!
打開(kāi)Channel設(shè)置
為了讓用戶(hù)能夠輕松訪(fǎng)問(wèn)Channel設(shè)置,我們可以通過(guò)下面的代碼在APP中加入設(shè)置入口點(diǎn),這樣用戶(hù)體驗(yàn)可能會(huì)更好!
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);
}
打開(kāi)通知設(shè)置
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));
//點(diǎn)擊自動(dòng)刪除通知
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點(diǎn)這里
參考
https://developer.android.google.cn/training/notify-user/channels.html
https://github.com/googlesamples/android-NotificationChannels/#readme