2

我有一個通知有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)但沒有...我不知道我要去的地方錯了...

我通知的屏幕...

notification

謝謝!

回答

4

在按下操作按鈕的通知永遠不會被解僱。它只是發送你相關的意圖。如果您希望將其解除,您需要致電NoficationManager.cancel(int id)自行取消。您通常會使用處理操作按鈕意圖的相同方法執行此操作。

Notification.FLAG_AUTO_CANCEL您嘗試使用的標誌僅適用於通知正文而不適用於操作按鈕。

+1

謝謝你的回答。我不明白我應該把'NotificationManager.cancel(int id)'放在哪裏。我的第一個動作按鈕開始一個活動,我應該放入該活動的「onCreate」嗎?但第二個動作啓動一個瀏覽器意圖。 – Tenaciousd93

+0

對於第一個動作,您可以將此代碼放入該活動的onCreate()。對於第二個動作,您可以按照以下方式進行。你需要首先開始你的活動,然後調用'cancel()',啓動瀏覽器意圖,並立即'完成'你的活動。所有這些你需要在'onCreate()'方法中完成。這將是沒有佈局的代理活動。 –

+0

我試過了。在第一種情況下一切正常:)但第二個動作不:我創建了一個「BrowserActivity」,並在這個類的onCreate中刪除了通知,並且我用'startActivity(browserIntent)'啓動了瀏覽器意圖,但是'BrowserActivity'永遠不會被調用..我不知道爲什麼.. – Tenaciousd93

0

試試這個

NotificationCompat.Builder mBuilder = new NotificationCompat.Builder(context) 
    .setDefaults(Notification.DEFAULT_ALL); 

,並嘗試發送UNIQUEID在notificationManager.notify(uniqueId, noti);

+0

謝謝您的回答活動,「UNIQUEID」對於2個操作按鈕或通知的唯一號碼的唯一ID? 'NotificationCompat.Builder mBuilder = new NotificationCompat.Builder(context)'替換我的'Notification noti = new Notification.Builder(context)'? – Tenaciousd93

+0

uniqueId是通知的唯一編號。是替換它Notification noti = new Notification.Builder(context)現在被折舊。 –

1

使用取消()中,通過你的行動開始消除通知