0

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?

Ramesh sambu
  • 3,431
  • 2
  • 23
  • 38
P. Ilyin
  • 751
  • 9
  • 26

3 Answers3

1

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.

ScottyB
  • 2,038
  • 25
  • 39
0

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());
Kapil Parmar
  • 816
  • 7
  • 18
  • How to know what activity I should place instead of ActivityN? There can be any activity of application at last position of activity-stack – P. Ilyin Dec 13 '17 at 11:36
  • Create a static gloabal variable in Gloabal Activity Like AactivityName =this and wehenver You go to new Activity change it like activityName =this; and check name in firebase service for as GloabalActivity.ActivityName; – Kapil Parmar Dec 13 '17 at 11:47
0

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.

ADM
  • 18,477
  • 11
  • 47
  • 78
  • In your answer you declared YourActivity, but what activity? When app go to foreground, here may be any activity in stack last. Also, Intent.FLAG_ACTIVITY_CLEAR_TOP will remove all other activities in my stack. Activity stack shoudn't be changed, only dialog must be displayed. – P. Ilyin Dec 13 '17 at 11:35
  • Whch activity you want to open on Notification click . – ADM Dec 13 '17 at 12:20
  • I want to open the last activity in my activity-stack on moment when app turned to background. It's may be Activity1, Activity2,... ActivityN, I don't know what activity it will be – P. Ilyin Dec 13 '17 at 12:23
  • Suppose Activity2 was in forground and you make notification to Activity2. Now user click back and goes back to Activity1. Now user click on notification but its was intented for Activity2 so it will open Activity2 . Is that Ok ? Or you want to perform action in Activity1 in this scenario ? – ADM Dec 13 '17 at 12:26
  • See my updated answer its something i can suggest. Thx – ADM Dec 13 '17 at 12:43