Solution 1 :
If you are using Firebase Cloud Messaging to send notifications to your application, then it means you are able to create your own notification. Then all you have to do is set the proper FLAG
for your intent
:
public class MyFirebaseMessagingService extends FirebaseMessagingService {
@Override
public void onMessageReceived(RemoteMessage remoteMessage) {
try {
Intent notificationIntent = new Intent(context, MainActivity.class);
//Intent.FLAG_ACTIVITY_NEW_TASK Launches a new instances of your Activity.
//So if there is already one in background, there will be two instances.
//Intent.FLAG_ACTIVITY_CLEAR_TOP is what you want, it resumes the last
//session if there is any, or launches a new instance if there is no instance in background
notificationIntent.setFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP | Intent.FLAG_ACTIVITY_SINGLE_TOP);
PendingIntent pendingIntent = PendingIntent.getActivity(context, requestID, notificationIntent, PendingIntent.FLAG_UPDATE_CURRENT);
//do the rest
} catch (Exception e) {
e.printStackTrace();
return null;
}
}
Solution 2 :
I solved this problem by changing launch mode of activity, so, in AndroidManifest, I set launchMode = singleTask
for MainActivity.
Problem :
I’m using FCM to send notifications to my app, but there is a problem. if my user is using the app and just press the home button( not back key) to exit the app and got a notification, then press on the notification, my app will reopen. if the user presses the back button, he/she exits from the reopened app and he/she will be directed to the last state of the app just before pressing the home button.(in the other word, the user should presses back button twice, once for exiting from reopened the app and another for exiting from the app in the background)
how can I load the last state of the app if it is in the background and reopen the app if it’s not, by taping on the notification?
EDIT :
I changed “onMessageReceived” to code bellow but nothing changed:
class MyFireBase() : FirebaseMessagingService(){
override fun onNewToken(p0: String) {
super.onNewToken(p0)
}
override fun onMessageReceived(p0: RemoteMessage) {
try {
val intent = Intent()
intent.action = "ACTION_STRING_ACTIVITY"
intent.putExtra("category", p0.data["category"])
sendBroadcast(intent)
val notificationIntent = Intent(baseContext, MainActivity::class.java)
notificationIntent.flags = Intent.FLAG_ACTIVITY_CLEAR_TOP
PendingIntent.getActivity(
baseContext,
1,
notificationIntent,
PendingIntent.FLAG_UPDATE_CURRENT
)
super.onMessageReceived(p0)
} catch (e: Exception) {
e.printStackTrace()
}
}
}
Comments
Comment posted by sajjad
Share your
Comment posted by mohammad
I have not such that [email protected]
Comment posted by sajjad
I think you can’t change this behavior because apparently you are using Firebase In-App messaging which automatically creates and handles notifications. To be able to change this behavior, you need to use Firebase cloud messaging which in turn needs a server to send notifications through Firebase.
Comment posted by mohammad
I am using FCM and getting notifications through my own server, now, how can I handle [email protected]
Comment posted by mohammad
I used these codes but nothing changed. plz check my edit.
Comment posted by sajjad
Use both