firebase / quickstart-android

Firebase Quickstart Samples for Android
https://firebase.google.com
Apache License 2.0
8.81k stars 7.32k forks source link

onMessageRecieved not working when app is killed. #368

Closed DanikKamilov closed 6 years ago

DanikKamilov commented 6 years ago

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 commented 6 years ago

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 commented 6 years ago

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 commented 6 years ago

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 commented 6 years ago

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

Adityavns commented 6 years ago

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 commented 6 years ago

@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 commented 6 years ago

Hi Raj,

Thanks for the reply! It is still not working in Vivo and one plus 5 phones that i have tested.

Regards, Aditya Vns

On Wed, Dec 6, 2017 at 5:06 PM, rajeshjakhar1092 notifications@github.com wrote:

@Adityavns https://github.com/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.

— You are receiving this because you were mentioned. Reply to this email directly, view it on GitHub https://github.com/firebase/quickstart-android/issues/368#issuecomment-349613753, or mute the thread https://github.com/notifications/unsubscribe-auth/AGEyDnSee13lT7lsO7A_fZJi429_m6a0ks5s9nxTgaJpZM4QSHEZ .

-- aditya369

rajeshjakhar1092 commented 6 years ago

@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 commented 6 years ago

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

Regards, Rajesh Jakhar

rajeshjakhar1092 commented 6 years ago

@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 commented 6 years ago

@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 commented 6 years ago

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 commented 6 years ago

List of devices: OnePlus, Oppo, Lenovo

kimnamcham commented 6 years ago

Same issues with asus a007 zenphone!!

rajeshjakhar1092 commented 6 years ago

@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 commented 6 years ago

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

aldo-abvr commented 6 years ago

@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 commented 6 years ago

how to handle notification when app in kill in firebase android

manishbutola commented 6 years ago

is this issue resolved?

aldo-abvr commented 6 years ago

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

manishbutola commented 6 years ago

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 commented 6 years ago

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 commented 5 years ago

Solution plz sir?.....

rameshvoltella commented 5 years ago

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 commented 5 years ago

@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 commented 5 years ago

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 commented 5 years ago

@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.

peterpeterpeter10101 commented 5 years ago

As programmer, i 'fell asleep' on issue. ' Lost' Difficult to recover. IAM Administrator

On Fri, Oct 12, 2018, 9:49 AM Arthur Thompson notifications@github.com wrote:

@iwwBittu https://github.com/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 https://firebase.google.com/support/ since this may be a device specific issue.

— You are receiving this because you are subscribed to this thread. Reply to this email directly, view it on GitHub https://github.com/firebase/quickstart-android/issues/368#issuecomment-429389729, or mute the thread https://github.com/notifications/unsubscribe-auth/Alj2rMAByl_fygHsD6Ozbee_4aknglHGks5ukMgWgaJpZM4QSHEZ .

vijayendra-tripathi commented 5 years ago

Ok, I did some test on Facebook app. I force stopped Facebook messenger app and sent a message to the messenger app from other device (using different FB account) and the messenger did display notification even though it was force stopped. However I noticed Facebook app was still running in the background, so I force stopped both apps (Facebook and Facebook messenger). I sent message again from the other device, but this time no notification was displayed. I believe this is a intentional behaviour. Force stop should completely stop apps and their services and no updates about the apps should be notified to user. I see one possibility though, if we can some how schedule a service to start automatically after some time to start receiving notifications again. Refer this link

Please do similar tests on your devices and share results. Cheers!

excitepv commented 5 years ago

@DanikKamilov If this is the case then how can anyone use it in real-time apps. Please suggest us a solution. Thanks in advance.

kvenkatasudhakar commented 5 years ago

Everywhere its mentioned that the Vendors like VIVO, ONE PLUS etc. had wrongly implemented Swipe feature to force close the app. This is preventing the apps from receiving any further notifications. Is there any deterministic solution for this issue ? App developers cannot ask each and every app user to disable optimization on battery or enable Autostart. This is a nightmare for people migrating from GCM to Firebase. @Firebase Team, what do you suggest ? When do you think you can correct all Vendors with their wrong implementation ? or provide an alternative solution ?

amitv87 commented 5 years ago

@FCM guys: There's a major architectural issue with the way push notifications work on android. I just do not understand why some part of the app is required to be running in background to receive and display the message. Can't the FCM service itself do that when the app isn't alive.

APNs on iOS does exactly that and it works great. It doesn't matter if the device was rebooted / app swiped / crashed / terminated. It works flawlessly if the user has permitted app to show notifications.

AFAIK FCM service is always alive on every phone that has google play services installed. Why can't it do the same thing.

In my country these days around 80% users own phones manufactured by Chinese vendors and it is impossible to use push notifications with acceptable performance of message delivery, FCM / anything is just not an option here. Plain old SMS are very handy here but expensive.

FCM guys blame it on vendors and these vendors don't care about small app / developers. I wonder how on earth did this happen to world's most popular os.

It is really very sad :(

peterpeterpeter10101 commented 5 years ago

@twitter has same problem. needs real-time 'date' analysis. Although not avalible. twitter seems to 'hold' data or 'stores' data so therfore not 'accuate' data will ever be produced. say if 1M impressed then it gets 'released' by 'increments'. ANYONE CAN ELABORATE? AND ALSO= Data Studio info can possibly get 'misinterpreted' by some of 'users' thinking that the 'display' or '{sample} templates are their 'OWN'

experiencing these problems across web.

TOO MANY LOG IN REQUIREMENT & CREDENTIAL ISSUES. ''VERIFY' SECURITY ISSUES ECT...

On Tue, Oct 23, 2018 at 5:00 AM excitepv notifications@github.com wrote:

@DanikKamilov https://github.com/DanikKamilov If this is the case then how can anyone use it in real-time apps. Please suggest us a solution. Thanks in advance.

— You are receiving this because you commented. Reply to this email directly, view it on GitHub https://github.com/firebase/quickstart-android/issues/368#issuecomment-432217550, or mute the thread https://github.com/notifications/unsubscribe-auth/Alj2rFqJxXyEGvTFAOJVSmhN1cmkEodFks5unwThgaJpZM4QSHEZ .

RDLemos commented 5 years ago

@Firebase Team: I completely agree with the above comment from amitv87 (https://github.com/firebase/quickstart-android/issues/368#issuecomment-433693068)

This makes the service completely unreliable, on phones for these Chinese manufacturers ( or any manufacturer that implemented Swipe feature to force close the app... The user can always stop receiving notifications from the app via Notification settings. The explanation provide on why is implemented this way, does not resist the test when comparing to APN, which works fine: I do not have iOS users where we use APN complaining of too many on undesirable Notifications, but I have a lot of Android users, where we use FCM, with some of the phones mentioned above, complaining of not receiving Notifications...

Any chance this decision can be reviewed? This really make FCM ( and Android) to look bad...

kroikie commented 5 years ago

@All, this issue has been raised here and in many other places. The FCM team is aware of the issue and is working on it. Thanks for the feedback.

Ehofas commented 5 years ago

Is there any news with this issue, or a link to track progress? We are still facing these issues. iOS works perfectly fine

kroikie commented 5 years ago

@Ehofas no news yet on this. I will be sure to update here and in other places once we have a resolution.

Prateek218 commented 5 years ago

VIVO, ONE PLUS, NOKIA, LENOVO are the device in which notification delivery is very poor. when the app in use then it delivers the notification but when the app in swiped from recent app lists. Notification stops delivering. Please resolve this asap. Thank You

NidhinVasudev commented 5 years ago

@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.

I am Tring on one Plus 3. I tried with adding the permission. buts the issue is still there.

NidhinVasudev commented 5 years ago

VIVO, ONE PLUS, NOKIA, LENOVO are the device in which notification delivery is very poor. when the app in use then it delivers the notification but when the app in swiped from recent app lists. Notification stops delivering. Please resolve this asap. Thank You

Having the same issue. Find any solution?

kroikie commented 5 years ago

Hi @NidhinVasudev no update on this issue yet, but we are working on a solution. Thanks for providing these device OEMs.

m4mum commented 5 years ago

@kroikie

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

  1. When user don't want notification from particular app, (s)he is smart enough to go on Settings > Notifications > Disable notification for specific app. Or Uninstall the app.

  2. Many custom ROM/Vendor swipe out = kill the app/Force stop app.

  3. Many times user force stop app cause of cpu usage, battery or slow performance of device

  4. When the app is not running or killed, it is FCM duty to either

A) show user this push notification. (If notification is not explicitly disabled by user) & let user decide to ignore notification or tap on it to launch the app. Or

B) App handle it

we are not sure why the app has been stopped. Thus we don't deliver messages to stopped applications.

Instead of assuming things, Android has explicit control, Settings > Notification > App > Enable/disable/customize as user is in control, FCM should honor the same. If user has not disable notification explicitly, fcm should do something similar to what apple is doing (Apple ecosystem is more strict on this unlike android...still).

Idea is At the end, user is in control to turn on/off app specific notification, or even uninstall the app.

abhishekgargx commented 5 years ago

https://github.com/firebase/quickstart-android/issues/96#issuecomment-449936698

mvanan88 commented 5 years ago

Same issues with asus a007 zenphone!!

Does it got fixed

a92c commented 5 years ago

no solution yet? I think it's a bug. Because if the app is not running, the service fcm it should still work.

it is not clear for the user, enabled app in the whitelist

RishishPandey commented 5 years ago

We're facing issue with Oppo & Vivo Phones. Our application is a VoIP Application and push delivery is critical for us to show the incoming call screen on the receiver end. On the Oppo and Vivo phone just removing the app from recent application kills the app completely and render our app useless. The caller gets frustrated. It's created a lot of problems for our users.

It's been a year since this thread was opened. The affected devices share about 40% of our customer base. It's hurting our business. I hope you guys find a solution fast ( 1 year is a very long time for solving a critical issue like this ). Looking forward to the update.

skfaisal93 commented 5 years ago

I don't think @fcm guys will ever fix this issue. I am complaining about this from the beginning, but instead of fixing it, they only blame it on vendors. The whole FCM team should be sacked for their pathetic development and ignoring developer issues.

Maybe my words are too harsh but I could not control myself anymore.

srikiz commented 5 years ago

I Agree as well, this is an issue and must be fixed by @fcm guys...

MJsaycon commented 5 years ago

I keep reading the thread hoping it was already resolve but it's already 2019. I don't know if the FCM folks really care about this issue.

karthick4in commented 5 years ago

Everyday i am watching this page any solution provided by FCM. Totally disappointed move to other solutions. any one help me?