Menu
Who Do Is
  • Home
  • What
  • How
  • Is
  • Can
  • Are
  • Does
  • Do
  • Why
  • Who
  • Where
  • Which
  • Which
  • Should
  • Will
  • When
  • What’s
  • Did
Who Do Is

[ANSWERED] android – how to reopen app just when it is not in background after tap on FCM notitiction

Posted on November 14, 2022

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?

READ  [ANSWERED] android - Delete path and its recursive child path in firebase cloud function
Powered by Inline Related Posts

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

Recent Posts

  • How can I play with my cat without toys?
  • What is a bag pipe band called?
  • Are Honda Civics actually fast?
  • Are Yankee candles toxic?
  • How do I pair my Michael Kors smartwatch with my Android?

Recent Comments

No comments to show.

Archives

  • January 2023
  • December 2022
  • November 2022
  • October 2022
  • September 2022

Categories

  • ¿Cómo
  • ¿Cuál
  • ¿Cuántas
  • ¿Cuánto
  • ¿Que
  • ¿Quién
  • 90” and 108” so you may have to round up to the nearest size.
  • and delete any Spotify folders from it. Once this is done
  • Android
  • Are
  • At
  • Bei
  • blink
  • C'est
  • Can
  • carbs
  • Comment
  • Did
  • Do
  • Does
  • During
  • For
  • Has
  • How
  • In
  • Is
  • Ist
  • Kann
  • Können
  • nouveau
  • On
  • or 108 inches.2020-08-03
  • Où
  • owning
  • Pourquoi
  • Puis-je
  • Quand
  • Quante
  • Quel
  • Quelle
  • Quelles
  • Quels
  • Qui
  • Should
  • Sind
  • Sollte
  • spiritual
  • tap the downward-facing arrow on the top left. A downward-facing arrow will appear underneath each song in the album; they'll turn green as the download completes.2020-07-28
  • Uncategorized
  • Wann
  • Warum
  • Was
  • Welche
  • Welcher
  • Welches
  • Welke
  • Wer
  • Were
  • What
  • What's
  • When
  • Where
  • Which
  • Who
  • Whose
  • Why
  • Wie
  • Will
  • Wo
  • Woher
  • you will receive two curtains each with the same measurements of width 66"" (168cm) x drop 54""(137cm).
  • you'll see a green downward-facing arrow next to each song.2021-02-26
©2023 Who Do Is | Powered by SuperbThemes & WordPress