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

iOS - on('notification') event is not fired when app in foreground using FCM #104

Open
magynhard opened this issue Jul 2, 2021 · 9 comments

Comments

@magynhard
Copy link

magynhard commented Jul 2, 2021

Bug Report

Expected Behaviour

When i send a push notification by FCM to my app on an iOS device and the app is opened in foreground, the event on('notification') is called so i can retrieve and process the push message.

Actual Behaviour

When i send a push notification to my app on an iOS device and the app is opened in foreground, no event is fired. Nothing happens.

The only information i found, was, that for "silent notifications" (no idea what that is) with FCM there is a attribute
content_available: true at the payload.
https://github.com/havesource/cordova-plugin-push/blob/master/docs/PAYLOAD.md#fcm-payload-details

But this seems to be outdated, as i get an error, that this attribute is invalid when using it while sending (see code example below).

Reproduce Scenario (including but not limited to)

Steps to Reproduce

Send a push notification to your app when it is open in the foreground. See code below in the example.

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

iPhone: 6S
iOS: 13.4.1

cordova info Printout

Cordova Packages:

cli: 10.0.0
    common: 4.0.2
    create: 3.0.0
    lib: 10.0.0
        common: 4.0.2
        fetch: 3.0.1
        serve: 4.0.0

Project Installed Platforms:

android: 9.1.0

Project Installed Plugins:

@havesource/cordova-plugin-push: 2.0.0
cordova-plugin-badge: 0.8.8
cordova-plugin-device: 2.0.3
cordova-plugin-enable-cleartext-traffic: 2.1.0
cordova-plugin-ionic-webview: 5.0.0
cordova-plugin-local-notification: 0.9.0-beta.2
cordova-plugin-network-information: 2.0.2
cordova-plugin-play-default-sounds: 1.0.1
cordova-plugin-splashscreen: 6.0.0
cordova-plugin-statusbar: 2.4.3
cordova-plugin-vibration: 3.1.1
cordova-plugin-whitelist: 1.3.4
cordova-sqlite-storage: 6.0.0

Environment:

OS: ManjaroLinux 21.0.7 (rolling) (linux 5.10.42-1-MANJARO) x64
Node: v16.3.0
npm: 7.16.0

android Environment:

android:

ERROR: Command failed with ENOENT: android list target
spawn android ENOENT

Project Setting Files:

config.xml:
<?xml version="1.0" encoding="utf-8" standalone="yes"?>
<widget xmlns="http://www.w3.org/ns/widgets" xmlns:cdv="http://cordova.apache.org/ns/1.0" xmlns:android="http://schemas.android.com/apk/res/android" id="com.myapp" version="1.0.0">
    <name>MyApp</name>
    <description>MyAppDescription</description>
    <author email="[email protected]" href="http://www.myapp.com">MyCompany</author>
    <content src="index.html"/>
    <access origin="*"/>
    <access origin="localhost:8080"/>
    <access origin="localhost:80"/>
    <access origin="localhost"/>
    <access origin="http://localhost:8080/*"/>
    <access origin="http://localhost/*"/>
    <allow-navigation href="*"/>
    <allow-navigation href="http://localhost/*"/>
    <allow-intent href="http://*/*"/>
    <allow-intent href="https://*/*"/>
    <allow-intent href="ws://*/*"/>
    <allow-intent href="wss://*/*"/>
    <allow-intent href="tel:*"/>
    <allow-intent href="sms:*"/>
    <allow-intent href="mailto:*"/>
    <allow-intent href="geo:*"/>
    <platform name="android">
        <allow-intent href="market:*"/>
        <!-- android push notification icons -->
        <resource-file src="resources/android/push_icons/ic_stat_name_mdpi.png" target="/app/src/main/res/drawable-mdpi/ic_stat_name.png"/>
        <resource-file src="resources/android/push_icons/ic_stat_name_hdpi.png" target="/app/src/main/res/drawable-hdpi/ic_stat_name.png"/>
        <resource-file src="resources/android/push_icons/ic_stat_name_xhdpi.png" target="/app/src/main/res/drawable-xhdpi/ic_stat_name.png"/>
        <resource-file src="resources/android/push_icons/ic_stat_name_xxhdpi.png" target="/app/src/main/res/drawable-xxhdpi/ic_stat_name.png"/>
        <resource-file src="resources/android/push_icons/ic_stat_name_xxxhdpi.png" target="/app/src/main/res/drawable-xxxhdpi/ic_stat_name.png"/>
        <!-- android app icons -->
        <resource-file src="resources/android/adaptive/mipmap-hdpi/ic_launcher.png" target="/app/src/main/res/mipmap-hdpi/ic_launcher.png"/>
        <resource-file src="resources/android/adaptive/mipmap-hdpi/ic_launcher_round.png" target="/app/src/main/res/mipmap-hdpi/ic_launcher_round.png"/>
        <resource-file src="resources/android/adaptive/mipmap-mdpi/ic_launcher.png" target="/app/src/main/res/mipmap-mdpi/ic_launcher.png"/>
        <resource-file src="resources/android/adaptive/mipmap-mdpi/ic_launcher_round.png" target="/app/src/main/res/mipmap-mdpi/ic_launcher_round.png"/>
        <resource-file src="resources/android/adaptive/mipmap-xhdpi/ic_launcher.png" target="/app/src/main/res/mipmap-xhdpi/ic_launcher.png"/>
        <resource-file src="resources/android/adaptive/mipmap-xhdpi/ic_launcher_round.png" target="/app/src/main/res/mipmap-xhdpi/ic_launcher_round.png"/>
        <resource-file src="resources/android/adaptive/mipmap-xxhdpi/ic_launcher.png" target="/app/src/main/res/mipmap-xxhdpi/ic_launcher.png"/>
        <resource-file src="resources/android/adaptive/mipmap-xxhdpi/ic_launcher_round.png" target="/app/src/main/res/mipmap-xxhdpi/ic_launcher_round.png"/>
        <resource-file src="resources/android/adaptive/mipmap-xxxhdpi/ic_launcher.png" target="/app/src/main/res/mipmap-xxxhdpi/ic_launcher.png"/>
        <resource-file src="resources/android/adaptive/mipmap-xxxhdpi/ic_launcher_round.png" target="/app/src/main/res/mipmap-xxxhdpi/ic_launcher_round.png"/>
        <!-- android splash screen landscape -->
        <splash width="320" height="200" src="resources/android/splash/android_land-ldpi.png" density="land-ldpi"/>
        <splash width="480" height="320" src="resources/android/splash/android_land-mdpi.png" density="land-mdpi"/>
        <splash width="800" height="480" src="resources/android/splash/android_land-hdpi.png" density="land-hdpi"/>
        <splash width="1280" height="720" src="resources/android/splash/android_land-xhdpi.png" density="land-xhdpi"/>
        <splash width="1600" height="960" src="resources/android/splash/android_land-xxhdpi.png" density="land-xxhdpi"/>
        <splash width="1920" height="1280" src="resources/android/splash/android_land-xxxhdpi.png" density="land-xxxhdpi"/>
        <!-- android splash screen default -->
        <splash width="320" height="200" src="resources/android/splash/android_land-ldpi.png" density="ldpi"/>
        <splash width="480" height="320" src="resources/android/splash/android_land-mdpi.png" density="mdpi"/>
        <splash width="800" height="480" src="resources/android/splash/android_land-hdpi.png" density="hdpi"/>
        <splash width="1280" height="720" src="resources/android/splash/android_land-xhdpi.png" density="xhdpi"/>
        <splash width="1600" height="960" src="resources/android/splash/android_land-xxhdpi.png" density="xxhdpi"/>
        <splash width="1920" height="1280" src="resources/android/splash/android_land-xxxhdpi.png" density="xxxhdpi"/>
        <!-- android splash screen portrait -->
        <splash width="200" height="320" src="resources/android/splash/android_port-ldpi.png" density="port-ldpi"/>
        <splash width="480" height="800" src="resources/android/splash/android_port-mdpi.png" density="port-mdpi"/>
        <splash width="320" height="480" src="resources/android/splash/android_port-hdpi.png" density="port-hdpi"/>
        <splash width="720" height="1280" src="resources/android/splash/android_port-xhdpi.png" density="port-xhdpi"/>
        <splash width="960" height="1600" src="resources/android/splash/android_port-xxhdpi.png" density="port-xxhdpi"/>
        <splash width="1280" height="1920" src="resources/android/splash/android_port-xxxhdpi.png" density="port-xxxhdpi"/>
        <!-- android sdk -->
        <preference name="android-minSdkVersion" value="22"/>
        <preference name="android-targetSdkVersion" value="30"/>
        <preference name="android-targetSandboxVersion" value="30"/>
        <!-- /android sdk -->
        <!-- push messages -->
        <resource-file src="resources/android/push_messages/google-services.json" target="/app/google-services.json"/>
        <!-- /push messages -->
    </platform>
    <platform name="ios">
        <allow-intent href="itms:*"/>
        <allow-intent href="itms-apps:*"/>
        <!-- ios app icons -->
        <icon width="29" height="29" src="resources/ios/icon-29.png"/>
        <icon width="40" height="40" src="resources/ios/icon-40.png"/>
        <icon width="50" height="50" src="resources/ios/icon-50.png"/>
        <icon width="57" height="57" src="resources/ios/icon-57.png"/>
        <icon width="57" height="57" src="resources/ios/Icon.png"/>
        <icon width="58" height="58" src="resources/ios/icon-58.png"/>
        <icon width="60" height="60" src="resources/ios/icon-60.png"/>
        <icon width="72" height="72" src="resources/ios/icon-72.png"/>
        <icon width="76" height="76" src="resources/ios/Icon-76.png"/>
        <icon width="80" height="80" src="resources/ios/icon-80.png"/>
        <icon width="87" height="87" src="resources/ios/icon-87.png"/>
        <icon width="100" height="100" src="resources/ios/icon-100.png"/>
        <icon width="114" height="114" src="resources/ios/icon-114.png"/>
        <icon width="120" height="120" src="resources/ios/icon-120.png"/>
        <icon width="120" height="120" src="resources/ios/[email protected]"/>
        <icon width="144" height="144" src="resources/ios/icon-144.png"/>
        <icon width="152" height="152" src="resources/ios/icon-152.png"/>
        <icon width="167" height="167" src="resources/ios/icon-167.png"/>
        <icon width="180" height="180" src="resources/ios/icon-180.png"/>
        <icon width="1024" height="1024" src="resources/ios/icon-1024.png"/>
        <splash height="2732" src="resources/ios/splash/Default@2x~universal~anyany.png" width="2732"/>
        <!-- ios supported languages information for app store -->
        <edit-config target="CFBundleLocalizations" file="*-Info.plist" mode="overwrite">
            <array>
                <string>en</string>
                <string>de</string>
                <string>es</string>
                <string>fr</string>
                <string>it</string>
                <string>ja</string>
                <string>pl</string>
                <string>pt</string>
                <string>ru</string>
                <string>zh</string>
            </array>
        </edit-config>
        <!-- push messages -->
        <resource-file src="resources/ios/push_messages/GoogleService-Info.plist"/>
        <!-- /push messages -->
    </platform>
    <icon src="www/img/app_icons/icon-96.png"/>
    <!-- web view engine -->
    <preference name="WKWebViewOnly" value="true"/>
    <preference name="webviewbounce" value="false"/>
    <preference name="UIWebViewBounce" value="false"/>
    <preference name="DisallowOverscroll" value="true"/>
    <preference name="CordovaWebViewEngine" value="CDVWKWebViewEngine"/>
    <!-- /web view engine -->
    <!-- splash screen -->
    <preference name="SplashMaintainAspectRatio" value="true"/>
    <preference name="FadeSplashScreenDuration" value="300"/>
    <preference name="SplashShowOnlyFirstTime" value="false"/>
    <preference name="SplashScreen" value="screen"/>
    <preference name="SplashScreenDelay" value="3000"/>
    <preference name="AutoHideSplashScreen" value="true"/>
    <!-- /splash screen -->
    <!-- storage -->
    <preference name="BackupWebStorage" value="none"/>
    <!-- /storage -->
    <!-- push messages (firebase) -->
    <preference name="GradlePluginGoogleServicesEnabled" value="true"/>
    <!-- /push messages (firebase) -->
</widget>
package.json:

--- Start of Cordova JSON Snippet ---

{
  "plugins": {
    "cordova-plugin-whitelist": {},
    "cordova-plugin-ionic-webview": {},
    "cordova-plugin-splashscreen": {},
    "cordova-sqlite-storage": {},
    "cordova-plugin-device": {},
    "cordova-plugin-statusbar": {},
    "cordova-plugin-network-information": {},
    "@havesource/cordova-plugin-push": {
      "ANDROID_SUPPORT_V13_VERSION": "28.0.0",
      "FCM_VERSION": "18.+",
      "IOS_FIREBASE_MESSAGING_VERSION": "~> 6.32.2"
    },
    "cordova-plugin-vibration": {},
    "cordova-plugin-play-default-sounds": {},
    "cordova-plugin-local-notification": {},
    "cordova-plugin-enable-cleartext-traffic": {}
  },
  "platforms": [
    "ios"
  ]
}

--- End of Cordova JSON Snippet ---

Sample Push Data Payload

Sample Code that illustrates the problem

const fs = require('fs');
var admin = require("firebase-admin");
const ios_device_id = fs.readFileSync('ios_device_id', {encoding: 'utf8', flag: 'r'});

const counter = (new Date()).getTime().toString().substring(7);

var message = {
    // content_available: true,  // will throw an error on sending, so we do not use it
    token: ios_device_id,
    notification: {
      title: "My Notification",
      body: "Notification at " + (new Date()).toLocaleString()
    }
    apns: {
        headers: {
            'apns-priority': '10',
        },
        payload: {
            aps: {
                sound: 'default',
                "badge": 101,
            }
        },
    },
    data: {
        "notId": counter,
        "custom_attribute": "abc",
    }
}

var serviceAccount = JSON.parse(fs.readFileSync("firebase_service_account_key.json"));

(async () => {
    admin.initializeApp({
        credential: admin.credential.cert(serviceAccount)
    });
    // Send a message to the device corresponding to the provided registration token.
    admin.messaging().send(message)
        .then((response) => {
            // Response is a message ID string.
            console.log('Successfully sent  message:', response ,"\n" + JSON.stringify(message, null, " "));
        })
        .catch((error) => {
            console.log('Error sending message:', error, "\n" + JSON.stringify(message, null, " "));
        });
})();

Logs taken while reproducing problem

@magynhard
Copy link
Author

FYI

Seems to be related with:
phonegap#2783
phonegap#2824

@shyamal890
Copy link

Any updates on this? I too am facing the same issue.

@gabn88
Copy link

gabn88 commented Feb 22, 2022

duca14036

@duca14036 Are you interested in merging your fixes with this repo? This is now more actively maintained. Thanks!

@gary258796
Copy link

Is this issue related or asme as this one #172 ? which I just opened yesterday .
Take a look , I do found some solution for foreground notifications, just have some question about the purpose and responsibility of the codes I comment out .

@gabn88
Copy link

gabn88 commented Mar 12, 2022

@gary258796

If I'm correct, in effect what you are doing is treating foreground notifications the same as background notifications by commenting those lines. I like the solution above better, because you'll still be able to differentiate between them. But if it works for you then it's good 😀

@gary258796
Copy link

@gabn88 By 'solution above', do you mean changing the payload from @duca14036 ? https://github.com/phonegap/phonegap-plugin-push/issues/2783#issuecomment-621972933

@denkomanceski
Copy link

same issue here

@campagna91
Copy link

same here!! anyone found a way to fix it ?

@DennisRein
Copy link

The solution is waiting here: #186 for almost 9 months now for no reason

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

7 participants