havesource / cordova-plugin-push

Register and receive push notifications
MIT License
144 stars 276 forks source link

on('notification') not triggered when app is closed #148

Open campagna91 opened 2 years ago

campagna91 commented 2 years ago

Bug Report

Hi. After having performed a first-round with the app, if we receive a notification with the app in both state background or foreground, the 'on notification' function is called as expected.

The problem occurred if the user close the app: in this case, the function listener specified for the 'on notification' event is not triggered

Expected Behaviour

When the user taps on the notification opening the app, the .on('notification', ... function is triggered.

Actual Behaviour

The app is opened normally

Reproduce Scenario (including but not limited to)

Steps to Reproduce

Register FCM token with its callback function, close the app, receive a push and finally tap on the push received.

Platform and Version (eg. Android 5.0 or iOS 9.2.1)

Android version 11 with plugin version@2.0.0

(Android) Device Vendor (e.g. Samsung, HTC, Sony...)

We tried to ore than one device, like Google Pixel 5, Samsung A series

cordova info Printout

our cofig.xml

<access origin="*"/>
<allow-intent href="sms:*" />
<allow-intent href="tel:*" />
<allow-intent href="geo:*" />
<allow-intent href="mailto:*" />
<allow-intent href="file://*/*" />
<allow-intent href="http://*/*" />
<allow-intent href="https://*/*" />
<allow-navigation href="http://*/*" />
<allow-navigation href="https://*/*" />

<!-- SCHEME and DOMAIN  -->
<preference name="scheme" value="ionic" />
<preference name="hostname" value="localhost" />
<preference name="Scheme" value="ionic" />
<preference name="iosScheme" value="ionic" />
<preference name="Hostname" value="localhost" />
<preference name="WKPort" value="" />

<!-- Preferences -->

<preference name="webviewbounce" value="false" />
<preference name="KeyboardResize" value="false" />
<preference name="BackupWebStorage" value="none" />
<preference name="DisallowOverscroll" value="true" />
<preference name="StatusBarStyle" value="lightcontent" />
<preference name="StatusBarOverlaysWebView" value="false" />
<preference name="KeyboardDisplayRequiresUserAction" value="false"/>

<!-- Splash screen -->
<preference name="AutoHideSplashScreen" value="true" />
<preference name="ShowSplashScreenSpinner" value="false" />
<preference name="FadeSplashScreenDuration" value="400" />
<preference name="SplashScreenDelay" value="3000" />

<!-- Hooks -->

<hook src="www/hooks/fwk-after-prepare-hooks.js" type="after_prepare" />
<hook src="www/hooks/fwk-after-plugin-add-hooks.js" type="after_plugin_add" />
<hook src="www/hooks/fwk-after-prepare-copy-build-extra-hooks.js" type="after_prepare" />
<hook src="www/hooks/fwk-after-compile-hooks.js" type="after_compile" />
<hook src="www/hooks/fwk-before-build-hooks.js" type="before_build" />

<!-- Android -->

<platform name="android">

    <preference name="AndroidXEnabled" value="true" />

    <!-- SDK version -->
    <preference name="android-maxSdkVersion" value="30" />
    <preference name="android-targetSdkVersion" value="30" />
    <preference name="android-minSdkVersion" value="22" />

    <!-- Resources -->
    <icon density="ldpi" src="www/res/icon/android/icon-ldpi.png" />
    <icon density="mdpi" src="www/res/icon/android/icon-mdpi.png" />
    <icon density="hdpi" src="www/res/icon/android/icon-hdpi.png" />
    <icon density="xhdpi" src="www/res/icon/android/icon-xhdpi.png" />
    <icon density="xxhdpi" src="www/res/icon/android/icon-xxhdpi.png" />
    <icon density="xxxhdpi" src="www/res/icon/android/icon-xxxhdpi.png" />
    <icon src="www/res/icon/android/icon-market.png" />
    <icon src="www/res/icon/android/icon.png" />

    <resource-file src="www/google-services.json" target="/app/src/debug/google-services.json" />
    <resource-file src="www/google-services.json" target="/app/src/release/google-services.json" />

    <!-- Push -->
    <resource-file src="www/res/icon/android/push/mipmap-mdpi/icon.png" target="res/mipmap-mdpi/icon.png" />
    <resource-file src="www/res/icon/android/push/mipmap-hdpi/icon.png" target="res/mipmap-hdpi/icon.png" />
    <resource-file src="www/res/icon/android/push/mipmap-xhdpi/icon.png" target="res/mipmap-xhdpi/icon.png" />
    <resource-file src="www/res/icon/android/push/mipmap-xxhdpi/icon.png" target="res/mipmap-xxhdpi/icon.png" />
    <resource-file src="www/res/icon/android/push/mipmap-xxxhdpi/icon.png" target="res/mipmap-xxxhdpi/icon.png" />

    <splash density="ldpi" src="www/res/splashscreen/android/ldpi.png" />
    <splash density="mdpi" src="www/res/splashscreen/android/mdpi.png" />
    <splash density="hdpi" src="www/res/splashscreen/android/hdpi.png" />
    <splash density="xhdpi" src="www/res/splashscreen/android/xhdpi.png" />
    <splash density="xxhdpi" src="www/res/splashscreen/android/xxhdpi.png" />
    <splash density="xxxhdpi" src="www/res/splashscreen/android/xxxhdpi.png" />

</platform>

<!-- LIST -->
<!-- Don't modify this plugins list because it's generate-->
<plugin name="cordova-plugin-push" spec="2.0.0"/>
<plugin name="cordova-plugin-app-version" spec="0.1.9"/>
<plugin name="cordova-plugin-calendar" spec="4.6.0"/>
<plugin name="cordova-plugin-call-number" spec="1.0.1"/>
<plugin name="cordova-plugin-device" spec="1.1.7"/>
<plugin name="cordova-plugin-facebook-connect" spec="3.2.0">
    <variable name="APP_ID" value="631910720623798" />
    <variable name="APP_NAME" value="com.companyname.gobooking" />
</plugin>
<plugin name="cordova-plugin-file" spec="6.0.2"/>
<plugin name="cordova-plugin-inappbrowser" spec="4.0.0"/>
<plugin name="cordova-plugin-ionic-keyboard" spec="2.2.0"/>
<plugin name="cordova-plugin-media-capture" spec="3.0.3">
    <variable name="PHOTOLIBRARY_USAGE_DESCRIPTION" value="Allow_the_app_to_access_your_photos" />
</plugin>
<plugin name="cordova-plugin-network-information" spec="1.3.4"/>
<plugin name="cordova-plugin-statusbar" spec="2.4.3"/>
<plugin name="cordova.plugins.diagnostic" spec="6.1.0"/>
<plugin name="cordova-plugin-splashscreen" spec="5.0.3"/>
<plugin name="cordova-plugin-camera" spec="6.0.0">
    <variable name="CAMERA_USAGE_DESCRIPTION" value="Allow_the_app_to_use_your_camera" />
    <variable name="PHOTOLIBRARY_USAGE_DESCRIPTION" value="Allow_the_app_to_access_your_photos" />
</plugin>
<plugin name="cordova-android-support-gradle-release" spec="3.0.1"/>

` -->

Sample Push Data Payload

Sample Code that illustrates the problem

Logs taken while reproducing problem

Logs taken from 'Log Cat' from Android Studio

2021-11-29 16:20:54.193 17913-17973/? D/Push_FCMService: onMessage - from: 54689968372 2021-11-29 16:20:54.206 17913-17973/? D/Push_FCMService: sender id = 54689968372 2021-11-29 16:20:54.207 17913-17973/? D/Push_FCMService: normalize extras 2021-11-29 16:20:54.207 17913-17973/? D/Push_FCMService: key = actions 2021-11-29 16:20:54.207 17913-17973/? D/Push_FCMService: replace key actions with actions 2021-11-29 16:20:54.207 17913-17973/? D/Push_FCMService: key = id 2021-11-29 16:20:54.207 17913-17973/? D/Push_FCMService: replace key id with id 2021-11-29 16:20:54.207 17913-17973/? D/Push_FCMService: key = cat 2021-11-29 16:20:54.207 17913-17973/? D/Push_FCMService: replace key cat with cat 2021-11-29 16:20:54.207 17913-17973/? D/Push_FCMService: key = notId 2021-11-29 16:20:54.207 17913-17973/? D/Push_FCMService: replace key notId with notId 2021-11-29 16:20:54.207 17913-17973/? D/Push_FCMService: key = title 2021-11-29 16:20:54.207 17913-17973/? D/Push_FCMService: replace key title with title 2021-11-29 16:20:54.208 17913-17973/? D/Push_FCMService: no locale found for key = title, error Value Ora of type java.lang.String cannot be converted to JSONObject 2021-11-29 16:20:54.208 17913-17973/? D/Push_FCMService: key = message 2021-11-29 16:20:54.208 17913-17973/? D/Push_FCMService: replace key message with message 2021-11-29 16:20:54.208 17913-17973/? D/Push_FCMService: no locale found for key = message, error Value <ul><li>SHATUSH< of type java.lang.String cannot be converted to JSONObject 2021-11-29 16:20:54.208 17913-17973/? D/Push_FCMService: key = compositeIdEventId 2021-11-29 16:20:54.208 17913-17973/? D/Push_FCMService: replace key compositeIdEventId with compositeIdEventId 2021-11-29 16:20:54.216 17913-17973/? D/Push_FCMService: background 2021-11-29 16:20:54.216 17913-17973/? D/Push_FCMService: message =[<ul><li>campo 1<\/li><li>CAMPO 2<\/li><\/ul><\/br> - LISA - 01\/12\/2021 16:10] 2021-11-29 16:20:54.216 17913-17973/? D/Push_FCMService: title =[Ora puoi confermare il tuo Appuntamento!] 2021-11-29 16:20:54.216 17913-17973/? D/Push_FCMService: contentAvailable =[null] 2021-11-29 16:20:54.216 17913-17973/? D/Push_FCMService: forceStart =[null] 2021-11-29 16:20:54.216 17913-17973/? D/Push_FCMService: create notification 2021-11-29 16:20:54.223 17913-17973/? D/Push_FCMService: Using channel ID = PushPluginChannel 2021-11-29 16:20:54.231 17913-17973/? D/Push_FCMService: stored icon=null 2021-11-29 16:20:54.232 17913-17973/? D/Push_FCMService: stored iconColor=null 2021-11-29 16:20:54.232 17913-17973/? D/Push_FCMService: stored sound=true 2021-11-29 16:20:54.232 17913-17973/? D/Push_FCMService: stored vibrate=true 2021-11-29 16:20:54.232 17913-17973/? D/Push_FCMService: no icon resource found - using application icon 2021-11-29 16:20:54.243 17913-17973/? D/Push_FCMService: create actions: with in-line 2021-11-29 16:20:54.489 4278-4278/? E/music: Blocked onNotificationPosted StatusBarNotification(pkg=com.companyname.appname user=UserHandle{0} id=5 tag=AppName key=0|com.companyname.appname|5|AppName|10475: Notification(channel=PushPluginChannel shortcut=null contentView=null vibrate=null sound=null tick defaults=0x0 flags=0x10 color=0x00000000 vis=PRIVATE)) 2021-11-29 16:20:57.773 1327-2887/? I/ActivityTaskManager: START u0 {flg=0x24000000 cmp=com.companyname.appname/com.adobe.phonegap.push.PushHandlerActivity (has extras)} from uid 10475 2021-11-29 16:20:57.774 1327-2887/? W/ActivityTaskManager: startActivity called from non-Activity context; forcing Intent.FLAG_ACTIVITY_NEW_TASK for: Intent { flg=0x24000000 cmp=com.companyname.appname/com.adobe.phonegap.push.PushHandlerActivity (has extras) } 2021-11-29 16:20:57.823 17913-17913/? D/Push_HandlerActivity: not id = 5 2021-11-29 16:20:57.823 17913-17913/? V/Push_HandlerActivity: onCreate 2021-11-29 16:20:57.823 17913-17913/? D/Push_HandlerActivity: callback = null 2021-11-29 16:20:57.824 17913-17913/? D/Push_HandlerActivity: dismissed = false 2021-11-29 16:20:57.829 17913-17913/? V/Push_Plugin: sendExtras: caching extras to send at a later time. 2021-11-29 16:20:57.829 17913-17913/? D/Push_HandlerActivity: bringToForeground = true 2021-11-29 16:20:57.847 17913-17913/? D/Push_HandlerActivity: isPushPluginActive = false 2021-11-29 16:20:57.847 17913-17913/? D/Push_HandlerActivity: forceMainActivityReload 2021-11-29 16:20:58.204 17913-17913/? D/PluginManager: startupPlugins: put - PushNotification 2021-11-29 16:20:58.294 4278-6029/? E/music: onNotificationRemoved... StatusBarNotification(pkg=com.companyname.appname user=UserHandle{0} id=5 tag=AppName key=0|com.companyname.appname|5|AppName|10475: Notification(channel=PushPluginChannel shortcut=null contentView=null vibrate=null sound=null tick defaults=0x0 flags=0x10 color=0x00000000 vis=PRIVATE)) 2021-11-29 16:21:01.233 17913-18154/? D/PluginManager: getPlugin - put: PushNotification 2021-11-29 16:21:01.233 17913-18154/? V/Push_Plugin: execute: action=hasPermission 2021-11-29 16:21:01.238 17913-18156/? D/Push_Plugin: has permission: true 2021-11-29 16:21:01.372 17913-18154/? V/Push_Plugin: execute: action=init 2021-11-29 16:21:01.373 17913-18156/? V/Push_Plugin: execute: data=[{"android":{}}] 2021-11-29 16:21:01.374 17913-18156/? V/Push_Plugin: execute: jo={} 2021-11-29 16:21:01.375 17913-18156/? V/Push_Plugin: execute: senderID=54689968372 2021-11-29 16:21:01.376 17913-18156/? V/Push_Plugin: onRegistered: {"registrationId":"hidden4privacy","registrationType":"FCM"} 2021-11-29 16:21:01.376 17913-18156/? D/Push_Plugin: no icon option 2021-11-29 16:21:01.376 17913-18156/? D/Push_Plugin: no iconColor option 2021-11-29 16:21:01.377 17913-18156/? V/Push_Plugin: sending cached extras 2021-11-29 16:21:01.377 17913-18156/? D/Push_Plugin: convert extras to json 2021-11-29 16:21:01.377 17913-18156/? D/Push_Plugin: key = actions 2021-11-29 16:21:01.377 17913-18156/? D/Push_Plugin: key = actionCallback 2021-11-29 16:21:01.377 17913-18156/? D/Push_Plugin: key = id 2021-11-29 16:21:01.378 17913-18156/? D/Push_Plugin: key = cat 2021-11-29 16:21:01.378 17913-18156/? D/Push_Plugin: key = notId 2021-11-29 16:21:01.378 17913-18156/? D/Push_Plugin: key = title 2021-11-29 16:21:01.378 17913-18156/? D/Push_Plugin: key = dismissed 2021-11-29 16:21:01.378 17913-18156/? D/Push_Plugin: key = message 2021-11-29 16:21:01.378 17913-18156/? D/Push_Plugin: key = compositeIdEventId 2021-11-29 16:21:01.378 17913-18156/? D/Push_Plugin: key = coldstart 2021-11-29 16:21:01.378 17913-18156/? D/Push_Plugin: key = foreground 2021-11-29 16:21:01.379 17913-18156/? V/Push_Plugin: extrasToJSON: {"title":"Titolo del messaggio","message":"<ul><li>campo 1<\/li><li>CAMPO 2<\/li><\/ul><\/br> - LISA - 01\/12\/2021 16:10","additionalData":{"actions":[],"id":"29887068","cat":"EVENT_TO_CONFIRM","notId":"5","dismissed":false,"compositeIdEventId":"29887068","coldstart":true,"foreground":false}} 2021-11-29 16:21:07.421 17913-18154/? V/Push_Plugin: execute: action=hasPermission 2021-11-29 16:21:07.422 17913-18156/? D/Push_Plugin: has permission: true

xriter commented 2 years ago

Same here. Not found a solution yet.

bcurioletti commented 2 years ago

Same with version 3.0.0

denkomanceski commented 2 years ago

Same here

xriter commented 2 years ago

What I experienced is that there was a conflicting plugin that also dealt with push notifications. After removing the other plugin, it started working. The reason for this was that they both seemed to implement an iOS class and thus conflicted with each other. So maybe that'll help: try removing any other plugin that does something with push notifications...

viktorzavadil commented 2 years ago

I use https://www.npmjs.com/package/firebase-admin for sending push notifications thru FCM.

If I send notification in this structure, on('notification') is not triggered:

{
    "data": {
        "redirectUrl": "..."
    },
        "notification": {
                "title": "Hello by Node.js!",
        "body": "It's working!"
        },
    "token": "{{token}}"
}

If I send this notification, on('notification') is triggered:

{
    "data": {
                "redirectUrl": "...",
        "title": "Hello by Node.js!",
        "body": "It's working!"
    },
    "token": "{{token}}"
}

I call messaging.send() method: https://firebase.google.com/docs/reference/admin/node/firebase-admin.messaging.messaging.md#messagingsend.

I would like to try also method messaging.sendToDevice() https://firebase.google.com/docs/reference/admin/node/firebase-admin.messaging.messaging.md#messagingsendtodevice

Heshyo commented 1 year ago

I use https://www.npmjs.com/package/firebase-admin for sending push notifications thru FCM.

If I send notification in this structure, on('notification') is not triggered:

{
  "data": {
      "redirectUrl": "..."
  },
        "notification": {
                "title": "Hello by Node.js!",
      "body": "It's working!"
        },
  "token": "{{token}}"
}

If I send this notification, on('notification') is triggered:

{
  "data": {
                "redirectUrl": "...",
      "title": "Hello by Node.js!",
      "body": "It's working!"
  },
  "token": "{{token}}"
}

I call messaging.send() method: https://firebase.google.com/docs/reference/admin/node/firebase-admin.messaging.messaging.md#messagingsend.

I would like to try also method messaging.sendToDevice() https://firebase.google.com/docs/reference/admin/node/firebase-admin.messaging.messaging.md#messagingsendtodevice

Isn't it what https://github.com/havesource/cordova-plugin-push/blob/master/docs/PAYLOAD.md#notification-vs-data-payloads tells you to do? I've had to use different messages for iOS and Android.

IpyZ commented 1 year ago

I use https://www.npmjs.com/package/firebase-admin for sending push notifications thru FCM.

If I send notification in this structure, on('notification') is not triggered:

{
  "data": {
      "redirectUrl": "..."
  },
        "notification": {
                "title": "Hello by Node.js!",
      "body": "It's working!"
        },
  "token": "{{token}}"
}

If I send this notification, on('notification') is triggered:

{
  "data": {
                "redirectUrl": "...",
      "title": "Hello by Node.js!",
      "body": "It's working!"
  },
  "token": "{{token}}"
}

I call messaging.send() method: https://firebase.google.com/docs/reference/admin/node/firebase-admin.messaging.messaging.md#messagingsend.

I would like to try also method messaging.sendToDevice() https://firebase.google.com/docs/reference/admin/node/firebase-admin.messaging.messaging.md#messagingsendtodevice

Works like a charm! Thanks so much bro