關於Android Notification.Builder不顯示通知的問題
阿新 • • 發佈:2019-02-09
今天看Android書上關於Notification的程式碼
功能成功實現了。但是發現Notification的建構函式和setLatestEventInfo函式已經不推薦使用了,官方建議使用Notification.Builder來代替。<span style="white-space:pre"> </span>Intent i = new Intent(this,NotificationActivity.class); i.putExtra("notificationID",notificationID); PendingIntent pendingIntent = PendingIntent.getActivity(this,0,i,0); NotificationManager nm = (NotificationManager)getSystemService(NOTIFICATION_SERVICE); Notification notif = new Notification( R.mipmap.ic_launcher, "Reminder:Meeting starts in 5 minutes", System.currentTimeMillis()); CharSequence from = "System Alarm"; CharSequence message = "Meeting with customer at 3pm..."; notif.setLatestEventInfo(this,from,message,pendingIntent); <span style="white-space:pre"> </span>notif.vibrate = new long[]{100,250,100,500}; nm.notify(notificationID,notif);
參照文件,我這樣寫了。
發現並沒有在通知欄中顯示通知。Notification notif = new Notification.Builder(this) .setContentIntent(pendingIntent) .setTicker("Reminder:Meeting starts in 5 minutes") .setContentTitle("System Alarm") .setContentText("Meeting with customer at 3pm...") .build();
後來在 這裡 找到了這樣的話:
A Notification object must contain the following:
A small icon, set by setSmallIcon()
A title, set by setContentTitle()
Detail text, set by setContentText()
所以我修改成這樣:
Notification notif = new Notification.Builder(this) .setContentIntent(pendingIntent) .setTicker("Reminder:Meeting starts in 5 minutes") .setSmallIcon(R.mipmap.ic_launcher) .setContentTitle("System Alarm") .setContentText("Meeting with customer at 3pm...") .build();
難道必須三者都指定才能顯示通知嗎?我進行了嘗試,如果不呼叫
setContentTitle() 或 setContentText()
是不會有問題的。
但是,不呼叫
setTicker("Reminder:Meeting starts in 5 minutes")
會造成通知欄不顯示非展開模式的通知,只顯示展開模式下的通知。
如果不呼叫
<strong>setSmallIcon(R.mipmap.ic_launcher)</strong>
一定不顯示通知,包括展開模式下的。
上圖: