我有一個通知有2個操作。一個意圖打開一個Activity,另一個打開瀏覽器網頁。刪除通知notification.builder對內容點擊
我的代碼:
Intent intent1 = new Intent(context, NotificationClickActivity.class);
PendingIntent pIntent1 = PendingIntent.getActivity(context, 0, intent1, PendingIntent.FLAG_CANCEL_CURRENT);
String url = "http://www.google.com";
Intent browserIntent = new Intent(Intent.ACTION_VIEW);
browserIntent.setData(Uri.parse(url));
PendingIntent pBrowserIntent = PendingIntent.getActivity(context, 1, browserIntent, PendingIntent.FLAG_CANCEL_CURRENT);
// Build notification
Notification noti = new Notification.Builder(context)
.setContentTitle(title)
.setContentText(shortDescription)
.setStyle(new Notification.BigTextStyle().bigText(longDescription))
.setSmallIcon(R.drawable.small_defaulticon)
.setAutoCancel(true)
.setContentIntent(pIntent1)
.addAction(R.drawable.playstore_icon_32, "Dettagli", pIntent1)
.setContentIntent(pBrowserIntent)
.addAction(R.drawable.small_defaulticon, "Vai al sito", pBrowserIntent).build();
NotificationManager notificationManager =
(NotificationManager) context.getSystemService(NOTIFICATION_SERVICE);
// Hide the notification after its selected
noti.flags |= Notification.FLAG_AUTO_CANCEL;
notificationManager.notify(0, noti);
如果我點擊它消失的通知,但如果我點擊2個動作之一,它打開右意圖,但通知不被清除。 我也嘗試.setDeleteIntent(myPendingIntent)
但沒有...我不知道我要去的地方錯了...
我通知的屏幕...
謝謝!
謝謝你的回答。我不明白我應該把'NotificationManager.cancel(int id)'放在哪裏。我的第一個動作按鈕開始一個活動,我應該放入該活動的「onCreate」嗎?但第二個動作啓動一個瀏覽器意圖。 – Tenaciousd93
對於第一個動作,您可以將此代碼放入該活動的onCreate()。對於第二個動作,您可以按照以下方式進行。你需要首先開始你的活動,然後調用'cancel()',啓動瀏覽器意圖,並立即'完成'你的活動。所有這些你需要在'onCreate()'方法中完成。這將是沒有佈局的代理活動。 –
我試過了。在第一種情況下一切正常:)但第二個動作不:我創建了一個「BrowserActivity」,並在這個類的onCreate中刪除了通知,並且我用'startActivity(browserIntent)'啓動了瀏覽器意圖,但是'BrowserActivity'永遠不會被調用..我不知道爲什麼.. – Tenaciousd93