Determine Addaction Click for Android Notifications

Determine addAction click for Android notifications

It's because you're using FLAG_UPDATE_CURRENT with Intents that have the same action

From the docs:

if the described PendingIntent already exists, then keep it but its replace its extra data with what is in this new Intent.

When you specify pendingIntentMaybe and pendingIntentNo, the system uses the PendingIntent created for pendingIntentYes, but it overwrites the extras. Thus, all three variables refer to the same object, and the last extras specified were for pendingIntentNo.

You should specify an alternative action for each Intent. You can still have one BroadcastReceiver, and just have it intercept all three actions. This would be less confusing semantically as well :)

Your Notification poster:

//Yes intent
Intent yesReceive = new Intent();
yesReceive.setAction(YES_ACTION);
PendingIntent pendingIntentYes = PendingIntent.getBroadcast(this, 12345, yesReceive, PendingIntent.FLAG_UPDATE_CURRENT);
mBuilder.addAction(R.drawable.calendar_v, "Yes", pendingIntentYes);

//Maybe intent
Intent maybeReceive = new Intent();
maybeReceive.setAction(MAYBE_ACTION);
PendingIntent pendingIntentMaybe = PendingIntent.getBroadcast(this, 12345, maybeReceive, PendingIntent.FLAG_UPDATE_CURRENT);
mBuilder.addAction(R.drawable.calendar_question, "Partly", pendingIntentMaybe);

//No intent
Intent noReceive = new Intent();
noReceive.setAction(NO_ACTION);
PendingIntent pendingIntentNo = PendingIntent.getBroadcast(this, 12345, noReceive, PendingIntent.FLAG_UPDATE_CURRENT);
mBuilder.addAction(R.drawable.calendar_x, "No", pendingIntentNo);

Your receiver:

@Override
public void onReceive(Context context, Intent intent) {
String action = intent.getAction();

if(YES_ACTION.equals(action)) {
Log.v("shuffTest","Pressed YES");
} else if(MAYBE_ACTION.equals(action)) {
Log.v("shuffTest","Pressed NO");
} else if(NO_ACTION.equals(action)) {
Log.v("shuffTest","Pressed MAYBE");
}
}

Android - How to create a Notification with Action

Use the NotificationCompat instead of Notification like this:

Notification.Action action = new NotificationCompat.Action(icon, title, pendingIntent);
Notification notification = new NotificationCompat.Builder(context)
.addAction(action)
.build();

Android Notification not closing on AddAction click

simply add builder.autoCancel(true);

This will solve your problem.

Receive extra from a notification action button

You are attempting to create two different PendingIntents, but your code actually only creates one PendingIntent. The first call creates a new PendingIntent:

Intent contentIntent = new Intent(context, MainActivity.class);
PendingIntent contentPendingIntent = PendingIntent.getActivity(context, App.REMINDERS_ID, contentIntent, PendingIntent.FLAG_MUTABLE);

The second call does not create a new PendingIntent. Instead, because the contents of contentIntent match the contents of extendIntent, the call to PendingIntent.getActivity() actually returns the PendingIntent previously created in the first call:

Intent extendIntent = new Intent(context, MainActivity.class);
extendIntent.putExtra(App.BUNDLE_ACTION, App.ACTION_EXTEND_WEAR);
PendingIntent extendPendingIntent = PendingIntent.getActivity(context, App.REMINDERS_ID, extendIntent, PendingIntent.FLAG_MUTABLE);

This is because when comparing the two Intents, the "extras" in the Intents are ignored.

To fix this you need to make sure that the PendingIntents are unique. There are many ways to do this, here are a few:

  • Add an ACTION to each Intent and make sure that they are different
  • Use different requestCode in each call to PendingIntent.getActivity() (instead of using App.REMINDERS_ID for both

how can i add action event on notification action

you should use IntentService in AlarmNotificationReceiver class and handle mMediaPlayer object for stop , pause , play or anything else that you want to do ,the same as following code :

  public static class NotificationActionService extends IntentService {
public NotificationActionService() {
super(NotificationActionService.class.getSimpleName());
}

@Override
protected void onHandleIntent(Intent intent) {
String action = intent.getAction();

if ("StopSound".equals(action)) {
// TODO: handle action StopSound.
mMediaPlayer.stop();
// If you want to cancel the notification:
NotificationManagerCompat.from(this).cancel(NOTIFICATION_ID);
// NOTIFICATION_ID : you can (set and get) notificationid (to/from) intent
}
}

in onReceive method of AlarmNotificationReceiver you should set intent and call NotificationActionService

 Intent stopSoundIntent = new Intent(context, 
NotificationActionService.class)
.setAction("StopSound");

PendingIntent stopSoundPendingIntent = PendingIntent.getService(context, 0,
stopSoundIntent, PendingIntent.FLAG_ONE_SHOT);

NotificationCompat.Builder notificationBuilder =
new NotificationCompat.Builder(context)
.setSmallIcon(R.drawable.ic_launcher)
.setContentTitle("Sample Notification")
.setContentText("Notification text goes here")
.addAction(new NotificationCompat.Action(R.drawable.ic_launcher,
"StopSound", stopSoundPendingIntent));

NotificationManagerCompat notificationManager = NotificationManagerCompat.from(context);
notificationManager.notify(NOTIFICATION_ID, notificationBuilder.build());

you can edit this solution according to what you need.

i hope this solution be benefit for you.

for more detail you can see this link



Related Topics



Leave a reply



Submit