resume current activity by notification

1k Views Asked by At

I use FCM (Firebase Cloud Messaging) in my app.

I have N activities in my app

During application's lifetime it may have any stack of activities:

Activity1 ->Activity2 -> Activity3-> ... -> ActivityN

I want to achieve behavior:

  1. Go to ActivityN
  2. Turn application to background
  3. Click on notification
  4. Turn application to foreground on ActivityN
  5. Show dialog on ActivityN

How to achieve it?

3

There are 3 best solutions below

2
On

in FirebaseMessagingService class>> onMessageRecived

Intent intent = new Intent(this, ActivityN.class);
        intent.addFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP);
        PendingIntent pendingIntent = PendingIntent.getActivity(this, 0, intent,
                FLAG_ONE_SHOT);


    NotificationCompat.Builder notificationBuilder = new NotificationCompat.Builder(this)
            .setSmallIcon(R.drawable._logo)
            .setContentText(notification)
            .setContentTitle(title)
            .setAutoCancel(true)
            .setSound(defaultSoundUri);
            .setContentIntent(pendingIntent);

    NotificationManager notificationManager =
            (NotificationManager) getSystemService(Context.NOTIFICATION_SERVICE);

    notificationManager.notify(0, notificationBuilder.build());
0
On

In your case where you are not sure to which Activity to be open on Notification click . Then you should broadcast on Notification click . Create a Broadcast Reaceiver.

   class NotificationClickReceiver extends BroadcastReceiver{

    @Override
    public void onReceive(Context context, Intent intent) {
        // here you will get Intent
        // Check which activity is currently open and pass the data to it
        // For passing data you can use a Another BroadcastReceiver or
    }
}

Entry in manifest

<receiver
        android:name=".NotificationClickReceiver"
        />

Then use pendingIntent to getBroadcast on notification click

Intent intent = new Intent(this, NotificationClickReceiver.class);
    intent.putExtra("key","val");
    PendingIntent pendingIntent = PendingIntent.getBroadcast(this, 3, intent,
            PendingIntent.FLAG_UPDATE_CURRENT | PendingIntent.FLAG_ONE_SHOT);
    NotificationCompat.Builder notificationBuilder = new NotificationCompat.Builder(this)
            .setSmallIcon(R.drawable._logo)
            .setContentText(notification)
            .setContentTitle(title)
            .setAutoCancel(true)
            .setSound(defaultSoundUri);
        .setContentIntent(pendingIntent);

    NotificationManager notificationManager =
            (NotificationManager) getSystemService(Context.NOTIFICATION_SERVICE);

    notificationManager.notify(0, notificationBuilder.build());

Now you will get intent in onReceive of NotificationClickReceiver then you can figure out to which activity to pass it on.

0
On

I had the same issue in that I wanted the app to just resume in its current state when the Firebase background notification arrived. Part of my solution from this answer.

In my case, I had up to four activities on the stack: A->B->C->D.

  1. In my app's manifest, I made activity D a singleTask activity (launchMode="singleTask") and put the intent-filter in that activity.

    <activity android:name=".activityD"> <intent-filter> <action android:name=".activityD" /> <category android:name="android.intent.category.DEFAULT" /> </intent-filter> </activity>

  2. I then put activity D as the "click_action" in the notification sent by Firebase Messaging.

  3. In my case, I didn't override the onNewIntent() method in activity D since the FirebaseMessageService's onMessageReceived method will get called when the app is brought to the foreground.
  4. Most important: Because activity D is a singleTask activity, it is only created if it's not already on the stack. Therefore, in activity D's onCreate() method, I check the contents of the Intent extras. If the extras didn't come from activity C, I pop D immediately (finish();return;) because I know it was created by the Firebase notification. That resumes the app's activity (A, B or C) when it went into the background. If the app's activity was D when it went into the background, onNewIntent() method will be called instead of onCreate() in which case I do nothing as described in step #3.

After thinking about it some more, a more general solution would be to create a "dummy" singleTask activity with the intent filter, then always pop it off the stack in the onCreate() method to resume the current activity when it was put in the background. Surely there's a better way... I just couldn't find it.