天天看點

重溫Android8.0系統的通知欄适配

        說起Android手機的 Notification,是真的痛苦,看到那些經常推送的的東西,如果不是我感興趣或者重要的,我基本上會劃掉不讓他們通知。還好從Android8.0開始Google引入了通知管道這個概念,其實就是開發者定義相應的管道,使用者自由的去控制管道 ,下面圖就是google map 在Android8.0手機上的Notification。我們可以看出這裡面劃分很詳細,使用者可以自己去選擇想要接收的Notification。(可憐我最近沒有8.0手機,隻能用用模拟器了)                                                  
重溫Android8.0系統的通知欄适配
重溫Android8.0系統的通知欄适配
适配         
  • 檢查targetSdkVersion已經指定到了26或者更高,如圖所示:                               
    重溫Android8.0系統的通知欄适配
  • 建立通知管道:
//判斷版本
    if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.O) {
        String channelId = "attention";//唯一的标志id
        String channelName = "關注消息";//給使用者看的,
        int importance = NotificationManager.IMPORTANCE_HIGH;//通知的等級,使用者也可以修改
        createChannel(channelId, channelName, importance);

        channelId = "comment";
        channelName = "評論消息";
        importance = NotificationManager.IMPORTANCE_DEFAULT;
        createChannel(channelId, channelName, importance);

        channelId = "news";
        channelName = "新聞消息";
        importance = NotificationManager.IMPORTANCE_LOW;
        createChannel(channelId, channelName, importance);

    }
}

@TargetApi(Build.VERSION_CODES.O)
private void createChannel(String channelId, String channelName, int importance) {
    //開始建立
    NotificationChannel channel = new NotificationChannel(channelId, channelName, importance);
    NotificationManager notificationManager = (NotificationManager) getSystemService(
            NOTIFICATION_SERVICE);
    notificationManager.createNotificationChannel(channel);
}
           
建立完成,運作後,可以在設定通知的地方看到我們設定的管道,可以手動去更改:                                                                 
重溫Android8.0系統的通知欄适配
  • 顯示通知
private void sendAttention() {

    NotificationManager notificationManager = (NotificationManager) getSystemService(NOTIFICATION_SERVICE);
    //注意第二個參數為channelId,必須加上和以前的版本有些差別
    Notification notification = new NotificationCompat.Builder(this, "attention")
            .setContentTitle("關注通知")
            .setContentText("mst關注了你")
            .setWhen(System.currentTimeMillis())
            .setSmallIcon(R.mipmap.ic_launcher)
            .setLargeIcon(BitmapFactory.decodeResource(getResources(), R.mipmap.ic_launcher))
            .setAutoCancel(true)
            .build();
    notificationManager.notify(1, notification);

}

private void sendComment() {

    NotificationManager notificationManager = (NotificationManager) getSystemService(NOTIFICATION_SERVICE);
    //注意第二個參數為channelId,必須加上和以前的版本有些差別
    Notification notification = new NotificationCompat.Builder(this, "comment")
            .setContentTitle("評論通知")
            .setContentText("你最近還好嗎?")
            .setWhen(System.currentTimeMillis())
            .setSmallIcon(R.mipmap.ic_launcher)
            .setLargeIcon(BitmapFactory.decodeResource(getResources(), R.mipmap.ic_launcher))
            .setAutoCancel(true)
            .build();
    notificationManager.notify(2, notification);

}

private void sendNews() {

    NotificationManager notificationManager = (NotificationManager) getSystemService(NOTIFICATION_SERVICE);
    //注意第二個參數為channelId,必須加上和以前的版本有些差別
    Notification notification = new NotificationCompat.Builder(this, "news")
            .setContentTitle("今日要點")
            .setContentText("明天世界杯揭幕戰")
            .setWhen(System.currentTimeMillis())
            .setSmallIcon(R.mipmap.ic_launcher)
            .setLargeIcon(BitmapFactory.decodeResource(getResources(), R.mipmap.ic_launcher))
            .setAutoCancel(true)
            .build();
    notificationManager.notify(3, notification);

}
           
從下圖可以看出隻會在設定優先級高的時候notication才會彈出來,其他的則出現在通知欄,快速滑動通知欄可以關閉一條通知,但是在Android8.0中緩慢地向左或者向右滑動,就會看到這樣兩個按鈕(好像和我當時用的華為手機不太一樣),一個是暫緩通知我一會再看,一個是設定用來控制notification
重溫Android8.0系統的通知欄适配
重溫Android8.0系統的通知欄适配
重溫Android8.0系統的通知欄适配

繼續閱讀