0

Currently, I'm using firebase push notification for notification. I have implemented the onMessageReceived() method using FirebaseMessagingService. When in foreground state, the onMessageReceived() method for notifications is triggered, but when the app is in the background state, onMessageReceived() is not called.

I'm not getting any type of data that I might have set on the server. I have done some R&D, but all the solutions show some handleIntent() for the specific library or some solution for using getIntent() and I'm using the Firebase library version above 17.3.4.

Could anyone show me the right way to use Firebase notification when the app is in background state and killed state?

Juan José Melero Gómez
  • 2,742
  • 2
  • 19
  • 36
  • Its easy. Your intent is sent to the main activity. Check if the intent is null or not and cycle through the keys. `String your_value; Object value;` `if (getIntent().getExtras() != null) { for (String key : getIntent().getExtras().keySet()) { value = getIntent().getExtras().get(key); if(key.equals("your_key")){ your_value = String.valueOf(value) }` – Pemba Tamang Nov 28 '18 at 09:35
  • @Pemba I'll send an intent to main Activity but in the background state its not load data. I'll check both the state data is loaded when the app is foreground state. Not any single key found which I'll set on the server. –  Nov 28 '18 at 09:45
  • so you want to load data when the notification arrives?? – Pemba Tamang Nov 28 '18 at 09:48
  • @Pemba yes I want notification data on my starting activity –  Nov 28 '18 at 09:50
  • So you want to tap on the notification then open the app then process your data right? This will work. – Pemba Tamang Nov 28 '18 at 09:52
  • @Pemba I'll do the same thing but the app is open but data is not loaded. Because of no data to getIntent. –  Nov 28 '18 at 09:55
  • wait I'll post a proper answer below – Pemba Tamang Nov 28 '18 at 09:57
  • @Pemba Single In foreground state i got the data otherwise not ? –  Nov 28 '18 at 09:57

1 Answers1

0

In the apps foreground state the onMessageReceived is called but in the background state it will not be called. Therefore in your main activity put loop through the extras in the intent.

 Object value;
        String str;
        if (getIntent().getExtras() != null) {
            for (String key : getIntent().getExtras().keySet()) {
                value = getIntent().getExtras().get(key);

                if(key.equals("key")){
                    str = String.valueOf(value);

                }
            }

        }

The "key" is the one you sent from your console. enter image description here

In my case the str will be equal to "test notification"

Pemba Tamang
  • 1,235
  • 16
  • 38
  • okay, I'll do the same thing but in the case of server-side language send me a push notification on my app. And I'm not getting any value in the background state –  Nov 28 '18 at 10:17
  • This is way to do it. read the accepted answer here https://stackoverflow.com/questions/37358462/firebase-onmessagereceived-not-called-when-app-in-background – Pemba Tamang Nov 28 '18 at 10:22
  • and if you can help me here https://stackoverflow.com/questions/53393180/get-real-time-internet-speed-in-android/53516094?noredirect=1#comment93902905_53516094 – Pemba Tamang Nov 28 '18 at 10:39
  • I'll Try all the conditions as well as both end but I'm not getting any data on server-side on background side.but in firebase console I'll send notification and I'll get data. please let me know how to do. If someone here done it, please share your code. –  Nov 28 '18 at 11:51
  • The code is up there.. that's how you do it. Keep trying – Pemba Tamang Nov 28 '18 at 11:56
  • let me know one thing I've to receive a notification with data message in the background and when I'll use get intent() than not get the data. Any other method for receiving notification data.? –  Nov 28 '18 at 12:06
  • let me know one thing for in the background side any service to handle the notification.? –  Nov 29 '18 at 05:42
  • no there is not. If you take time and read the answer in the link by @János This is working as intended, notification messages are delivered to your onMessageReceived callback only when your app is in the foreground. If your app is in the background or closed then a notification message is shown in the notification center, and **any data from that message is passed to the intent** . So thats it. All data is passed on the intent of the activity launched by it. – Pemba Tamang Dec 01 '18 at 04:18
  • see the link and read it again,use my code and accept and upvote my answer before this question is closed off as a duplicate. I needs reps for setting up a new bounty. – Pemba Tamang Dec 01 '18 at 04:21