2015-11-18 51 views
2

我需要使用兩個按鈕來顯示通知。不同的操作需要爲每個按鈕執行。因此,我已經編寫了下面的代碼,但是當我得到多個通知刪除操作未執行時。帶多個按鈕的通知

Random NOTIFICATION_ID = new Random(); 
int CANCELNOTIFICATIONID = NOTIFICATION_ID.nextInt(); 
// define sound URI, the sound to be played when there's a notification 
Uri soundUri = RingtoneManager.getDefaultUri(RingtoneManager.TYPE_NOTIFICATION); 
Log.i("******* Service6", "" + msg); 
// intent triggered, you can add other intent for other actions 
Intent intent = new Intent(GcmIntentService.this, LoginActivity.class); 
PendingIntent pIntent = PendingIntent.getActivity(GcmIntentService.this, 0, intent, 0); 


Intent deleteIntent = new Intent(GcmIntentService.this, DeleteArchiveLoopActivity.class); 
deleteIntent.putExtra(LoopMeConstants.EXTRA_DELETE_ARCHIVE_LOOPS, "Delete loops"); 
Trace.i(TAG, "Looptype Delete loop"); 
deleteIntent.putExtra("DELETE_ARCHIVE_LOOP_ID", loopId); 
deleteIntent.putExtra("NOTIFICATONID", CANCELNOTIFICATIONID); 
deleteIntent.setData(Uri.parse(intent.toUri(Intent.URI_INTENT_SCHEME))); 
//   PendingIntent pDeleteIntent = PendingIntent.getActivity(this, 145623, deleteIntent, PendingIntent.FLAG_UPDATE_CURRENT); 

Intent archiveIntent = new Intent(GcmIntentService.this, DeleteArchiveLoopActivity.class); 
Trace.i(TAG, "Looptype Archive loop"); 
archiveIntent.putExtra(LoopMeConstants.EXTRA_DELETE_ARCHIVE_LOOPS, "Archive loops"); 
archiveIntent.putExtra("DELETE_ARCHIVE_LOOP_ID", loopId); 
archiveIntent.putExtra("NOTIFICATONID", CANCELNOTIFICATIONID); 
archiveIntent.setData(Uri.parse(intent.toUri(Intent.URI_INTENT_SCHEME))); 
//   PendingIntent pArchiveIntent = PendingIntent.getActivity(this, 145623, archiveIntent, PendingIntent.FLAG_UPDATE_CURRENT); 

NotificationCompat.Builder notificationBuilder = new NotificationCompat.Builder(this); 
notificationBuilder.setSmallIcon(R.drawable.ic_launcher); 
notificationBuilder.setContentTitle("Sample"); 
notificationBuilder.setStyle(new NotificationCompat.BigTextStyle().bigText(mPushtext)); 
notificationBuilder.setContentText(mPushtext); 
notificationBuilder.setSound(soundUri); 
notificationBuilder.addAction(R.drawable.delete, "Delete", PendingIntent.getActivity(this, 145623, deleteIntent, PendingIntent.FLAG_ONE_SHOT)); 
notificationBuilder.addAction(R.drawable.archive, "Archive", PendingIntent.getActivity(this, 145623, archiveIntent, PendingIntent.FLAG_UPDATE_CURRENT)); 
notificationBuilder.setAutoCancel(false); 

//   notificationBuilder.setContentIntent(pArchiveIntent); 
Log.i("GCMIntent Srevice5", "" + msg); 
notificationBuilder.setOngoing(true); 
mNotificationManager.notify(CANCELNOTIFICATIONID, notificationBuilder.build()); 

如何解決這個問題。

+0

我覺得你設置的意圖2時間的addAction和你刪除的意圖是通過你的archiveintent覆蓋 –

+0

@VishalGaur我們需要編寫不同的意圖。 – Harish

+0

所以「歸檔」操作正在工作? – 0X0nosugar

回答

1

使用不同requestCode在下面的方法,我已經解決了它。

在意向

我已經給出了兩個differentactivities

Intent deleteIntent = new Intent(GcmIntentService.this, DeleteLoopActivity.class); 

Intent archiveIntent = new Intent(GcmIntentService.this, ArchiveLoopActivity.class); 

notificationBuilder.addAction(R.drawable.delete, "Delete", PendingIntent.getActivity(this, CANCELNOTIFICATIONID, deleteIntent, 0)); 
notificationBuilder.addAction(R.drawable.archive, "Archive", PendingIntent.getActivity(this, CANCELNOTIFICATIONID, archiveIntent, 0)); 

上面的代碼解決了我的問題 感謝所有

1

您需要Pending intent

notificationBuilder.addAction(R.drawable.delete, "Delete", PendingIntent.getActivity(this, 1, deleteIntent, PendingIntent.FLAG_ONE_SHOT)); 
notificationBuilder.addAction(R.drawable.archive, "Archive", PendingIntent.getActivity(this, 2, archiveIntent, PendingIntent.FLAG_UPDATE_CURRENT)); 
+0

如果我給不同的requestCode也不工作。它只爲一個通知工作。 – Harish

+0

嘗試爲不同的通知使用隨機差異請求代碼 – nv95