I have the following code, I want retrieve some data when user taps a notification. But I don't know how to deal with it. I know there is a way to pass the activity as an argument but depends of the content of the notification I should open different activitys.
This is the desire workflow
I publish a notification(with a key-value data) -> User taps notification -> BroadcastReceiver(retrieve key-value data) -> Activity
The problem is my BroadcastReceiver is not receiving data.
Intent notificationIntent = new Intent(NOTIFICATION_SELECTED_ACTION);
notificationIntent.putExtra("mykey","myvalue");
PendingIntent intent = PendingIntent.getBroadcast(context, 0, notificationIntent, 0);
context.getApplicationContext().registerReceiver(receiverOpen, new IntentFilter(NOTIFICATION_SELECTED_ACTION));
NotificationCompat.Builder mBuilder = new NotificationCompat.Builder(context)
...
mBuilder.setContentIntent(intent);
//I publish the notification in the notification bar
notification = mBuilder.build();
mNotificationManager.notify(id_push, notification);
private final BroadcastReceiver receiverOpen = new BroadcastReceiver() {
@Override
public void onReceive(Context context, Intent intent) {
Intent i = new Intent(context.getApplicationContext(),MainActivity.class);
i.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
//Value is always null ffs
String value = intent.getStringExtra("mykey");
count = 0; // Do what you want here
notificationList.clear();
editor.clear();
editor.commit();
context.startActivity(i);
}
};