KDOSHI KDOSHI - 1 year ago 92
Android Question

Pending Intent not receiving the putExtraBudle for diffrent notificationId

enter image description hereI am using the code of PingService.java https://github.com/android/platform_development/blob/master/samples/training/notify-user/src/com/example/android/pingme/PingService.java

* PingService creates a notification that includes 2 buttons: one to snooze the
* notification, and one to dismiss it.

public class PingService extends IntentService {

private NotificationManager mNotificationManager;
private String mMessage;
private int mMillis;
NotificationCompat.Builder builder;

public PingService() {

// The super call is required. The background thread that IntentService
// starts is labeled with the string argument you pass.

protected void onHandleIntent(Intent intent) {
// The reminder message the user set.
mMessage = intent.getStringExtra(CommonConstants.EXTRA_MESSAGE);
// The timer duration the user set. The default is 10 seconds.
mMillis = intent.getIntExtra(CommonConstants.EXTRA_TIMER,
NotificationManager nm = (NotificationManager)

String action = intent.getAction();
// This section handles the 3 possible actions:
// ping, snooze, and dismiss.
if(action.equals(CommonConstants.ACTION_PING)) {
issueNotification(intent, mMessage);
} else if (action.equals(CommonConstants.ACTION_SNOOZE)) {
Log.d(CommonConstants.DEBUG_TAG, getString(R.string.snoozing));
// Sets a snooze-specific "done snoozing" message.
issueNotification(intent, getString(R.string.done_snoozing));

} else if (action.equals(CommonConstants.ACTION_DISMISS)) {

private void issueNotification(Intent intent, String msg) {
mNotificationManager = (NotificationManager)

// Sets up the Snooze and Dismiss action buttons that will appear in the
// expanded view of the notification.
Intent dismissIntent = new Intent(this, PingService.class);
PendingIntent piDismiss = PendingIntent.getService(this, 0, dismissIntent, 0);

Intent snoozeIntent = new Intent(this, PingService.class);
PendingIntent piSnooze = PendingIntent.getService(this, 0, snoozeIntent, 0);

// Constructs the Builder object.
builder =
new NotificationCompat.Builder(this)
.setDefaults(Notification.DEFAULT_ALL) // requires VIBRATE permission
* Sets the big view "big text" style and supplies the
* text (the user's reminder message) that will be displayed
* in the detail area of the expanded notification.
* These calls are ignored by the support library for
* pre-4.1 devices.
.setStyle(new NotificationCompat.BigTextStyle()
.addAction (R.drawable.ic_stat_dismiss,
getString(R.string.dismiss), piDismiss)
.addAction (R.drawable.ic_stat_snooze,
getString(R.string.snooze), piSnooze);

* Clicking the notification itself displays ResultActivity, which provides
* UI for snoozing or dismissing the notification.
* This is available through either the normal view or big view.
Intent resultIntent = new Intent(this, ResultActivity.class);
resultIntent.putExtra(CommonConstants.EXTRA_MESSAGE, msg);

// Because clicking the notification opens a new ("special") activity, there's
// no need to create an artificial back stack.
PendingIntent resultPendingIntent =


private void issueNotification(NotificationCompat.Builder builder) {
mNotificationManager = (NotificationManager)
// Including the notification ID allows you to update the notification later on.
mNotificationManager.notify(CommonConstants.NOTIFICATION_ID, builder.build());

private void startTimer(int millis) {
Log.d(CommonConstants.DEBUG_TAG, getString(R.string.timer_start));
try {

} catch (InterruptedException e) {
Log.d(CommonConstants.DEBUG_TAG, getString(R.string.sleep_error));
Log.d(CommonConstants.DEBUG_TAG, getString(R.string.timer_finished));

Intent mServiceIntent = new Intent(context.getApplicationContext(), PingService.class);
mServiceIntent.putExtra(Const.EXTRA_MESSAGE, "" + msg);
mServiceIntent.putExtra(Const.ID, reminder_id);

And passing the data to issueNotification method for Action Button intent like

Intent dismissIntent = new Intent(this, PingService.class);
dismissIntent.putExtra(CommonConstants.ID, reminder_id);
PendingIntent piDismiss = PendingIntent.getService(this, 0, dismissIntent, 0);

Intent snoozeIntent = new Intent(this, PingService.class);
snoozeIntent.putExtra(CommonConstants.ID, reminder_id);
PendingIntent piSnooze = PendingIntent.getService(this, 0, snoozeIntent, 0);

But i am not getting the ID at the time of Click event of the Action button from issueNotification and passing the intent in onHandleIntent method

It is automatically set to 1 after Notification generation and i am not able to cancel that notification due to wrong notification id set into putExtra of PendingIntent

I am trying with passing different flag like

PendingIntent piDismiss = PendingIntent.getService(this, 0, taken_intent, PendingIntent.FLAG_UPDATE_CURRENT);

Please help me out i am trying it since long time but not getting the correct id.
My putExtra is override with 1 somehow....

Thank you in advnace

Answer Source

After so much reasearching finally got a solution that i need to pass different code in pending intent like

PendingIntent piDismiss = PendingIntent.getBroadcast(this, CommonConstants.NOTIFICATION_ID,   dismissIntent, 0);

which is dynamic one and unique one in each Action Button case. Use Broadcast receiver to achieve this instead of Service ( getBroadcast ).

Thank you.

Recommended from our users: Dynamic Network Monitoring from WhatsUp Gold from IPSwitch. Free Download