Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

onMessageRecieved not working when app is killed. #368

Closed
DanikKamilov opened this issue Nov 4, 2017 · 137 comments
Closed

onMessageRecieved not working when app is killed. #368

DanikKamilov opened this issue Nov 4, 2017 · 137 comments
Assignees

Comments

@DanikKamilov
Copy link

onMessageRecieved not working when app is killed. It works perfect in foreground and in background and I can receive Extras in MainActivity. But when app is killed I receive notification and after click on it- Extras == null.

I send only DATA (to, data) notification. What I do wrong?

`
@OverRide
public void onMessageReceived(RemoteMessage remoteMessage) {

    Map<String, String> map = remoteMessage.getData();
    String message="";
    for (Map.Entry<String, String> entry : map.entrySet()) {
        message= entry.getValue();
    }
    sendNotification(message);

}

private void sendNotification(String messageBody) {
 Intent intent = new Intent(this, MainActivity.class);
intent.putExtra("notification",messageBody);
intent.addFlags(Intent.FLAG_ACTIVITY_SINGLE_TOP);
PendingIntent pendingIntent = PendingIntent.getActivity(this, 0 /* Request code */, intent,
        PendingIntent.FLAG_ONE_SHOT);

String channelId = getString(R.string.default_notification_channel_name);
Uri defaultSoundUri= RingtoneManager.getDefaultUri(RingtoneManager.TYPE_NOTIFICATION);
NotificationCompat.Builder notificationBuilder =
        new NotificationCompat.Builder(this)
                .setSmallIcon(R.drawable.ic_notifications_active_black_24dp)
                .setContentTitle("MyTitle")
                .setContentText(messageBody)
                .setAutoCancel(true)
                .setSound(defaultSoundUri)
                .setContentIntent(pendingIntent);

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

notificationManager.notify(0 /* ID of notification */, notificationBuilder.build());

}
`

and I've tried to use different flags in intent and pending intent and it not works... (I can't catch intent.putExtra("notification",messageBody); in MainActivity when app is closed.)

@DanikKamilov
Copy link
Author

Now (for APP IS CLOSED case) I write Notification text to file and read this text if extras == null and notificationText.txt is exists... its stupid solution but it works. How can I catch this extras when app is closed in other way?((

@carlosalexandresmo
Copy link

carlosalexandresmo commented Nov 7, 2017

Hello guys
I solved this inserting this lines in Manifest.xml

<uses-permission android:name="com.google.android.c2dm.permission.RECEIVE" />
 <uses-permission android:name="android.permission.WAKE_LOCK" />

My app received notifications when is foregorund, background and is closed.

@kroikie kroikie self-assigned this Nov 10, 2017
@kroikie
Copy link
Contributor

kroikie commented Nov 10, 2017

Hi @DanikKamilov and @carlosalexandresmo FCM does not process messages if an app is "killed" or force stopped. When a user kills an app it is an indication that the user does not want the app running so that app should not run till the user explicitly starts it again.

Note that swiping an app from recents list should NOT "kill" or force stop it. If messages are not being received after swiping from recents list then please identify these devices and we will work with the manufactures to correct this behaviour.

Only being able to handle messages when the app is in the foreground or background is working as intended.

@opus1269
Copy link

@kroikie Are you guys keeping a running list of devices with this problem, and if so can you make it public?

@Adityavns
Copy link

Hi @kroikie on messageReceived is not getting called for the below FCM when the app has been removed from recents by swiping on devices manufactured by VIVO, ONE PLUS.
{
"to":"erWZDlJg9Fo:APA91bFUe_fc6X1kzg7bmZTool7dpBp41AcSPPBEpQVPUihzYR9Q1uBVlUcCkmqj5bs4ObgcgfPjuGCDIiU22DMUxVSArj0aD91WBFMs591To9ge0oIKbCvSuii9WoPFCk2fhC8KeGSD","priority":"high","data":{"message":"Some Message"}
}
Any help is appreciated. Thanks in advance.

PLease

@rajeshjakhar1092
Copy link

@Adityavns try adding these two permissions also in manifest

"uses-permission android:name="com.google.android.c2dm.permission.RECEIVE"
"uses-permission android:name="android.permission.WAKE_LOCK"

I also have oneplus 3 mobile and i was having same issues ,but after adding the permissions my issue got resolved.

@Adityavns
Copy link

Adityavns commented Dec 6, 2017 via email

@rajeshjakhar1092
Copy link

@Adityavns yeah you are right ,actually i am now again facing the same issue, I still not able to figure out what is the main issue, actually I have a app in which i have implemented the same code and there I am getting notifications all the time even app is killed... but in another app it is not working.. I will let you know if I get a solution :)

Regards
Rajesh Jakhar

@rajeshjakhar1092
Copy link

@Adityavns please let me know also if you find any solution.

Regards,
Rajesh Jakhar

@rajeshjakhar1092
Copy link

@Adityavns try this one, go to settings in battery>> battery optimization>>select your app>>you will see that your app is optimised>>> click on dont optimise>> then try pushing notificaiton.... hope this helps.
PS:- I then cliked on optimise app.. now I am getting notifications

Please let me know if this works...

Regards
Rajesh Jakhar

@rajeshjakhar1092
Copy link

@Adityavns It is because of DOZE mode and battery optimisation,you just have to turn off battery optimisation for all apps or particular app.
Enjoy :) :)

Regards
Rajesh Jakhar

@samtstern
Copy link
Contributor

As @kroikie said this is intended behavior, although we are still interested to know when developers run into it. If you find devices that you think behave strangely please continue to tell us.

I am going to close this issue as there's nothing we can do to "fix" it besides continuing to discuss new places you find it,

@skfaisal93
Copy link

List of devices: OnePlus, Oppo, Lenovo

@kimnamcham
Copy link

Same issues with asus a007 zenphone!!

@rajeshjakhar1092
Copy link

rajeshjakhar1092 commented Mar 23, 2018

@kimnamcham It is because of DOZE mode and battery optimization,you just have to turn off battery optimization for all apps or particular app. Go to Settings>> apps >> select your app>> battery>>
battery optimization> select your app>> select don't optimise.
Problem solved.

I have OnePlus 3 and I had the same issue . I did this and my problem was solved. battery optimization blocks incoming notifications from apps so that the wont kill battery.

Thanks Regards

@skfaisal93
Copy link

@rajeshjakhar1092 We all know this. You should read the whole thread.

@aldo-abvr
Copy link

@kimnamcham This is not solution. There are apps like Whatsapp, iMobile, Facebook,etc which is set to "optimise" in Battery Optimisation Setting but still it gets notification without missing any.

@samtstern @kroikie It can't be intended behaviour if other apps in background still gets notifications. Something bug with firebase push notification sdk.

I tested with firebase 4.5 package using unity. Not working on Oneplus 5.

@saprahits
Copy link

how to handle notification when app in kill in firebase android

@manishbutola
Copy link

is this issue resolved?

@aldo-abvr
Copy link

No. Google developers closing issue without solution on there own OS.

@manishbutola
Copy link

i am getting this issue on staging app on live it is working fine. might be my app is white listed when it goes live.

@kchandawat
Copy link

even though opmization is enabled apps like whatsapp recieve message when in background and after being swiped out. Any solution for an app targeting oreo ?

@kchandawat
Copy link

Solution plz sir?.....

@rameshvoltella
Copy link

Hi @DanikKamilov and @carlosalexandresmo FCM does not process messages if an app is "killed" or force stopped. When a user kills an app it is an indication that the user does not want the app running so that app should not run till the user explicitly starts it again.

Note that swiping an app from recents list should NOT "kill" or force stop it. If messages are not being received after swiping from recents list then please identify these devices and we will work with the manufactures to correct this behaviour.

Only being able to handle messages when the app is in the foreground or background is working as intended.
@kroikie
if not then Why is it used for push notification? because push notification we need to notify user something,even if they are inside or outside app.How can we make onplus and other custom rom device to show the notification,its a major issue guys

@kroikie
Copy link
Contributor

kroikie commented Sep 25, 2018

@rameshvoltella outside has 2 meanings.

  1. The user leaves the app but it is still running in the background.
  2. The user explicitly stops the app via Managed Applications in settings. Or on some devices swiping the app from the recents menu.

In 1. case we deliver the notification.
In 2. case we don't (should not).

From Android docs:

Note that the system adds FLAG_EXCLUDE_STOPPED_PACKAGES to all broadcast intents. It does this to prevent broadcasts from background services from inadvertently or unnecessarily launching components of stoppped applications. A background service or application can override this behavior by adding the FLAG_INCLUDE_STOPPED_PACKAGES flag to broadcast intents that should be allowed to activate stopped applications.

Applications are in a stopped state when they are first installed but are not yet launched and when they are manually stopped by the user (in Manage Applications).

We do not add the FLAG_INCLUDE_STOPPED_PACKAGES flag since we are not sure why the app has been stopped. Thus we don't deliver messages to stopped applications.

@iwwBittu
Copy link

I getting notification in background , open, resume mode in all devices very well.
In Lolipop :- also getting in after killed (swipe) mode but greater than lolipop i am not getting any notification after swipe.

Please suggest and coordinate me ,
Thaks in advance for your great knowledge.

@kroikie
Copy link
Contributor

kroikie commented Oct 12, 2018

@iwwBittu there was no change after lollipop on how messages are handled after swiping away an app from the recents menu. Same code should work.

Could you file a ticket with our support team since this may be a device specific issue.

@mrazam110
Copy link

Any solutions for infinix or different chinese devices?

@develbass
Copy link

I have the same problem, and in 2020 there is still no solution for that ?? a shame for google.

@sandeep5193
Copy link

sandeep5193 commented Jul 14, 2020

as mentioned by @kroikie many times here, vendors have not implemented swipe-up from recents correctly. I have seen this issue in Lenovo Vibe K5 Plus, few apps are whitelisted by vendor. There is restrict-to-launch checkbox in app management. for well-known apps it is unticked. If I download whatsapp, it is unticked by default. for our app it is ticked, so FCM is not getting delivered after swipe-up. If I untick it, FCM works, but who will educate this to users :)

@bukovskiy7777
Copy link

Hi, when I call ##426## on my phone, I see the screen with logs of notifications. In this screen shown that firebase delivered notifications, but android can't broadcast stopped app. So I think this is not a problem of firebase. Maybe it is helps someone to understand how to solve this issue.

@abhishekBansal
Copy link

abhishekBansal commented Aug 13, 2020

Can confirm that this issue is reproducing for me on Google Pixel3 running Android 10 for even Data only Max Priority FCM messages. App does not receive/show notification when killed from recent panel. However, when app is resumed again all pending notifications are delivered.
This happens even when phone is awake and not sleeping. This is not ideal for me as I am trying to develop a calling app.

Update
Its working as expected in release build.. weird stuff!!

@austyking
Copy link

austyking commented Aug 13, 2020

Has this issue been fixed after 3 years? Or any work around?

@KORuL
Copy link

KORuL commented Aug 13, 2020

Not fixed

@satyajiit
Copy link

Tested on a production app, over 100+ devices. Just don't Pass the "Notification" object .
Issue arises when Passed the "Notification" object.
( I know this is strange 😷)

What I am doing?
Sending notification to devices group/id , on new document (creation) trigger of Firestore.

check this cloud function
https://github.com/satyajiit/PWA_TO_NATIVE_ANDROID_APP/blob/master/Cloud_Functions_For_FCM/functions/index.js

FirebaseService

https://github.com/satyajiit/PWA_TO_NATIVE_ANDROID_APP/blob/master/app/src/main/java/com/argonlabs/satyajit/FirebaseService.java

in manifest

<service
            android:name=".FirebaseService"
            android:exported="false">
            <intent-filter>
                <action android:name="com.google.firebase.MESSAGING_EVENT" />
            </intent-filter>
        </service>

Don't forget to subscribe topic "all" , for the above code snip.

This is what working for me.
I was having the same issue earlier.

@abhishekBansal
Copy link

@satyajiit my message is data only already. Strangely! It works in release builds on Pixel 3

@Waleedasim
Copy link

same issue, any solution for infinix?

@Waleedasim
Copy link

Any solutions for infinix or different chinese devices?

same issue did you find any solution?

@Waleedasim
Copy link

finally issue resolved just follow below code it is working fine on oneplus huawei and infinix

 ComponentName componentName = new ComponentName(this, FCMService.class);
getPackageManager().setComponentEnabledSetting(componentName, PackageManager.COMPONENT_ENABLED_STATE_ENABLED,
                PackageManager.DONT_KILL_APP);

@Waleedasim
Copy link

finally issue resolved just follow below code it is working fine on oneplus huawei and infinix

 ComponentName componentName = new ComponentName(this, FCMService.class);
getPackageManager().setComponentEnabledSetting(componentName, PackageManager.COMPONENT_ENABLED_STATE_ENABLED,
                PackageManager.DONT_KILL_APP);

Kotlin version with broadcast would be:

val receiver = ComponentName(applicationContext, BootCompleteReceiver::class.java)

        applicationContext.packageManager?.setComponentEnabledSetting(
            receiver,
            PackageManager.COMPONENT_ENABLED_STATE_ENABLED,
            PackageManager.DONT_KILL_APP
        )

@AllwinJohnson
Copy link

Sometimes onMessage not working when the app in background ( removed app from the recent apps)
Android 9 Lenovo PB-6505M
while the app is in the foreground, OnMessage is triggering when I send another notification, and also the pending notifications are coming.

@Cyhermes
Copy link

Cyhermes commented Nov 6, 2020

finally issue resolved just follow below code it is working fine on oneplus huawei and infinix

 ComponentName componentName = new ComponentName(this, FCMService.class);
getPackageManager().setComponentEnabledSetting(componentName, PackageManager.COMPONENT_ENABLED_STATE_ENABLED,
                PackageManager.DONT_KILL_APP);

Kotlin version with broadcast would be:

val receiver = ComponentName(applicationContext, BootCompleteReceiver::class.java)

        applicationContext.packageManager?.setComponentEnabledSetting(
            receiver,
            PackageManager.COMPONENT_ENABLED_STATE_ENABLED,
            PackageManager.DONT_KILL_APP
        )

I tried adapting this code for Xamarin, but was unable to get it to work, still not receiving the push notification when app is closed on Infinix. @Waleedasim if you don't mind, can you give any other pointers that might be helpful. Thanks

@shahzadafridi
Copy link

If someone is looking for a detailed explanation please visit: https://github.com/shahzadafridi/FCM-Notification-Test-Cases I am sure this will helps you.

@viveksomaiya1103
Copy link

Anyone with any working solution

@KORuL
Copy link

KORuL commented Nov 23, 2020

@google decided to do nothing, it left us with this problem alone

@anurag-176
Copy link

Lets get one thing cleared!
What ever the documentation says, what I have experienced is that one doesn't need the "onMessageReceived" to simply open app via notification or a specific part of it, such things can be handled by TAGS in notification's payload and and Intent receiver/reader in the startup activity.
If you have noticed, Push notifications do not pop up when app is in foreground (while user is using the app)
and "onMessageReceived" works exactly at that time.

@anurag-176
Copy link

anurag-176 commented Nov 29, 2020

Also, did you guys check this out? https://firebase.google.com/docs/cloud-messaging/android/receive
Check this topic there "Background Restricted Apps (Android P or newer)"

@ManjKS
Copy link

ManjKS commented Dec 30, 2020

App state | Notification | Data | Both
Foreground | onMessageReceived | onMessageReceived | onMessageReceived
Background | System tray | onMessageReceived | Notification: system trayData: in extras of the intent.
When app is in background onMessageReceived() is not called. This is how the behavior is.
When app is in background,notification will be handled by system tray and data will be handled by extras in intent in main launcher.
Hope this helps.

@ibeshkar
Copy link

Hi there,
this problem happens because of the "notification" part of remoteMessage. so if you wanna get a message when the app is in the foreground or killed, you should remove the "notification" from server-side notif.

@xayappz
Copy link

xayappz commented Jan 26, 2021

I have one App which is on Play Store it receives the notification but the App which is in development process not getting I think after publishing it on the store it will also work.

@sorosons
Copy link

For me in Xcode ı Fix it ruunner >Sign &Capabitlies cheack All - Debug-Release-Profile to be same bundle id and for all accept push notificaiton this was my problem and take my 5 days

@MuruganandhamE
Copy link

MuruganandhamE commented Jul 3, 2021

why this issue is closed , any solution for this issue?

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

No branches or pull requests