Android O適配N(xiāo)otification Channel

從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è)置界面

Notification Setting

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

Channel Setting
創(chuàng)建Notification Channel
  1. 創(chuàng)建NotificationChannel對(duì)象,指定Channel的id、name和通知的重要程度。

  2. setDescription可以指定設(shè)置中Channel的描述,如上圖中的(this is default channel!

  3. 使用NotificationMannagercreateNotificationChannel方法來(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)用NotificationCompatsetPriority方法來(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

?著作權(quán)歸作者所有,轉(zhuǎn)載或內(nèi)容合作請(qǐng)聯(lián)系作者
【社區(qū)內(nèi)容提示】社區(qū)部分內(nèi)容疑似由AI輔助生成,瀏覽時(shí)請(qǐng)結(jié)合常識(shí)與多方信息審慎甄別。
平臺(tái)聲明:文章內(nèi)容(如有圖片或視頻亦包括在內(nèi))由作者上傳并發(fā)布,文章內(nèi)容僅代表作者本人觀(guān)點(diǎn),簡(jiǎn)書(shū)系信息發(fā)布平臺(tái),僅提供信息存儲(chǔ)服務(wù)。

相關(guān)閱讀更多精彩內(nèi)容

  • 一,直觀(guān)來(lái)看 android-o上對(duì)通知做了更細(xì)粒度的管理。根據(jù)app的業(yè)務(wù)場(chǎng)景將通知分類(lèi)。用戶(hù)可以在設(shè)置中選擇接...
    笑羋閱讀 4,565評(píng)論 0 2
  • 原文出處: http://www.androidchina.net/6174.html Notification在...
    木木00閱讀 12,545評(píng)論 3 32
  • Android 自定義View的各種姿勢(shì)1 Activity的顯示之ViewRootImpl詳解 Activity...
    passiontim閱讀 179,048評(píng)論 25 709
  • 一、什么是Notification? Notification是一種有全局效果的通知,可以顯示在系統(tǒng)通知欄。以下內(nèi)...
    douhao1333閱讀 771評(píng)論 0 1
  • 人生不是活在別人的世界里,回想當(dāng)年,自以為是、高不可攀的人,現(xiàn)在又如何?
    V_one閱讀 176評(píng)論 0 1

友情鏈接更多精彩內(nèi)容