Issue
My app is pushing a notification at a specific date in the future. In the notification you shall have two options:
- Click the notification body --> open the app normally
- Click the action button in the notification --> open the app and perform an action
In order to do so, I want to add an extra to the intent which can be read when starting the app. Therefore I set up the notification receiver as following:
public class NotificationReceiver extends BroadcastReceiver {
@Override
public void onReceive(Context context, Intent intent) {
Intent contentIntent = new Intent(context, MainActivity.class);
PendingIntent contentPendingIntent = PendingIntent.getActivity(context, App.REMINDERS_ID, contentIntent, PendingIntent.FLAG_MUTABLE);
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);
NotificationCompat.Builder builder = new NotificationCompat.Builder(context, CHANNEL_REMINDERS_ID)
.setSmallIcon(R.drawable.ic_baseline_calendar_today_24)
.setContentTitle(intent.getStringExtra("title"))
.setContentText(intent.getStringExtra("text"))
.setContentIntent(contentPendingIntent)
.addAction(0,intent.getStringExtra("action_extend"), extendPendingIntent)
.setPriority(NotificationCompat.PRIORITY_DEFAULT);
// .addAction(0,intent.getStringExtra("action_stop"), contentIntent)
NotificationManagerCompat notificationManager = NotificationManagerCompat.from(context);
int id = intent.getIntExtra("id", 1);
notificationManager.notify(id, builder.build());
}
No, when the app starts, I want to check if the intent has an extra (BUNDLE_ACTION) or not.
Bundle bundle = this.getActivity().getIntent().getExtras();
if (bundle != null && bundle.containsKey(App.BUNDLE_ACTION)) {
// Perform the action on app start
Log.e(App.TAG, "Action received from notification! Action = " + bundle.getInt(App.BUNDLE_ACTION));
} else {
Log.e(App.TAG, "No action received from notification.");
}
The extra is received, the Log entry is shown accordingly. But, the extra is received either I press the notification body or the notification action button.
Can anybody advise me what I am doing wrong?
Thanks!
Solution
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
Intentand make sure that they are different - Use different
requestCodein each call toPendingIntent.getActivity()(instead of usingApp.REMINDERS_IDfor both
Answered By - David Wasser
0 comments:
Post a Comment
Note: Only a member of this blog may post a comment.