原文: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
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之前的平台
不過個人建議是加一個版本判斷 因為可以加上另外一段代碼來兼容25之前的平台
下面是最終代碼
Intent intent = new Intent(Intent.ACTION_VIEW,Uri.parse("http://www.baidu.com"));
PendingIntent pendingIntent = PendingIntent.getActivity(MainActivity.this,0,intent,0);
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);
}
{
//當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);
}
