0

I want to send a extra to an activity when user clicks on a notification. Extras is received correctly, but the activity is reset when user clicks on the notification. I want to keep current activity without relaunching it when it is already opened.

This is how I create the notification:

    Intent notifIntent = MyActivity.getIntent();
    notifIntent.setFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP);

    NotificationCompat.Builder(context)
      .setContentIntent(PendingIntent.getActivity(context, notificationId, notifIntent, PendingIntent.FLAG_UPDATE_CURRENT))

But MyActivity is always relaunched, despite it already running in foreground.

Anantha Raju C
  • 1,780
  • 12
  • 25
  • 35

1 Answers1

0

You could use package manager.

If your app is already running, it won't relaunch your app. But if you receive notification while app is in the background or closed it will open.

PackageManager packageManager = getPackageManager();
Intent notifIntent = packageManager.getLaunchIntentForPackage(com.example.testapp);

PendingIntent pendingIntent = PendingIntent.getActivity(context, notificationId, notifIntent, PendingIntent.FLAG_UPDATE_CURRENT);
NotificationCompat.Builder(context).setContentIntent(pendingIntent);

But if you are just sending data on notification recieve w/o starting any activity, maybe you can use local broadcast?

Skyler
  • 97
  • 3