ionic-team / capacitor

Build cross-platform Native Progressive Web Apps for iOS, Android, and the Web ⚡️
https://capacitorjs.com
MIT License
11.31k stars 962 forks source link

Ionic4 and capacitor FCM push notification issue in android #1914

Closed rajlee5129 closed 4 years ago

rajlee5129 commented 4 years ago

Description of the problem: I am developing ionic4 app and I integrated push notification in my app using the following link. I just copy and paste the following link code in my ionic4 app. I am getting a notification if I send notifications from firebase develop console and using "fcm-node" npm module when the app is open or minimized. but if I close or kill the app. I am not receiving any notification. But no issue in ios app, getting notifications even if I kill the ios app. I integrated push notification from the following link. https://capacitor.ionicframework.com/docs/guides/push-notifications-firebase/

Affected platform

OS of the development machine

digaus commented 4 years ago

@rajlee5129

What payload do you send and what do you mean by not getting a notification? Do you not receive the event in the application or does the notification not get displayed?

rajlee5129 commented 4 years ago

not receiving event and data in the application. I tried almost all payloads given by stack overflow, ionic forum, etc. I am using the following payload { "notification": { "title": "Notification title", "body": "Notification body", "sound": "default", "icon": "fcm_push_icon" }, "data": { "title": "actual data title", "body": "actual data body" }, "to": token, "priority": "high" };

digaus commented 4 years ago

@rajlee5129 When the app is force closed in android or ios (killed and not just moved to background) then you will only receive the event when you click on the notification.

What do you want to do with the payload that you need it directly and not when user clicks on it?

rajlee5129 commented 4 years ago

@digaus Like WhatsApp, if I force close the WhatsApp and if anyone sends the message I will get push notification even if force kills the WhatsApp. I need the same behavior in my app.

digaus commented 4 years ago

@digaus Like WhatsApp, if I force close the WhatsApp and if anyone sends the message I will get push notification even if force kills the WhatsApp. I need the same behavior in my app.

not receiving event and data in the application.

I asked you if the notification is not shown or if you do not receive the notification event and you told the later. If you have a title and body, the notification will get displayed on android even if the app is killed.

What you want to do (my guess) is modifying the notification before it gets displayed. Is that correct`?

rajlee5129 commented 4 years ago

No sir, I don't want to modify the notification before it gets displayed. I have title and body but notification not receiving on android if the app is killed. if the app is minimized(background), I am getting the notification and receiving the notification event.

digaus commented 4 years ago

No sir, I don't want to modify the notification before it gets displayed. I have title and body but notification not receiving on android if the app is killed. if the app is minimized(background), I am getting the notification and receiving the notification event.

OK this is something I cannot reproduce on my device. Works correctly here. Notification is displayed on top with Title and body and when I click on it, the app opens and I receive notification event

rajlee5129 commented 4 years ago

Did you use the capacitor push notification, which I provided in the issue(push notification capacitor link) https://capacitor.ionicframework.com/docs/guides/push-notifications-firebase/

rajlee5129 commented 4 years ago

or did you used the Cordova plugin?

digaus commented 4 years ago

I am also using the capacitor plugin. But I use it combined with the capacitor-fcm plugin but that should not be an issue. Which android version do you have ?

rajlee5129 commented 4 years ago

I tested in android M mobiles and nougat mobiles. in build.gradle: minSdkVersion 21 targetSdkVersion 28

digaus commented 4 years ago

@rajlee5129

Could you test android >= 8? Have it running on Android 9 without any issues.

Also check app settings in app manager and make sure that you have everything allow regarding battery optimization or background running.

What device brand are you trying to send notifications to? Huawei, Samsung?

rajlee5129 commented 4 years ago

I am using one plus and oppo

digaus commented 4 years ago

@rajlee5129

Could you test in emulator with android 8/9

Edit: maybe try this -> https://www.google.com/amp/s/www.forbes.com/sites/bensin/2017/07/28/how-to-fix-push-notifications-on-oppo-phones/amp/

rajlee5129 commented 4 years ago

Ok. i will test.

rajlee5129 commented 4 years ago

It is working only on the android pie.

digaus commented 4 years ago

It is working only on the android pie.

Did you try this: https://www.google.com/amp/s/www.forbes.com/sites/bensin/2017/07/28/how-to-fix-push-notifications-on-oppo-phones/amp/ ?

Also did you test in emulator with android 6/7/8 ?

using "fcm-node" npm module when the app is open or minimized

What do you mean with that?

Also might try send push with only notification part.

digaus commented 4 years ago

I did test it on Android 8.1 in the emulator without any issues.

rajlee5129 commented 4 years ago

I tried the link which you have provided, still the same issue. I tried in mobiles 6,7 and 8. I changed the battery optimized setting for my app as suggested in the link. but not receiving the notification and event if I killed or closed the app.

rajlee5129 commented 4 years ago

I didn't test in emulators, because finally, it should work in mobiles. that's why I tested mobile(6,7 and 8) but it working only in android 9.

jcesarmobile commented 4 years ago

When you send the notifications with notification payload (or when you send them from firebase console), if the app is backgrounded or closed, those notifications will be handled by the OS, not by the plugin. Some vendors like Oppo and Xiaomi seem to not handle them well in some cases. There is a similar link that cover some more vendors https://help.batch.com/en/articles/2246074-how-can-i-fix-a-notification-issue-on-my-honor-huawei-sony-oppo, but sadly your users will have to do that.

What you can try is to send data only pushes, those should be handled by the app. But data pushes are not displayed, you should use LocalNotification plugin or some other plugin if you want to display something to the user.

digaus commented 4 years ago

@rajlee5129

You could try use https://github.com/digaus/capacitor-background-fcm

Just send data payload only and implement BackgroundFCMHandler in your android project.

It will display local notification if you do it correctly. That way you can modify notification based on data available in the app (like language or other info). iOS is currently not implement yet though

ionitron-bot[bot] commented 1 year ago

Thanks for the issue! This issue is being locked to prevent comments that are not relevant to the original issue. If this is still an issue with the latest version of Capacitor, please create a new issue and ensure the template is fully filled out.