1

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:

  1. App in foreground: my service sends a Broadcast and my BroadcastReceiver in the Activity gets the event and handles it. No Notification needed. It works well.
  2. App killed: the PendindIntent reopens my app, and my Activity accesses my info through the Bundle using getIntent().getExtras(). Everything works well.
  3. 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 with getIntent().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());
}
Ziem
  • 6,579
  • 8
  • 53
  • 86
Teo Inke
  • 5,928
  • 4
  • 38
  • 37

2 Answers2

0

Have you tried to change

PendingIntent resultPendingIntent = PendingIntent.getActivity(this, 0, resultIntent, PendingIntent.FLAG_UPDATE_CURRENT);

to

PendingIntent resultPendingIntent = PendingIntent.getActivity(this, 0, resultIntent, 0);
Ziem
  • 6,579
  • 8
  • 53
  • 86
  • I had this code a while ago, then changed it to what I have now. In practice it doesn't make any difference. What I noticed is that once I open my (killed) app from the notification, it has the info in the Bundle. If I send it to background and bring to foreground, the info is still there. Even if I read it and do Bundle.clear, next time I bring to foreground it'll have the info in Bundle. – Teo Inke Apr 24 '15 at 19:50
  • I could, but actually there isn't anything related to my BroadcastReceiver (I'm creating and registering dynamically). According to this post: http://stackoverflow.com/questions/23154922/why-broadcastreceiver-is-not-running-in-the-background I may have to register it as a separate class and declare it in the Manifest. – Teo Inke Apr 24 '15 at 20:12
  • Okey, but have you registered your activity as a singleInstance in your manifest? That might cause the problem aswell. – Ashkan Shams Apr 24 '15 at 20:38
  • It was set to singleTop. I changed it but got the sam results. I actually found out that getIntent().getExtras().clear() doesn't work, so I did getIntent.removeExtra(). Now I'm sure it only receives the Bundle from the Notification when it's killed. Pretty weird. – Teo Inke Apr 24 '15 at 21:04
0

Finally, I found the solution!

What happened is that on my cases:

  1. App foreground: Service sends a Broadcast. Activity receives it and handles the event.
  2. App killed: Notification clicked, PendingIntent launched. App reopens and handles the event with the info from Bundle.
  3. App in background: Notification clicked, it sends a PendingIntent, however the Activity is already running so it doesn't override it, which is why nothing happened.

Solution: override the onNewIntent method:

@Override
protected void onNewIntent(Intent intent) { // Receives the PendingIntent
    super.onNewIntent(intent);
    if (intent.getExtras() != null && intent.getExtras().getString(OPEN_MY_FRAGMENT) != null) {
        // TODO something with intent.getExtras().getString("myInfo");
    }
}
Teo Inke
  • 5,928
  • 4
  • 38
  • 37