解決Fail to post notification on channel "null"的方法

新手 網上關於這個問題的解決方法有點少 下面是自己的一些理解和測試後確實可用的代碼

在sdk版本爲25或25之前想在notification中添加一個點擊事件 只要通過setContentIntent()傳入一個PendingIntent就可以實現通知點擊事件 代碼如下

Intent intent = new Intent(Intent.ACTION_VIEW,Uri.parse("http://www.baidu.com"));
PendingIntentpendingIntent = PendingIntent.getActivity(MainActivity.this,0,intent,0);
NotificationManager manager = (NotificationManager)getSystemService(NOTIFICATION_SERVICE);
Notification notification = new NotificationCompat.Builder(MainActivity.this)
                                .setContentTitle("This is content title")
                                .setContentText("This is content text")
                                .setSmallIcon(R.mipmap.ic_launcher)
                                .build();
manager.notify(1,notification);

但對於不少像我一樣的新手用的模擬器或者調試工具都是最新版本即sdk爲26的平臺
所以如果還用上面的代碼就會跳出這個錯誤
這裏寫圖片描述

當時最後是在一個Android O的更新說明中找到了答案
傳送門:https://www.ithome.com/html/android/298943.htm

這裏寫圖片描述

再反觀錯誤提示

Failed to post notification on channel “null”

這個時候我們就知道問題是什麼啦

意思就是在Android O後 引入了一個叫NotificationChannel的類 在sdk版本爲26的時候 我們不加這個東西 就設置用不了點擊事件啦

就我個人的理解 NotificationChannel的作用就是細化對notification的設置 之前關於notification的設置都是可以在Notification.Builder(Context,int)中完成

引入NotificationChannel後 關於震動 聲音 提示燈 優先級的設置就可以在NotificationChannel中設置

不過個人測試後 感覺Android O在通知方面更注重用戶了 就算你在代碼中設置了重要性 但是實際提示的效果還是根據用戶在手機中設置的通知重要性來判斷 所以個人感覺開發者在代碼設置重要性這部分可以直接略去

加入NotificationChannel後
代碼如下


String id ="channel_1";//channel的id
String description = "123";//channel的描述信息
int importance = NotificationManager.IMPORTANCE_LOW;//channel的重要性
NotificationChannel channel = new NotificationChannel(id, "123", importance);//生成channel
//爲channel添加屬性
//channel.enableVibration(true); 震動
//channel.enableLights(true);提示燈
NotificationManager manager = (NotificationManager)getSystemService(NOTIFICATION_SERVICE);
manager.createNotificationChannel(channel);//添加channel
Notification notification = new Notification.Builder(MainActivity.this,id)
                                    //注意這裏多了一個參數id,指配置的NotificationChannel的id
                                    //你可以自己去試一下 運行一次後 即配置完後 將這行代碼以上的代
                                    //碼註釋掉 將參數id直接改成“channel_1”也可以成功運行
                                    //但改成別的如“channel_2”就不行了
                                    .setCategory(Notification.CATEGORY_MESSAGE)
                                    .setSmallIcon(R.mipmap.ic_launcher)
                                    .setContentTitle("This is a content title")
                                    .setContentText("This is a content text")
                                    .setContentIntent(pendingIntent)
                                    .setAutoCancel(true)
                                    .build();
manager.notify(1,notification);

不過要用於項目中 還是不行 因爲我們要考慮一個兼容版本問題 所以還要加上一個版本判斷 或者 是一個requireApi爲Android O
不過個人建議是加一個版本判斷 因爲可以加上另外一段代碼來兼容25之前的平臺

下面是最終代碼

Intent intent = new Intent(Intent.ACTION_VIEW,Uri.parse("http://www.baidu.com"));
PendingIntent pendingIntent = PendingIntent.getActivity(MainActivity.this,0,intent,0);

NotificationManager manager = (NotificationManager)getSystemService(NOTIFICATION_SERVICE);

 if(Build.VERSION.SDK_INT >= 26) 
 {
               //當sdk版本大於26
   String id = "channel_1";
   String description = "143";
   int importance = NotificationManager.IMPORTANCE_LOW;
   NotificationChannel channel = new NotificationChannel(id, description, importance);
//                     channel.enableLights(true);
//                     channel.enableVibration(true);//
   manager.createNotificationChannel(channel);
   Notification notification = new Notification.Builder(MainActivity.this, id)
                                    .setCategory(Notification.CATEGORY_MESSAGE)
                                    .setSmallIcon(R.mipmap.ic_launcher)
                                    .setContentTitle("This is a content title")
                                    .setContentText("This is a content text")
                                    .setContentIntent(pendingIntent)
                                    .setAutoCancel(true)
                                    .build();
   manager.notify(1, notification);
   }
   else
   {
            //當sdk版本小於26
    Notification notification = new NotificationCompat.Builder(MainActivity.this)
                                    .setContentTitle("This is content title")
                                    .setContentText("This is content text")
                                    .setContentIntent(pendingIntent)
                                    .setSmallIcon(R.mipmap.ic_launcher)
                                    .build();
    manager.notify(1,notification);
   }
發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章