先上8.0之前通知的代碼
NotificationCompat.Builder builder = new NotificationCompat.Builder(this);
builder.setSmallIcon(R.mipmap.ic_launcher);
builder.setContentTitle("新消息"); builder.setContentText("你有一條新的消息"); builder.setDefaults(Notification.DEFAULT_SOUND); // 設(shè)置聲音/震動等 //設(shè)置點擊通知跳轉(zhuǎn)頁面后,通知消失 builder.setAutoCancel(true);
NotificationManager notificationManager =(NotificationManager)getSystemService(Context.NOTIFICATION_SERVICE); notificationManager.notify(11,builder.build())
在8.0之前這樣寫是沒有問題的,但是在8.0的手機上肉康,會發(fā)現(xiàn)無效募强,原因是沒有設(shè)置setChannelId,至于為什么要這樣做萧福,后面再說拉鹃,
NotificationCompat.Builder builder = new NotificationCompat.Builder(this); builder.setSmallIcon(R.mipmap.ic_launcher); builder.setContentTitle("新消息"); builder.setContentText("你有一條新的消息");
builder.setChannelId("1");
builder.setDefaults(Notification.DEFAULT_SOUND); // 設(shè)置聲音/震動等 //設(shè)置點擊通知跳轉(zhuǎn)頁面后,通知消失 builder.setAutoCancel(true);
NotificationManager notificationManager =(NotificationManager)getSystemService(Context.NOTIFICATION_SERVICE); notificationManager.notify(11,builder.build());
直接setChannelId設(shè)置一個id就能解決8.0系統(tǒng)通知不顯示的問題统锤,如果要設(shè)置通知的點擊效果
NotificationCompat.Builder builder = new NotificationCompat.Builder(this);
?builder.setSmallIcon(R.mipmap.ic_launcher);
?builder.setContentTitle("新消息");
?builder.setContentText("你有一條新的消息");
?builder.setDefaults(Notification.DEFAULT_SOUND); // 設(shè)置聲音/震動等 //設(shè)置點擊通知跳轉(zhuǎn)頁面后毛俏,通知消失 builder.setAutoCancel(true); builder.setChannelId("1");
?Intent intent = new Intent(this,MainActivity2.class);
?PendingIntent pi = PendingIntent.getActivity(this, 0, intent, PendingIntent.FLAG_UPDATE_CURRENT); builder.setContentIntent(pi); ?
NotificationManager notificationManager = (NotificationManager) getSystemService(Context.NOTIFICATION_SERVICE); notificationManager.notify(11,builder.build());
這樣就ok了