firebase / quickstart-android

Firebase Quickstart Samples for Android
https://firebase.google.com
Apache License 2.0
8.82k 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.)

ghost commented 5 years ago

I have also faced the same issue for the VIVO device. Is there any update form FCM side or OS side?

nasreekar commented 5 years ago

@kroikie @samtstern Hi, just to reconfirm, if the app is killed and I receive bundled notifications and clicking it opens the app(foreground) and when I tap on the notification shouldn't do anything. Is this the right understanding?

Two behaviors:

  1. In-app (foreground), tap bundle, navigate to home screen, tap bundle again, navigate to Notifications screen.
  2. In-app, Home screen, log out, kill the app, tap bundle, navigate to the login screen, tap bundle again, no redirection to Notifications screen.

Any help is appreciated.

KORuL commented 5 years ago

I have this problem! In Meizu M1 Note data-only push comes if app is foreground/background? but if app is killed onMessageRecieved is not call. But in many tetsted phone it works fine

KORuL commented 5 years ago

error W/GCM: broadcast intent callback: result=CANCELLED forIntent { act=com.google.android.c2dm.intent.RECEIVE pkg=net.korul.hbbft (has extras) }

dinak1989 commented 5 years ago

VIVO, OPPO,REDMI 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.

Any solution for this issue ???

swasti29 commented 5 years ago

@fcm Any solutions?

Andronymous commented 5 years ago

Any solutions?

m16u31D commented 5 years ago

@fcm Any solutions?

m4mum commented 5 years ago

Yes fuck fcm & spending few hours on matt give better results & matrix

On Mon, 6 May 2019 at 5:10 AM, elsuper-migueld notifications@github.com wrote:

@fcm https://github.com/fcm Any solutions?

— 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-489473583, or mute the thread https://github.com/notifications/unsubscribe-auth/AAHEYOBCGCJDHJK762BIFPLPT5V67ANCNFSM4ECIOEMQ .

rameshvoltella commented 5 years ago

guys i guess fcm wont give any fix, best solution will be identify manufactures and navigate to enable background service for apps

m4mum commented 5 years ago

or write your own mqtt code and stay away from fcm

On Wed, 8 May 2019 at 6:46 PM, Ramesh M Nair notifications@github.com wrote:

guys i guess fcm wont give any fix, best solution will be identify manufactures and navigate to enable background service for apps

— 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-490481057, or mute the thread https://github.com/notifications/unsubscribe-auth/AAHEYOGHHVSBBSRZHVDPFR3PULHA7ANCNFSM4ECIOEMQ .

EnriqueDDR91 commented 5 years ago

Any solutions?

gopipeddisetti commented 5 years ago

Any body have solution for this???

rahulrvp commented 5 years ago

The thread is still super active and there is no proper solution/workaround given by the contributors? This is sad. I am also facing this same issue. Any help other than adding the wake-lock permissions?

linhtc commented 5 years ago

Same issue : ((

cocadev commented 5 years ago

Nobody can solve this issue right now. maybe need to fix packages

gopipeddisetti commented 5 years ago

is there any alternative solution to fire base???,in iOS there is voip notification(push-kit) like we have any solution???

ThirupathiMukkera commented 5 years ago

I have problem with the OPPO device. When clear the app from the recent apps, The device is not getting notifications, When I open the app, I got all the notifications, how to fix?

michaelr524 commented 5 years ago

So, how is WhatsApp so good at waking up and starting? Which mechanism are they using?

sudhirkhanger commented 5 years ago

@michaelr524 likely they have partnership with the manufacturers so that their app gets priority and isn't killed by the system. Even WhatsApp can't receive messages if it has been killed by the user.

337240552 commented 5 years ago

So, how is WhatsApp so good at waking up and starting? Which mechanism are they using?

white list

KORuL commented 5 years ago

I heard that some enter their application in autostart and then this problem is solved. How to do it programmatically? Or how to programmatically add the application to the white sheet? Banking applications work!

sudhirkhanger commented 5 years ago

Even WhatsApp can't receive messages if it is killed by the user or system via Force Close. You can try it on your device.

MaheshGawhane11 commented 5 years ago

I am getting "FirebaseInstanceId: Error while delivering the message: ServiceIntent not found" when app is killed. Its working fine on Foreground and Background.

facing this issue from last few days. Same code working fine before.

Anyone solved this issue?

kroikie commented 5 years ago

If a user force closes an app the next open should only be done explicitly by the user, FCM messages will not be received in that case.

For apps that are Force Closed by the system (this should not happen) please file a support ticket to Firebase identifying the Make and Model of the device. We will work with the manufacture to correct this behavior.

mu-hasan commented 5 years ago

@kroikie I have try shell pm dump com.kliklabs.chat | grep stopped, on VIVO and OPPO manufacturer, I got response stopped=true although I only swipe up the recent apps list. Thanks

pembeci commented 5 years ago

@kroikie, did you mean: "If a user force closes an app the next open should only be done explicitly by the user, otherwise FCM messages will not be received".

ThirupathiMukkera commented 5 years ago

When we clear the app from memory)(clear from Recent apps), the app is not getting notification. I tried alarm manager to wake the device, but it couldn't. Even Work manager also didn't work to wake the device. This happen in only Chinese ROMs(OPPO, Xiomi, etc.,). If any alternative for the push notifications do let me know.

kroikie commented 5 years ago

@pembeci correct

ThirupathiMukkera commented 5 years ago

When we clear the app from memory)(clear from Recent apps), the app is not getting notification. I tried alarm manager to wake the device, but it couldn't. Even Work manager also didn't work to wake the device. This happen in only Chinese ROMs(OPPO, Xiomi, etc.,). If any alternative for the push notifications do let me know.

@kroikie Do you have solution for it?

kroikie commented 5 years ago

@Tharunreddych unfortunately no, we are working with manufactures to implement the correct functionality, so apps are not "killed" so aggressively

hetang commented 5 years ago

@kroikie why this restrictions implemented first place. If user allowing push notifications settings than blocking notifications by FCM is stupid decision. I am very frustrated to see no solution from past 2 years. APNs notifications works flawlessly and my users just complain about not seeing notifications at critical time. Can we please get at least ETA on the fix?

rajlee5129 commented 5 years ago

Anyone fixed it?

professorbk commented 5 years ago

Been 2 years, any update? Please fix this, my year end bonus depends on this :)

codal-rkatta commented 5 years ago

Try this solution: When app is killed, you won't get data from "notification" object means (remoteMessage.getNofication()== null).

Try to send a custom notification which means do not send "nofication" object in notification and you will get a Push notification when an app is killed.

Example:

{ "to": "token", "collapse_key": "type_a", "data": { "custom_key": "value", "body": "Body of Your Notification", "title": "Title of Your Notification" } }

m4mum commented 5 years ago

Issue with this is when user manually turned off notification, it may lead to spammy behaviour & users are not in control

idle solution is google/fcm need to force device brand to stick to one desired implementation across os

else put upfront disclosure & direct to Non fcm solution to achieve seamless experience for users & developers

correct me if I am missing any point?

Raxit

On Fri, 13 Sep 2019 at 6:07 PM, codal-rkatta notifications@github.com wrote:

Try this solution: When app is killed, you won't get data from "notification" object means (remoteMessage.getNofication()== null).

Try to send a custom notification which means do not send "nofication" object in notification and you will get a Push notification when an app is killed.

Example:

{ "to": "token", "collapse_key": "type_a", "data": { "custom_key": "value", "body": "Body of Your Notification", "title": "Title of Your Notification" } }

— You are receiving this because you commented. Reply to this email directly, view it on GitHub https://github.com/firebase/quickstart-android/issues/368?email_source=notifications&email_token=AAHEYOCPCBG6MEOI2D54U2DQJOCSLA5CNFSM4ECIOEM2YY3PNVWWK3TUL52HS4DFVREXG43VMVBW63LNMVXHJKTDN5WW2ZLOORPWSZGOD6U4POI#issuecomment-531220409, or mute the thread https://github.com/notifications/unsubscribe-auth/AAHEYOA3QSFP2PKQQ3SJICLQJOCSLANCNFSM4ECIOEMQ .

codal-rkatta commented 4 years ago

Issue with this is when user manually turned off notification, it may lead to spammy behaviour & users are not in control idle solution is google/fcm need to force device brand to stick to one desired implementation across os else put upfront disclosure & direct to Non fcm solution to achieve seamless experience for users & developers correct me if I am missing any point? Raxit On Fri, 13 Sep 2019 at 6:07 PM, codal-rkatta @.***> wrote: Try this solution: When app is killed, you won't get data from "notification" object means (remoteMessage.getNofication()== null). Try to send a custom notification which means do not send "nofication" object in notification and you will get a Push notification when an app is killed. Example: { "to": "token", "collapse_key": "type_a", "data": { "custom_key": "value", "body": "Body of Your Notification", "title": "Title of Your Notification" } } — You are receiving this because you commented. Reply to this email directly, view it on GitHub <#368?email_source=notifications&email_token=AAHEYOCPCBG6MEOI2D54U2DQJOCSLA5CNFSM4ECIOEM2YY3PNVWWK3TUL52HS4DFVREXG43VMVBW63LNMVXHJKTDN5WW2ZLOORPWSZGOD6U4POI#issuecomment-531220409>, or mute the thread https://github.com/notifications/unsubscribe-auth/AAHEYOA3QSFP2PKQQ3SJICLQJOCSLANCNFSM4ECIOEMQ .

Turning notification off is another case. You will not get "data" object if you are sending "notification" in your push notification: Check below link for reference: (https://medium.com/@cdmunoz/working-easily-with-fcm-push-notifications-in-android-e1804c80f74)

However, if you want both the objects in a push notification, you have to manage it through intent-filter and your launcher activity.

professorbk commented 4 years ago

I heard that some enter their application in autostart and then this problem is solved. How to do it programmatically? Or how to programmatically add the application to the white sheet?

My device is Asus Zenphone Max X00PD. After I enable autostart for my app, notifications will come when the app is closed. Apparently, we cant enable it programmatically for now.

Srinidhi1561997 commented 4 years ago

:when the app is not in background/killed/not open, then on tapping push notification takes the user to Homescreen, need to change deeplinking logic and take the user to chatScreen

NadeemIqbal commented 4 years ago

Push is not receiving when app is force closed on Motorolla One but receiving in Samsung devices.

Pinjari commented 4 years ago

@fcm Guys Are you really going to provide solution? Its been around 2 years and you have not fixed this in a single Device there are too many manufacturers(Oppo, VIVO, One Plus, Samsung, Honor, Huawei, NOKIA, LENOVO, REDMI) doing it, they dont think about small developers and kill apps whenever they go in the background(Even not swiping app from recent app list) It is so critical issue for every android app developer

KORuL commented 4 years ago

https://github.com/firebase/quickstart-android/issues/368#issuecomment-539272893

This is about work services in background, not about FCM

gopipeddisetti commented 4 years ago

@Tharunreddych unfortunately no, we are working with manufactures to implement the correct functionality, so apps are not "killed" so aggressively

Any update......... :(

mingxin-yang commented 4 years ago

I tried Message App in my HUAWEI Phone, it can receive message when it killed, do it use FCM?

jorgeperalta commented 4 years ago

you should give developers some solution around this, no one to blame but google. You decide wrong behaviors and your documentation is a mess, I've been reading a lot to understand why it works only on some devices and get only this ugly response.

sujeetT9L commented 4 years ago

I am not receiving notification in Huawei honor device when application is in killed state or in background state

olivertech commented 4 years ago

I am using Samsung J5 with this push notification problem when app is swaped from recent apps list.

I have seen other models and brands with the same problem, so I think it is not a cellphone problem, but a FCM one.

My problem has occurred when I upgraded my apps from a 32 bits version to a 64 bits version.

I have 37 apps which I am maintaining, and some of them are old, and are in 32 bits version ... those apps, works fine in foregroud, background and closed state.

But some of my apps, are in 64 bits version, and those apps, the push notification do not work in closed state.

So, I think it could be something associated with this specific situation... Anyway, we need this feature working well, cause our apps uses it as a main feature.

sujeetT9L commented 4 years ago

so I think it is not a cellphone problem, but a FCM one. then why FCM is not resolving this issue.

Isratmity01 commented 4 years ago

We are facing two issues mainly with firebase push notification :-

  1. Users don't receive it
  2. Only 1% of the user opens it (from analytical panel) Maybe this could happen considering devices are killing the background services and users are actually not receiving it. Isn't there any solution for this yet?
Ein6tein commented 4 years ago

We are facing very similar issue: after app is swiped out, if we send notification (we send ONLY data notifications, because we need some custom behaviour) - we receive it and display it via NotificationCompat.Builder workflow.

BUT!!! First received notification is shown and after like 3-5 seconds is automatically dismissed. If we send another notification after that - it works for some time as expected - notification stays in the tray, but, after some time, Android (I suppose) stops service again.

We cannot provide list of ALL devices, that face this issue since we cannot track that stuff - from every perspective but user's - everything works just fine - push came, it was shown successfully.

We could write some workarounds to re-awake service whenever it is stopped (there are examples for that in Google, using BroadcastReceivers), but that should not be our repsonsibility.

As far as I can tell - main problem comes from @firebase decision to not allow us to start service as START_STICKY.

@kroikie - any new insight in the issue?