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

原文:https://blog.csdn.net/weixin_40604111/article/details/78674563
在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);123456789
但對於很多像我同樣的新手用的模擬器或者調試工具都是最新版本即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);1234567891011121314151617181920212223
不過要用於項目中 仍是不行 由於咱們要考慮一個兼容版本問題 因此還要加上一個版本判斷 或者 是一個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);    }
相關文章
相關標籤/搜索