fechanique / cordova-plugin-fcm

Google FCM Push Notifications Cordova Plugin
623 stars 998 forks source link

onNotification event not triggering when app is closed/in background #502

Open fcarreno opened 6 years ago

fcarreno commented 6 years ago

Hi,

First of all, thanks for such a great plugin. I've read several (possibly related) issues on this repo, but could not find a concrete answer to my use case.

Here's a sample message payload I'm attempting:

{ "data": { "eventType": "assetCreated", "assetType": "review", "assetId": "12345", }, "to":"[DEVICE TOKEN HERE]" }

LogCat monitor shows the following (which proves the message is effectively delivered to the device):

05-02 16:08:23.694 722-1163/? D/ActivityManager: caller:android.app.ApplicationThreadProxy@3310f0ea, r.packageName :ar.com.andobuscando 05-02 16:08:23.714 1529-3778/ar.com.andobuscando D/FCMPlugin: ==> MyFirebaseMessagingService onMessageReceived 05-02 16:08:23.714 1529-3778/ar.com.andobuscando D/FCMPlugin: Key: assetId Value: 12345 05-02 16:08:23.724 1529-3778/ar.com.andobuscando D/FCMPlugin: Key: eventType Value: assetCreated 05-02 16:08:23.724 1529-3778/ar.com.andobuscando D/FCMPlugin: Key: assetType Value: review 05-02 16:08:23.724 1529-3778/ar.com.andobuscando D/FCMPlugin: Notification Data: {assetType=review, eventType=assetCreated, assetId=12345, wasTapped=false} 05-02 16:08:23.724 1529-3778/ar.com.andobuscando D/FCMPlugin: ==> FCMPlugin sendPushPayload 05-02 16:08:23.724 1529-3778/ar.com.andobuscando D/FCMPlugin: notificationCallBackReady: false 05-02 16:08:23.724 1529-3778/ar.com.andobuscando D/FCMPlugin: gWebView: null 05-02 16:08:23.724 1529-3778/ar.com.andobuscando D/FCMPlugin: payload: assetType => review 05-02 16:08:23.724 1529-3778/ar.com.andobuscando D/FCMPlugin: payload: eventType => assetCreated 05-02 16:08:23.724 1529-3778/ar.com.andobuscando D/FCMPlugin: payload: assetId => 12345 05-02 16:08:23.724 1529-3778/ar.com.andobuscando D/FCMPlugin: payload: wasTapped => false 05-02 16:08:23.724 1529-3778/ar.com.andobuscando D/FCMPlugin: View not ready. SAVED NOTIFICATION: javascript:FCMPlugin.onNotificationReceived({"assetType":"review","eventType":"assetCreated","assetId":"12345","wasTapped":false}) 05-02 16:08:23.764 722-1334/? D/ActivityManager: caller:android.app.ApplicationThreadProxy@1690f624, r.packageName :com.google.android.googlequicksearchbox 05-02 16:08:23.774 32277-3780/? I/BgTaskExecutorImpl: Starting EXCLUSIVE background task now_update_gcm_registration.

Now, the issue is that the 'onNotification' event observer I've set on my code is not triggered (note I'm using ionic, and the @ionic-native/fcm wrapper around the plugin)

this.fcm.onNotification().subscribe(data =>{ console.log(Data: ${JSON.stringify(data)}); if(data.wasTapped){ console.log("Received in background"); } else { console.log("Received in foreground"); }; });

I've tested including the "notification" key (with title, body, and click_action) inside the payload, and that - also having the app closed/backgrounded- ends up showing the notification on the notification area, to finally successfully trigger the onNotification event when the notification is tapped, but in my case I want to trigger/show the notification from my app (instead of having fcm do it automatically), given it should be a customized one (e.g.: including action buttons like 'Ignore', 'Accept', 'Deny'), and FCM does not seem to allow that with data in the payload (at least from what I've seen on their docs).

I've also checked some threads that mention including "content-available": "1" or "content-available": true inside the payload data object, but none of those worked for me.

Am I missing something here? Or it seems my use case is not possible with this plugin (maybe not meant to wake up a (ionic) closed app in this scenario)?

Thanks!

PS: after opening the app, the event is triggered, and logcat shows:

05-02 16:29:08.678 1529-1529/ar.com.andobuscando D/FCMPlugin: ==> FCMPlugin sendPushPayload 05-02 16:29:08.678 1529-1529/ar.com.andobuscando D/FCMPlugin: notificationCallBackReady: true

Which makes me think the SAVED NOTIFICATION event highlighted above ends up being restored and sent to the app (but only after manually opening it, which does not help me, as I want the app to be notified while in background/closed)

fcarreno commented 6 years ago

Additional note/clarification on my previous question/comment. When the app is killed (e.g.: by swiping out from the list of current/recent apps), and a data message is received, the plugin captures it, automatically restarts the app process (without opening the UI) , and consequently it does not trigger the callback registered on the app. I guess this is normal behavior.

Once the app is opened manually back again, it sends the notification to the app and the callback gets triggered, which makes sense.

Now, looking at the plugin code, under https://github.com/fechanique/cordova-plugin-fcm/blob/master/src/android/MyFirebaseMessagingService.java

Uncommenting https://github.com/fechanique/cordova-plugin-fcm/blob/161d02dcebd0d798e017a2d16bc016b930da97b2/src/android/MyFirebaseMessagingService.java#L53 helps generating a notification -which could be customized to include buttons for example-, but at this point (without possibility to customize it), it makes no difference than sending a push notification (with both, notification and data properties), which in turn provides the same effect.

Wondering if there are any plans to extend the plugin to customize the notification to be displayed? I understand additional checks may be required, though (e.g.: if app is in foreground, maybe do not display the notification, differentiate between data only vs. notification messages behavior, etc, etc)...

Or maybe this could all be resolved by the main firebase process/service allowing to customize notification styles...(in addition to just displaying icon, title and body...)?

abcdurga commented 6 years ago

I have two ionic Apps:-

App 1:-

cordova android version: 6.30 fcm plugin version: 2.1.2 Push notifications working in foreground, background, and also when app is killed.

App 2:-

cordova android version: 6.30 fcm plugin version: 2.1.2 Push notifications working in foreground, background but not when app is killed.

Initially it was strange. But later i figured out that the payload is different for both projects. So i changed the payload format for App2 and now it receives notifications in all states.

The android payload should be in the following format:

{ "to": "deviceToken", "notification": { "title" : "Hi", "body" : "Received when app is killed", }, "data": { "customKey" : "customValue", } }

harshadduasane commented 6 years ago

@abcdurga your solution is not working with me. i'm not getting notification when app is closed from backgroound. can anyone has solved this problem? plz help

abcdurga commented 6 years ago

@abcdurga your solution is not working with me. i'm not getting notification when app is closed from backgroound. can anyone has solved this problem? plz help

Solved by this approach

Check this post. It is explained very clearly that why some devices are not receiving push notifications.

DrHrishi commented 5 years ago

@abcdurga your solution is not working with me. i'm not getting notification when app is closed from backgroound. can anyone has solved this problem? plz help

use older versions of com.google.firebase:firebase-core and messaging in your android project properties cordova.system.library.4=com.google.firebase:firebase-core:9.8.0 cordova.system.library.5=com.google.firebase:firebase-messaging:9.8.0 instead of cordova.system.library.4=com.google.firebase:firebase-core:+ cordova.system.library.5=com.google.firebase:firebase-messaging:+ core@17.0.0 and messaging@19.0.1 aslo not work on some devices , older works for me.

VarunAnandK commented 5 years ago

Payload : { "notification":{ "title":"Notification title", "body":"Notification body", "click_action":"FCM_PLUGIN_ACTIVITY" }, "data":{ "param1":"value1", "param2":"value2" }, "to":"eS7airUOAeI:APA91bFNM0vW78sesF-tpClHtGUwm82mtXOHpJaPV1DS8NPRRerxMR2pHVpKYPJkxW0rlw51Xtf5aZG3bQ9Ej9iKIpH3ycGyQv5grfRxRloNIuBa6ME2IkUZTMqUnnmjUf6-6U-VaJBa", "priority":"high", "restricted_package_name":"" } in Config.xml

  <edit-config file="AndroidManifest.xml" mode="merge" target="/manifest/application">
            <application android:usesCleartextTraffic="true" />
        </edit-config>

remove this line if u added.

this is for http api

Thanks.

abcdurga commented 5 years ago

https://github.com/fechanique/cordova-plugin-fcm/issues/384#issuecomment-332959253

vardan13 commented 5 years ago

add "click_action":"FCM_PLUGIN_ACTIVITY" in data of server

link : https://stackoverflow.com/questions/39251956/cordova-fcm-plugin-on-android-data-wastapped-not-working