Cordova-plugin-firebasex: Not received foreground notification info on OnMessageReceive

Created on 23 Jul 2019  Â·  30Comments  Â·  Source: dpa99c/cordova-plugin-firebasex

Bug report

No notification received on OnMessageReceived when app is in foreground Mode with a notification sended from Firebase console.

When the app is in background mode the notification arrives to system tray and then if i click to it the info that arrives to OnMessageReceived is this:

collapse_key: "com.mybundleid.app"
from: "679237267662"
google.delivered_priority: "high"
google.message_id: "0:1563868372948653%a793bb1ea793bb1e"
google.original_priority: "high"
google.sent_time: 1563868372756
google.ttl: 2419200
messageType: "notification"
tap: "background"

No info about title, body.. arrives.

Expected behavior:
Notification info arrives to OnMessageReceived from both modes, foreground and background(tapped)

Environment information

Ionic:

   ionic (Ionic CLI)  : 4.12.0 (/usr/local/lib/node_modules/ionic)
   Ionic Framework    : ionic-angular 3.9.6
   @ionic/app-scripts : 3.2.3

Cordova:

   cordova (Cordova CLI) : 9.0.0 ([email protected])
   Cordova Platforms     : android 8.0.0, ios 5.0.1
   Cordova Plugins       : cordova-plugin-ionic-webview 1.2.1, (and 22 other plugins)

System:

   Android SDK Tools : 26.1.1 (/Users/myuser/Library/Android/sdk)
   ios-deploy        : 1.9.2
   ios-sim           : 8.0.1
   NodeJS            : v9.9.0 (/usr/local/bin/node)
   npm               : 5.7.1
   OS                : macOS Mojave
   Xcode             : Xcode 10.2.1 Build version 10E1001
  • Device details

    • Samsung device

  • OS details

    • Android 7.0

cordova-plugin-firebasex 6.0.0
cordova-plugin-androidx 1.0.2
cordova-plugin-androidx-adapter 1.1.0

Related code:

...
declare const window: any;
...

    getNotificationRecived() {
       console.log('before')
        window.FirebasePlugin.onMessageReceived(function(message) {
            console.dir(message) // above info showed
            console.dir(message.title) // undefined
            console.dir(message.notification.title) // error
            console.log("Message type: " + message.messageType); // notification
            if (message.messageType === "notification"){
                console.log("Notification message received");
                if(message.tap){
                    console.log("Tapped in " + message.tap);
                }
            }
            //console.dir(notification);
        },function(error)  {
            console.error(error);
        });

More info:

I have already tested sending a test notification and a full notification from firebase console. The behaviour is the same.

Thanks for your work

android bug investigate

Most helpful comment

I have the same issue . when i call it in background i do get the notification and on onMessageReceived show me this response

collapse_key: "com.**.**"
from: "599298325333"
google.delivered_priority: "high"
google.message_id: "0:1589550478288659%c0efd0a8c0efd0a8"
google.original_priority: "high"
google.sent_time: 1589550477989
google.ttl: 2419200
messageType: "notification"
tap: "background"

but when i call it forground i dont get the notification and onMessageReceived show me this response :

body: "mm"
collapse_key: "com.**.**"
from: "599298325333"
id: "0:1589550548775009%c0efd0a8c0efd0a8"
messageType: "notification"
sent_time: "1589550548554"
show_notification: "true"
sound: "default"
title: "mm"
ttl: "2419200"

any solution please

All 30 comments

Thanks for reporting - I'll have a look into this later

I've published this fix in [email protected]

I tried the same with latest 6.0.1. Still foreground push not working.

notification.json sent to example project app using [email protected] while in foreground:

Screenshot_20190724-091302

The problem now has changed with 6.0.1:

While the app is in foreground the notification arrives with the info:

body: "555"
collapse_key: "com.bundle.app"
from: "679237267662"
id: "0:1563958960304468%a793bb1ea793bb1e"
messageType: "notification"
sent_time: "1563958960293"
show_notification: "false"
title: "55"
ttl: "2419200"

However if the app is background(killed and minimized) and tap it the notification arrives with:

collapse_key: "com.bundle.app"
from: "679237267662"
google.delivered_priority: "high"
google.message_id: "0:1563959031769194%a793bb1ea793bb1e"
google.original_priority: "high"
google.sent_time: 1563959031764
google.ttl: 2419200
messageType: "notification"
tap: "background"

From firebase console I do not send the "notification_foreground": "true", att

If I send a push notification via postman to :

https://fcm.googleapis.com/fcm/send

With this body (your notification.json example):

{
  "to":"MY_DEVICE_TOKEN", 
  "name": "notification",
  "notification": {
    "body": "Notification with default settings",
    "title": "Default Notification"
  },
  "android": {
    "collapse_key": "a",
    "priority": "normal",
    "ttl": "3600s",
    "notification": {
      "channel_id": "fcm_default_channel",
      "sound": "default",
      "color": "#4290b7"

    }
  },
  "apns": {
    "payload": {
      "aps": {
        "badge": 1,
        "sound": "default"
      }
    }
  },
  "data": {
    "notification_foreground": "true",
    "key_1": "Data for key one"
  }
}

The app force closes and the LogCat shows this error:

07-24 14:27:23.488 9092-9886/com.bundle.app E/AndroidRuntime: FATAL EXCEPTION: Firebase-FirebasePluginMessagingService
    Process: com.bundle.app, PID: 9092
    java.lang.NullPointerException: Attempt to invoke virtual method 'boolean java.lang.String.equals(java.lang.Object)' on a null object reference
        at org.apache.cordova.firebase.FirebasePluginMessagingService.sendMessage(FirebasePluginMessagingService.java:206)
        at org.apache.cordova.firebase.FirebasePluginMessagingService.onMessageReceived(FirebasePluginMessagingService.java:154)
        at com.google.firebase.messaging.FirebaseMessagingService.zzd(Unknown Source)
        at com.google.firebase.iid.zzb.run(Unknown Source)
        at java.util.concurrent.ThreadPoolExecutor.runWorker(ThreadPoolExecutor.java:1133)
        at java.util.concurrent.ThreadPoolExecutor$Worker.run(ThreadPoolExecutor.java:607)
        at com.google.android.gms.common.util.concurrent.zza.run(Unknown Source)
        at java.lang.Thread.run(Thread.java:761)
07-24 14:27:24.190 9092-9170/com.bundle.app E/ConnectivityManager.CallbackHandler: callback not found for RELEASED message

One question, do you receive reports if this issue is clossed? Or I have to open new issue?

One question, do you receive reports if this issue is clossed? Or I have to open new issue?

I still get emails.

If I send a push notification via postman to : https://fcm.googleapis.com/fcm/send

This is the legacy API - notification.json is formatted for the v1 API: https://fcm.googleapis.com/v1/

But yes, it shouldn't cause an app crash - I will fix that.

I've published [email protected] which improves the Android implementation:
better exception handling e.g. sending malformed notification message should no longer cause an app crash but will log error to Crashlytics/logcat/Webview.

Note that if the app is not running when the notification message arrives on Android, when you tap the system notification to launch the app, the full notification message payload (e.g. title & body) is not available in the Intent, therefore the plugin cannot forward this on to onMessageReceived().

I've updated the documentation to make this explicitly clear.

Hello,

First of all thanks for your work, pleasure to work with your updated plugin :)

You forgot to update the documentation too, because this tap=foreground|background should be this now tap=background

@M0NsTeRRR The documentation is correct: | is the logical OR operator so is conventionally used to list multiple possible values, i.e.

tap=foreground|background => tap=foreground OR tap=background

Hi, in the documentation is not explicit, on iOS notifications the notification_foreground takes effect?
On iOS I am not get the notification on system tray if app is in foreground.

If I send a push notification via postman to : https://fcm.googleapis.com/fcm/send

This is the legacy API - notification.json is formatted for the v1 API: https://fcm.googleapis.com/v1/

I am trying via Postman with this url but get a 404 error
https://fcm.googleapis.com/v1/projects/myproject-hash/messages:send

Is the url correct?

It looks correct, however I have not interacted directly with the v1 FCM API via HTTP, but instead use the google-auth-library in a NodeJS environment to send the messages, as the sendMessage script does in the example project

about foreground notifications on ios?

I'm not sure what changed. After two days investing for foreground notification to work. I rolled back to v6.0.0 and the foreground works fine. However, same doesn't work on v6.0.1 as well as on v6.0.2. I have a ionic 4 project w/ angular.

EDIT: This is just for android haven't tested out iOS yet.

I hope you can help to resolve this.

@tusharvikky Try building and running the example project and using its FCM API client to send the predefined test messages.

Here's screenshots of it when I send notification.json (app build with v6.0.2 of this plugin):


I have the same problem..
I don't get the onMessageReceived callback, when my app is in foreground but it works when the app is in background and i tap the notification..

Exactly as said from @pilz97. I just check this isse and follow the instructions to update the plugin to @6.0.2 but still the same behavior.

image
As you can see, I added the setting for notification_foreground: 'true'.

What are I missing here? Can you help us @dpa99c?

@TIGarupa i got the solution!

I just added:
<config-file target="AndroidManifest.xml" parent="/manifest/application" > <service android:enabled="true" android:exported="false" android:name="com.google.android.gms.measurement.AppMeasurementService" /> <service android:name="org.apache.cordova.firebase.FirebasePluginMessagingService"> <intent-filter> <action android:name="com.google.firebase.MESSAGING_EVENT"/> </intent-filter> </service> <receiver android:name="org.apache.cordova.firebase.OnNotificationOpenReceiver"/> <meta-data android:name="com.google.firebase.messaging.default_notification_color" android:resource="@color/accent" /> <meta-data android:name="com.google.firebase.messaging.default_notification_channel_id" android:value="@string/default_notification_channel_id"/> </config-file>

in my <platform name="android"> section.

Hope that solves the problem for you too :)

Thank you @pilz97 this solved for now.
I just think this is just a work around, but for now, can be used.

@dpa99c I have some issue in ionic4 application.
First of all I appreciate to use your [email protected].
I tested using your test app. It is working fully for push notification.
I build ionic4 application using your plugin. So I use typescript wrapper of your plugin.
When app is installed on iOS device first, push notification is working fully.
But after app is closed or changed into background, push notification is not working fully.
In other words, I can see push notification. But "onMessageRecevied" function is not working fully.
I added following function after checking firebase permission.
window['FirebasePlugin'].onMessageReceived(function(message) {
try{
console.log("onMessageReceived");
console.dir(message);

    }catch(e){
        // logError("Exception in onMessageReceived callback: "+e.message);
        console.log("Exception in onMessageReceived callback:");
    }

}, function(error) {
    //logError("Failed receiving FirebasePlugin message: " + error);
    console.log("Failed receiving FirebasePlugin message:");
});

But I can't see any log data in Xcode console.
In test project. I think you used same code.
If you have any idea about this issue, please let me know.
Thanks.

@Roman8817 I have the exact same issue as you.
Did you find a solution to this issue?

Having the exact same issue on android. Any solution to this

Note: on tapping a background notification, if your app is not running, only the data section of the notification message payload will be delivered to onMessageReceived. i.e. the notification title, body, etc. will not. Therefore if you need the properties of the notification message itself (e.g. title & body) to be delivered to onMessageReceived, you must duplicate these in the data section, e.g.:

{
"name": "my_notification",
"notification": {
"body": "Notification body",
"title": "Notification title"
},
"data": {
"notification_body": "Notification body",
"notification_title": "Notification title"
}
}

Any idea how can i do this? When my app is in background and when i send a notification from FCM, i get my data like this:

background notification {
"google.delivered_priority": "high",
"google.sent_time": 1570833258541,
"google.ttl": 2419200,
"google.original_priority": "high",
"messageType": "notification",
"tap": "background",
"from": "143972685264",
"google.message_id": "0:1570833258720050%776720b6776720b6",
"collapse_key": "com.wearesas.dragonridge"
}

without title and body. How can i get my notification title and body?

@pilz97 Thanks man, solved here!

@TIGarupa i got the solution!

I just added:
<config-file target="AndroidManifest.xml" parent="/manifest/application" > <service android:enabled="true" android:exported="false" android:name="com.google.android.gms.measurement.AppMeasurementService" /> <service android:name="org.apache.cordova.firebase.FirebasePluginMessagingService"> <intent-filter> <action android:name="com.google.firebase.MESSAGING_EVENT"/> </intent-filter> </service> <receiver android:name="org.apache.cordova.firebase.OnNotificationOpenReceiver"/> <meta-data android:name="com.google.firebase.messaging.default_notification_color" android:resource="@color/accent" /> <meta-data android:name="com.google.firebase.messaging.default_notification_channel_id" android:value="@string/default_notification_channel_id"/> </config-file>

in my <platform name="android"> section.

Hope that solves the problem for you too :)

This did not work for me.

What I noticed is, that if we call onMessageReceived() the notification is shown only in background, but when in foreground we have to handle the notification.

Still doesn't work. Please assist me...

Let me know how can i assist you. Happy to help.

On Sun, May 10, 2020, 10:30 PM Yogesh Kumar notifications@github.com
wrote:

Still doesn't work. Please assist me...

—
You are receiving this because you commented.
Reply to this email directly, view it on GitHub
https://github.com/dpa99c/cordova-plugin-firebasex/issues/52#issuecomment-626357977,
or unsubscribe
https://github.com/notifications/unsubscribe-auth/ABQY6POHJTD4ABOL5CHWNATRQ3MUPANCNFSM4IGBGVIA
.

The issue still persist in v9.1.1 and platform Android.
When i receive the push in background and tap this, title and body fields don't appear.
When the push arrive in foreground, both fields are showed.
I have sent notifications from Firebase and my own backend, but the result is the same

I have the same issue . when i call it in background i do get the notification and on onMessageReceived show me this response

collapse_key: "com.**.**"
from: "599298325333"
google.delivered_priority: "high"
google.message_id: "0:1589550478288659%c0efd0a8c0efd0a8"
google.original_priority: "high"
google.sent_time: 1589550477989
google.ttl: 2419200
messageType: "notification"
tap: "background"

but when i call it forground i dont get the notification and onMessageReceived show me this response :

body: "mm"
collapse_key: "com.**.**"
from: "599298325333"
id: "0:1589550548775009%c0efd0a8c0efd0a8"
messageType: "notification"
sent_time: "1589550548554"
show_notification: "true"
sound: "default"
title: "mm"
ttl: "2419200"

any solution please

Was this page helpful?
0 / 5 - 0 ratings

Related issues

capc0 picture capc0  Â·  5Comments

almothafar picture almothafar  Â·  5Comments

Akz47 picture Akz47  Â·  6Comments

luboslav picture luboslav  Â·  6Comments

marcelonnunes picture marcelonnunes  Â·  3Comments