從Android 8.0(API 26)開始噩茄,所有的Notification
都要指定Channel
(通道)囊颅,對(duì)于每一個(gè)Channel你都可以單獨(dú)去設(shè)置它;比如通知開關(guān)畏妖、提示音脉执、是否震動(dòng)或者是重要程度等;這樣每個(gè)應(yīng)用程序的通知在用戶面前都是透明的戒劫。
下面我們來看一下通知的設(shè)置頁(yè)面和Channel的設(shè)置界面
這邊通知設(shè)置界面中的類別指的就是Channel半夷,你必須要?jiǎng)?chuàng)建一個(gè)或者多個(gè)Channel;這邊需要注意的是如果你的tartgetSdkVersion>=26
,如果你發(fā)布通知不指定Channel的話迅细,通知是不會(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
方法來添加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
方法來設(shè)置,如下所示
builder.setPriority(NotificationCompat.PRIORITY_DEFAULT);
我們總結(jié)一下蜻直;Android 8.0及以上是使用NotificationManager.IMPORTANCE_*
盯质,Android 7.1及以下是使用NotificationCompat.PRIORITY_*
它們都是定義的常量;下面我們以表格的形式更好的展示出來概而。
用戶通知級(jí)別 | Android 8.0及以上 | Android 7.1及以下 |
---|---|---|
緊急級(jí)別 (發(fā)出通知聲音并顯示為提示通知) | IMPORTANCE_HIGH |
PRIORITY_HIGH 或者PRIORITY_MAX
|
高級(jí)別(發(fā)出通知聲音呼巷,并且通知欄有通知) | IMPORTANCE_DEFAULT |
PRIORITY_DEFAULT |
中等級(jí)別(沒有通知聲音,但是通知欄有通知) | IMPORTANCE_LOW |
PRIORITY_LOW |
低級(jí)別(沒有通知聲音赎瑰,也不會(huì)出現(xiàn)在狀態(tài)欄上) | IMPORTANCE_MIN |
PRIORITY_MIN |
對(duì)于上面這些通知級(jí)別用戶都是可以在Channel設(shè)置中更改的王悍,嗯就是這樣!
打開Channel設(shè)置
為了讓用戶能夠輕松訪問Channel設(shè)置餐曼,我們可以通過下面的代碼在APP中加入設(shè)置入口點(diǎn)压储,這樣用戶體驗(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);
}
打開通知設(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