Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Not received foreground notification info on OnMessageReceive #52

Closed
testanddeploy opened this issue Jul 23, 2019 · 39 comments
Closed

Not received foreground notification info on OnMessageReceive #52

testanddeploy opened this issue Jul 23, 2019 · 39 comments
Labels
android Relates to Android platform bug Something isn't working properly investigate Issue requires investigation

Comments

@testanddeploy
Copy link

testanddeploy commented Jul 23, 2019

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

@testanddeploy testanddeploy changed the title Not receive foreground notification info on OnMessageReceive Not received foreground notification info on OnMessageReceive Jul 23, 2019
@dpa99c
Copy link
Owner

dpa99c commented Jul 23, 2019

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

@dpa99c dpa99c added android Relates to Android platform bug Something isn't working properly investigate Issue requires investigation labels Jul 23, 2019
@dpa99c dpa99c closed this as completed in cfdae72 Jul 23, 2019
@dpa99c
Copy link
Owner

dpa99c commented Jul 23, 2019

I've published this fix in [email protected]

@tusharvikky
Copy link

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

@dpa99c
Copy link
Owner

dpa99c commented Jul 24, 2019

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

Screenshot_20190724-091302

@testanddeploy
Copy link
Author

testanddeploy commented Jul 24, 2019

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

@testanddeploy
Copy link
Author

testanddeploy commented Jul 24, 2019

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?

@dpa99c
Copy link
Owner

dpa99c commented Jul 24, 2019

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

I still get emails.

@dpa99c dpa99c reopened this Jul 24, 2019
@dpa99c
Copy link
Owner

dpa99c commented Jul 24, 2019

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.

@dpa99c
Copy link
Owner

dpa99c commented Jul 24, 2019

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.

@dpa99c dpa99c closed this as completed Jul 24, 2019
@M0NsTeRRR
Copy link

M0NsTeRRR commented Jul 24, 2019

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

@dpa99c
Copy link
Owner

dpa99c commented Jul 25, 2019

@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

@testanddeploy
Copy link
Author

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?

@dpa99c
Copy link
Owner

dpa99c commented Jul 25, 2019

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

@testanddeploy
Copy link
Author

about foreground notifications on ios?

@tusharvikky
Copy link

tusharvikky commented Jul 26, 2019

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.

@dpa99c
Copy link
Owner

dpa99c commented Jul 26, 2019

@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):

@pilz97
Copy link

pilz97 commented Aug 1, 2019

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

@TiGarupa
Copy link

TiGarupa commented Aug 1, 2019

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?

@pilz97
Copy link

pilz97 commented Aug 1, 2019

@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 :)

@TiGarupa
Copy link

TiGarupa commented Aug 1, 2019

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

@driggers-paul
Copy link

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

@nico24github
Copy link

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

@ramzauchenna
Copy link

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

@kartiksolanki
Copy link

kartiksolanki commented Oct 11, 2019

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?

@isaacbatst
Copy link

@pilz97 Thanks man, solved here!

tommy-hartz pushed a commit to tommy-hartz/cordova-plugin-firebasex that referenced this issue Nov 22, 2019
…nt to onMessageReceived().

Ensure message is always sent to onMessageReceived(), regardless if it was tapped.
Resolves dpa99c#52.
@iAbhinav
Copy link

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

@yogeshpatil143
Copy link

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

@iAbhinav
Copy link

iAbhinav commented May 10, 2020 via email

@fgarcia5
Copy link

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

@mohamedHassanKa
Copy link

mohamedHassanKa commented May 15, 2020

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

@jfrubio
Copy link

jfrubio commented Mar 3, 2022

Hi.
I'm using cordova-plugin-firebasex 14.0.0, and have the same error.
Push arrive ok in background but in foreground nothing happends, no push is showed and onMessageReceived its not fired. onMessageReceived is only fired if I have the app in foreground and tap on it, then the app opens and I cant work with message.
¿Any solution?

@adrianva1983
Copy link

Buenos días, tambien tengo el mismo problema:
Estoy usando cordova-plugin-firebasex 14.0.0 y tengo el mismo error.
El empuje llega bien en segundo plano, pero en primer plano no sucede nada, no se muestra ningún empuje y onMessageReceived no se activa. onMessageReceived solo se activa si tengo la aplicación en primer plano y la toco, luego la aplicación se abre y no puedo trabajar con el mensaje.
¿Alguna solución?
gracias

@jfrubio
Copy link

jfrubio commented Mar 25, 2022

Hola [adrianva1983], yo no he encontrado solución. Si encuentro algo te digo, si vieses algo no dejes de avisar. Gracias

@tolutronics
Copy link

Hello, i think this issue occurs in the recent versions of this plugin.

@imadkhatib
Copy link

Hello, It occurs in the recent version of plugin("cordova-plugin-firebasex": "^14.1.0")
Please advise.

@Adityakhandelwal021
Copy link

Hello guys It's fine in the background but there is issue in getting foreground notification

this.firebaseX.onMessageReceived()
.subscribe(data => console.log(User opened a notification ${data}));
didn't get anything here is console and ad the time of foreground it's not call onMessagerecived because the string is also not print in the console

@anuskaoo
Copy link

anuskaoo commented Feb 5, 2024

Hola. Mi version del plugin es 16.1.0. Yo tengo el mismo problema en iOS. En segundo plano funciona correctamente tanto en Android como en iOS. En primer plano en ANdroid va bien pero en iOS me da el message como indefinido. ¿Qué puede pasar? Me estoy volviendo loca
Gracias y un saludo.

@anuskaoo
Copy link

anuskaoo commented Feb 5, 2024

Ayudame @dpa99c por favorrrrrrr. Gracias

@alejandroldlr
Copy link

Mismo problema firebasex 16.3 IOS en primer plano no recibe los mensajes de data porque no se dispara el evento onMessageReceived

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
android Relates to Android platform bug Something isn't working properly investigate Issue requires investigation
Projects
None yet
Development

No branches or pull requests