I have a background Service that triggers events and builds notifications for my app. If clicked, a notification should open the MainActivity, and depending on the info sent, open one of my Fragments within this Activity. For that, the Notification contains a PendingIntent that saves data into the Intent's Bundle.
There are three scenarios for this triggered event:
- App in foreground: my service sends a
Broadcastand myBroadcastReceiverin theActivitygets the event and handles it. NoNotificationneeded. It works well. - App killed: the
PendindIntentreopens my app, and myActivityaccesses my info through theBundleusinggetIntent().getExtras(). Everything works well. - App in background: the Activity was created already, so the BroadcastReceiver is registered. I click on my Notification, but nothing happens. Neither I receive a broadcast message, nor I can access my
Bundle(checking it in onResume withgetIntent().getExtras()and it's null).
Any thoughts on how to solve the third case?
Code that creates the Notification:
private void createNotification() {
NotificationCompat.Builder mBuilder = new NotificationCompat.Builder(this);
mBuilder.setSmallIcon(R.drawable.my_icon);
mBuilder.setContentTitle("My App");
mBuilder.setContentText("Notification message");
Bundle bundle = new Bundle();
bundle.putString(MainActivity.OPEN_FRAGMENT, "myFragment");
Intent resultIntent = new Intent(this, MainActivity.class);
resultIntent.putExtra("myInfo","myInfo");
resultIntent.addFlags(Intent.FLAG_ACTIVITY_REORDER_TO_FRONT);
resultIntent.putExtras(bundle);
PendingIntent resultPendingIntent = PendingIntent.getActivity(this, 0, resultIntent, PendingIntent.FLAG_UPDATE_CURRENT);
mBuilder.setContentIntent(resultPendingIntent);
mBuilder.setAutoCancel(true);
NotificationManager mNotificationManager = (NotificationManager) getSystemService(Context.NOTIFICATION_SERVICE);
mNotificationManager.notify(0, mBuilder.build());
}