Android8.0、Android9.0 通知notification不顯示

Android O 8.0通知新特性

android 8.0通知新增了NotificationChannel(通知渠道),用來幫助管理用戶通知。Android Studio新版會默認使用新版本的SDK編譯項目,如果App的targetSDKVersion >=26或者Android Studio默認使用高於26的sdk作爲targetSDK時,沒有使用NotificationChannel就會出現在Android 8.0及以上系統通知無法展示的情況。

Android O 引入了 通知渠道(Notification Channels),以提供統一的系統來幫助用戶管理通知,如果是針對 android O 爲目標平臺時,必須實現一個或者多個通知渠道,以向用戶顯示通知。比如聊天軟件,爲每個聊天組設置一個通知渠道,指定特定聲音、燈光等配置。

通過NotificationChannel創建Notification

創建NotificationChannel對象時,除了構造方法裏面的參數,其餘參數是可選的,如果不設置會使用系統默認的,且部分參數因爲手機廠商不同,可能沒有效果比如設置桌面Launcher消息角標

public static String CALENDAR_ID = "channel_01";

private static Notification createNotification(Context context, NotificationManager notificationManager) {
        Notification notification;NotificationCompat.Builder builder;
        if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.O) {
            NotificationChannel notificationChannel = new NotificationChannel(CALENDAR_ID, "123",
                    NotificationManager.IMPORTANCE_DEFAULT);
            // 設置渠道描述
            notificationChannel.setDescription("測試通知組");
            // 是否繞過請勿打擾模式
            notificationChannel.canBypassDnd();
            // 設置繞過請勿打擾模式
            notificationChannel.setBypassDnd(true);
            // 桌面Launcher的消息角標
            notificationChannel.canShowBadge();
            // 設置顯示桌面Launcher的消息角標
            notificationChannel.setShowBadge(true);
            // 設置通知出現時聲音,默認通知是有聲音的
            notificationChannel.setSound(null, null);
            // 設置通知出現時的閃燈(如果 android 設備支持的話)
            notificationChannel.enableLights(true);
            notificationChannel.setLightColor(Color.RED);
            // 設置通知出現時的震動(如果 android 設備支持的話)
            notificationChannel.enableVibration(true);
            notificationChannel.setVibrationPattern(new long[]{100, 200, 300, 400, 500, 400,  
                    300, 200, 400});
            notificationManager.createNotificationChannel(notificationChannel);
        }
        builder = new NotificationCompat.Builder(context, CALENDAR_ID);
        PendingIntent pendingIntent = PendingIntent.getActivity(context, 11, 
                new Intent(context, SecondActivity.class), 0);
        notification = builder.setSmallIcon(R.mipmap.ic_launcher_round)
                .setContentIntent(pendingIntent)
//                .setChannelId(CALENDAR_ID)
                .build();
        return notification;
    }

PS:推薦使用v4包下面的``NotificationCompat.Builder來創建Notification,兼容性和新特性都能使用到,創建Notification.Builder的時候推薦使用雙參數的構造方法 Builder(@NonNull Context context, @NonNull String channelId),單參數的構造方法已經被棄用,可能會因爲沒有配置ChannelId導致通知發送失敗,即網上有的開發者說的Android 8.0通知不顯示的問題

Tips:Android 8.0通知不顯示大概率是因爲沒有配置ChannelId

Android P 9.0通知新特性

Android8.0以後的版本,通知要顯示除了要配置ChannelId,還在系統中註冊應用的通知渠道(NotificationChanne),可以在原來的通知生成之前加入以下代碼:

    private void createNotificationChannel() {
        // Create the NotificationChannel, but only on API 26+ because
        // the NotificationChannel class is new and not in the support library
        if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.O) {
            CharSequence name = getString(R.string.channel_name);
            String description = getString(R.string.channel_description);
            int importance = NotificationManager.IMPORTANCE_DEFAULT;
            NotificationChannel channel = new NotificationChannel(CHANNEL_ID, name, importance);
            channel.setDescription(description);
            // Register the channel with the system; you can't change the importance
            // or other notification behaviors after this
            NotificationManager notificationManager = getSystemService(NotificationManager.class);
            notificationManager.createNotificationChannel(channel);
        }
    }

需要注意的是ChannelId和後面NotificationCompat.Builder中的值保持一致,方法裏面判斷了Android 8.0及以上纔會向系統註冊通知渠道(NotificationChanne)

Tips:Android 9.0以及以後版本通知不顯示大概率是向系統註冊通知渠道(NotificationChanne)

通知的重要級別

通知的重要級別在Android 8.0 上下的設置方式、命名和值都不一樣,比如同樣是DEFAULT,Android 8.0裏面是IMPORTANCE_DEFAULT,值是3,Android 7.0裏面是PRIORITY_DEFAULT,值是0
Android 7.0設置方法

builder.setPriority(NotificationCompat.PRIORITY_DEFAULT);

Android 8.0是通過NotificationChannel構造方法傳入

NotificationChannel notificationChannel = new NotificationChannel(CALENDAR_ID, "123", 
        NotificationManager.IMPORTANCE_DEFAULT);
notificationManager.createNotificationChannel(notificationChannel);

下面通過表格說明Android 8.0及以上與Android 8.0以下的通知用戶級別的關係

用戶通知級別 Android 8.0及以上 Android 7.0及以下
緊急級別(發出通知聲音並顯示爲提示通知) IMPORTANCE_HIGH PRIORITY_HIGH或者PRIORITY_MAX
高級別(發出通知聲音並且通知欄有通知 IMPORTANCE_DEFAULT PRIORITY_DEFAULT
中等級別(沒有通知聲音但通知欄有通知) IMPORTANCE_LOW PRIORITY_LOW
低級別(沒有通知聲音也不會出現在狀態欄上) IMPORTANCE_MIN PRIORITY_MIN

根據表格顯示,級別是高級及以上級別時,系統是會發出聲音的,不管你有沒有設置提示語音。

參考鏈接:Android O(8.0) Notification解決方案

發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章