Android 8.0系統(tǒng)的通知欄適配

為什么要進行通知欄適配?
現(xiàn)在經常是早上一覺醒來拿起手機一看,通知欄上全是各種APP的推送,煩。隨著智能手機發(fā)展的成熟,通知欄搞得越來越不討人喜歡了。各個App都希望搶占通知欄的空間,來盡可能地銷售自己的產品。

通知欄是Android系統(tǒng)原創(chuàng)的,雖說喬布斯一直認為Android系統(tǒng)是徹底抄襲iOS的一個產品,蘋果在iOS 5之后也抄襲了Android的通知欄。

通知欄的設計巧妙,它不占用屏幕空間,只有當用戶需要的時候用手指在狀態(tài)欄上向下滑動,通知欄的內容才會顯示出來,在智能手機發(fā)展的初期解決了手機屏幕小,內容展示不足的問題。

從Android 8.0系統(tǒng)開始,Google引入了通知渠道這個概念。
什么是通知渠道呢?顧名思義,就是每條通知都要屬于一個對應的渠道。每個App自由創(chuàng)建通知渠道,但這些通知渠道的控制權都是掌握在用戶手上。用戶隨便設置通知渠道的重要程度,是否響鈴、是否振動、或者是否要關閉這個渠道的通知。

舉個例子,我希望立馬收到支付寶的收款信息,因為不想錯過每筆錢,但是又不想收到支付寶給我推薦的周圍美食,因為我沒錢去大飯店。這種情況,支付寶就可以創(chuàng)建兩種通知渠道,一個收支,一個推薦,而我作為用戶對推薦類的通知討厭,那么我直接把推薦通知渠道關閉,這樣不影響我關心的通知,又不會讓那些我不關心的通知來打擾我了。

我一定要搞適配嗎?
Google這次對于8.0系統(tǒng)通知渠道的態(tài)度硬硬的。如果你升級了AppCompat庫,所有使用AppCompat庫來構建通知的地方全部會進行廢棄方法提示,如下所示:


image.png

Google也并沒做絕,即使方法標為廢棄,還是可以湊合用??墒侨绻銓㈨椖恐械膖argetSdkVersion指定到了26或者更高,Android系統(tǒng)認為APP已經做好了8.0系統(tǒng)的適配工作,當然包括了通知欄的適配。這個時候不使用通知渠道,你App的通知將不彈出。

那么現(xiàn)在開始我們就正式來學習一下如何進行8.0系統(tǒng)中通知欄的適配。

創(chuàng)建通知渠道
首先我們使用Android Studio來新建一個項目,就叫它NoticationChannel

創(chuàng)建好項目,打開app/build.gradle文件檢查一下,targetSdkVersion已經指定到了26或者更高,如下:

android {
    compileSdkVersion 28
    buildToolsVersion "28.0.0"
    defaultConfig {
        applicationId "com.example.noticationchannel"
        minSdkVersion 21
        targetSdkVersion 28
        versionCode 100
        versionName "1.0"
        testInstrumentationRunner "androidx.test.runner.AndroidJUnitRunner"
    }
}

public class MainActivity extends AppCompatActivity {

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
    }

    public void sendChatMsg(View view) {
        NotificationManager manager = (NotificationManager) getSystemService(NOTIFICATION_SERVICE);
      
        if (android.os.Build.VERSION.SDK_INT >= android.os.Build.VERSION_CODES.O) {
            String channelId = "chat";
            String channelName = "聊天消息";
            int importance = NotificationManager.IMPORTANCE_HIGH;
            NotificationChannel channel = new NotificationChannel(channelId, channelName, importance);
            NotificationManager notificationManager = (NotificationManager) getSystemService(NOTIFICATION_SERVICE);
            notificationManager.createNotificationChannel(channel);
        }

        Notification notification = new NotificationCompat.Builder(this, "chat")
                .setContentTitle("來了吧,小伙子 聊天消息")
                .setContentText("中午吃點啥呢?")
                .setWhen(System.currentTimeMillis())
                .setSmallIcon(R.mipmap.ic_launcher)
                .setAutoCancel(true)
                .build();
        manager.notify(1, notification);
    }

    public void sendSubscribeMsg(View view) {
        if (android.os.Build.VERSION.SDK_INT >= android.os.Build.VERSION_CODES.O) {
            String channelId = "subscribe";
            String channelName = "訂閱消息";
            int importance = NotificationManager.IMPORTANCE_DEFAULT;
            NotificationChannel channel = new NotificationChannel(channelId, channelName, importance);
            NotificationManager notificationManager = (NotificationManager) getSystemService(NOTIFICATION_SERVICE);
            notificationManager.createNotificationChannel(channel);
        }

        NotificationManager manager = (NotificationManager) getSystemService(NOTIFICATION_SERVICE);
        Notification notification = new NotificationCompat.Builder(this, "subscribe")
                .setContentTitle("來了吧,小伙子 訂閱消息")
                .setContentText("房子便宜賣啦,有要買的嗎!")
                .setWhen(System.currentTimeMillis())
                .setSmallIcon(R.mipmap.ic_launcher_round)
                .setAutoCancel(true)
                .build();
        manager.notify(2, notification);
    }

    public void cancelChatMsg(View view) {
        NotificationManager manager = (NotificationManager) getSystemService(NOTIFICATION_SERVICE);
        manager.cancel(1);
        if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.O) {
            manager.deleteNotificationChannel("chat");
        }
    }

    public void cancelSubscribeMsg(View view) {
        NotificationManager manager = (NotificationManager) getSystemService(NOTIFICATION_SERVICE);
        manager.cancel(2);
        if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.O) {
            manager.deleteNotificationChannel("chat");
        }
    }

}
下面我們就來讓通知顯示出來首先修改activity_main.xml中的代碼,如下所示:
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:orientation="vertical">

    <LinearLayout
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:layout_marginTop="60dp"
        android:gravity="center_horizontal"
        android:orientation="horizontal">

        <androidx.appcompat.widget.AppCompatButton
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:onClick="sendChatMsg"
            android:text="發(fā)送聊天消息" />

        <androidx.appcompat.widget.AppCompatButton
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:onClick="cancelChatMsg"
            android:text="取消聊天消息" />

    </LinearLayout>

    <LinearLayout
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:layout_marginTop="60dp"
        android:gravity="center_horizontal"
        android:orientation="horizontal">

        <androidx.appcompat.widget.AppCompatButton
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:onClick="sendSubscribeMsg"
            android:text="發(fā)送訂閱消息" />

        <androidx.appcompat.widget.AppCompatButton
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:onClick="cancelSubscribeMsg"
            android:text="取消訂閱消息" />

    </LinearLayout>

</LinearLayout>

想象一下我們正在開發(fā)類似于微信的APP,其中APP通知主要分為兩類,一類是聊天消息,這類消息非常重要,因此重要等級設為了IMPORTANCE_HIGH。另一類公眾號訂閱消息,這類消息不那么重要,重要等級+我設為了IMPORTANCE_DEFAULT。當然,重要等級還可以設置為IMPORTANCE_LOW、+IMPORTANCE_MIN,分別對應更低的通知重要程度。

代碼不長,要確保的是當前手機的系統(tǒng)版本必須是Android 8.0系統(tǒng)或者更高,低版本的手機系統(tǒng)沒有通知渠道功能,不做系統(tǒng)版本檢查會在低版本手機上造成崩潰。

創(chuàng)建通知渠道很簡單,需要注意的是,創(chuàng)建一個通知渠道需要渠道ID、渠道名稱以及重要等級這三個參數(shù),其中渠道ID隨便寫,只要保證全局獨一份就可以。渠道名稱是給用戶看的,要能夠表達清楚這個渠道的用途。等級不同決定通知行為不同,這里只是初始狀態(tài)下的等級,用戶隨時更改渠道的等級,APP 無法干預。

現(xiàn)在運行一下代碼了,運行成功之后,點擊發(fā)送聊天消息,點擊發(fā)送訂閱消息,狀態(tài)欄上會有通知小圖標,用手指在狀態(tài)欄上向下滑動,如下圖所示:


image.png

剛才我們創(chuàng)建兩個通知渠道已經顯示出來了。由于這兩個通知渠道的重要等級不同,通知的行為也是不同的,聊天消息可以發(fā)出在屏幕上彈出通知,而訂閱消息不會彈出通知。

進入到設置 -> 應用 -> 通知當中,查看NoticationChannel這個APP的通知界面,如下圖所示:


image.png

當然,用戶還可以點擊進去對該通知渠道進行任意的修改,比如降低聊天消息的重要等級,甚至是可以完全關閉該渠道的通知。

管理通知渠道
通知渠道一旦創(chuàng)建就不能再通過代碼修改了。為此,Android賦予了開發(fā)者讀取通知渠道配置的權限,如果某個功能必須配置通知渠道才能使用,可以提示用戶手動更改通知渠道配置。

概念總是抽象,我們還是通過具體例子來實象。想象我們開發(fā)的是一個類似于微信的APP,聊天消息是重要地,如果用戶將聊天消息的通知渠道關閉了,所有重要聊天通知全部都丟了,為此我們一定要保證用戶打開了聊天消息通知渠道才行。

修改MainActivity中的代碼

public void sendChatMsg(View view) {
        NotificationManager manager = (NotificationManager) getSystemService(NOTIFICATION_SERVICE);
        if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.O) {
            NotificationChannel channel = manager.getNotificationChannel("chat");
            if (channel != null && channel.getImportance() == NotificationManager.IMPORTANCE_NONE) {
                Intent intent = new Intent(Settings.ACTION_CHANNEL_NOTIFICATION_SETTINGS);
                intent.putExtra(Settings.EXTRA_APP_PACKAGE, getPackageName());
                intent.putExtra(Settings.EXTRA_CHANNEL_ID, channel.getId());
                startActivity(intent);
                Toast.makeText(this, "請手動將通知打開", Toast.LENGTH_SHORT).show();
            }
        }
      ......
}

這里我們對sendChatMsg()方法進行了修改,通過getNotificationChannel()方法獲取到了NotificationChannel對象,如果為null,沒有設置通知渠道,否則就可以讀取該通知渠道下的所有配置了。這里我們判斷如果通知渠道的importance等于IMPORTANCE_NONE,就說明用戶將該渠道的通知給關閉了,這時會跳轉到通知的設置界面提醒用戶手動打開。

1 當聊天消息的通知渠道關閉后,下次再次發(fā)送聊天消息直接跳轉到通知設置界面,提醒用戶手動將通知打開。
2 除了以上管理通知渠道的方式之外,Android 8.0還賦予了我們刪除通知渠道的功能,只需使用如下代碼即可刪除:
NotificationManager manager = (NotificationManager) getSystemService(NOTIFICATION_SERVICE);
manager.deleteNotificationChannel(channelId);

文章中的示例源碼點擊 https://github.com/githubwwj/NoticationChannel下載

喜歡就分享給你身邊的三個小伙伴

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

友情鏈接更多精彩內容