havesource / cordova-plugin-push

Register and receive push notifications
MIT License
149 stars 285 forks source link

coldstart property is always false #316

Open lorenzodianni opened 6 days ago

lorenzodianni commented 6 days ago

Bug Report

After updating from version 4 to 5, the "coldstart" property is always set to false, even when I tap on the notification while the app is closed.

Expected Behaviour

The coldstart property should be set to true if the notification is tapped while the app is closed.

Actual Behaviour

Tapping the notification while the app is closed, coldstart is always set to false.

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

iOS 18

cordova info Printout

Cordova Packages:

    cli: 12.0.0
        common: 5.0.0
        create: 5.0.0
        lib: 12.0.2
            common: 5.0.0
            fetch: 4.0.0
            serve: 4.0.1

Project Installed Platforms:

    ios: 7.1.1

Project Installed Plugins:

    @havesource/cordova-plugin-push: 5.0.0
    cordova-plugin-dialogs: 3.0.0-dev
    cordova-plugin-firebase-analytics: 8.0.0
    cordova-plugin-geolocation: 5.0.0
    cordova-plugin-inappbrowser: 6.0.0
    cordova-plugin-keyboard: 1.2.0
    cordova-plugin-localization-strings: 5.0.5
    cordova-plugin-splashscreen: 6.0.2
    cordova-plugin-taptic-engine: 2.2.0

Environment:

    OS: macOS Sonoma 14.5 (23F79) (darwin 23.5.0) arm64
    Node: v20.11.0
    npm: 10.2.4

ios Environment:

    xcodebuild:
Xcode 16.1
Build version 16B40

Project Setting Files:

    config.xml:
<?xml version='1.0' encoding='utf-8'?>
<widget id="com.test.foo" version="0.0.0" ios-CFBundleVersion="9" xmlns="http://www.w3.org/ns/widgets"
        xmlns:cdv="http://cordova.apache.org/ns/1.0">
  <name>Example</name>
  <!--
      <description>Sample Apache Cordova App</description>
      <author email="dev@cordova.apache.org" href="https://cordova.apache.org">
          Apache Cordova Team
      </author>
  -->
  <content src="index.html"/>
  <allow-intent href="http://*/*"/>
  <allow-intent href="https://*/*"/>
  <allow-intent href="tel:*"/>
  <preference name="AutoHideSplashScreen" value="false" />
  <preference name="FadeSplashScreen" value="true"/>
  <preference name="FadeSplashScreenDuration" value="500"/>
  <preference name="ShowSplashScreenSpinner" value="false"/>
  <preference name="Orientation" value="portrait"/>
  <preference name="target-device" value="handset"/>
  <preference name="HideKeyboardFormAccessoryBar" value="true"/>
  <platform name="ios">
    <preference name="deployment-target" value="16.4"/>
    <resource-file src="GoogleService-Info.plist"/>
    <edit-config target="NSLocationWhenInUseUsageDescription" file="*-Info.plist" mode="merge">
      <string>Need location access to find things nearby</string>
    </edit-config>
    <edit-config target="NSLocationAlwaysAndWhenInUseUsageDescription" file="*-Info.plist" mode="merge">
      <string>Need location access to find things nearby</string>
    </edit-config>
    <edit-config target="NSLocationAlwaysUsageDescription" file="*-Info.plist" mode="merge">
      <string>Need location access to find things nearby</string>
    </edit-config>
    <edit-config target="CFBundleLocalizations" file="*-Info.plist" mode="overwrite">
      <array>
        <string>en</string>
        <string>it</string>
      </array>
    </edit-config>
    <icon height="57" src="resources/ios/icon/icon.png" width="57"/>
    <icon height="114" src="resources/ios/icon/icon@2x.png" width="114"/>
    <icon height="20" src="resources/ios/icon/icon-20.png" width="20"/>
    <icon height="40" src="resources/ios/icon/icon-20@2x.png" width="40"/>
    <icon height="60" src="resources/ios/icon/icon-20@3x.png" width="60"/>
    <icon height="29" src="resources/ios/icon/icon-29.png" width="29"/>
    <icon height="58" src="resources/ios/icon/icon-29@2x.png" width="58"/>
    <icon height="87" src="resources/ios/icon/icon-29@3x.png" width="87"/>
    <icon height="48" src="resources/ios/icon/icon-24@2x.png" width="48"/>
    <icon height="55" src="resources/ios/icon/icon-27.5@2x.png" width="55"/>
    <icon height="88" src="resources/ios/icon/icon-44@2x.png" width="88"/>
    <icon height="172" src="resources/ios/icon/icon-86@2x.png" width="172"/>
    <icon height="196" src="resources/ios/icon/icon-98@2x.png" width="196"/>
    <icon height="216" src="resources/ios/icon/icon-108@2x.png" width="216"/>
    <icon height="40" src="resources/ios/icon/icon-40.png" width="40"/>
    <icon height="80" src="resources/ios/icon/icon-40@2x.png" width="80"/>
    <icon height="120" src="resources/ios/icon/icon-40@3x.png" width="120"/>
    <icon height="50" src="resources/ios/icon/icon-50.png" width="50"/>
    <icon height="100" src="resources/ios/icon/icon-50@2x.png" width="100"/>
    <icon height="60" src="resources/ios/icon/icon-60.png" width="60"/>
    <icon height="120" src="resources/ios/icon/icon-60@2x.png" width="120"/>
    <icon height="180" src="resources/ios/icon/icon-60@3x.png" width="180"/>
    <icon height="72" src="resources/ios/icon/icon-72.png" width="72"/>
    <icon height="144" src="resources/ios/icon/icon-72@2x.png" width="144"/>
    <icon height="76" src="resources/ios/icon/icon-76.png" width="76"/>
    <icon height="152" src="resources/ios/icon/icon-76@2x.png" width="152"/>
    <icon height="167" src="resources/ios/icon/icon-83.5@2x.png" width="167"/>
    <icon height="1024" src="resources/ios/icon/icon-1024.png" width="1024"/>
    <splash height="1136" src="resources/ios/splash/Default-568h@2x~iphone.png" width="640"/>
    <splash height="1334" src="resources/ios/splash/Default-667h.png" width="750"/>
    <splash height="2688" src="resources/ios/splash/Default-2688h~iphone.png" width="1242"/>
    <splash height="1242" src="resources/ios/splash/Default-Landscape-2688h~iphone.png" width="2688"/>
    <splash height="1792" src="resources/ios/splash/Default-1792h~iphone.png" width="828"/>
    <splash height="828" src="resources/ios/splash/Default-Landscape-1792h~iphone.png" width="1792"/>
    <splash height="2436" src="resources/ios/splash/Default-2436h.png" width="1125"/>
    <splash height="1125" src="resources/ios/splash/Default-Landscape-2436h.png" width="2436"/>
    <splash height="2208" src="resources/ios/splash/Default-736h.png" width="1242"/>
    <splash height="1242" src="resources/ios/splash/Default-Landscape-736h.png" width="2208"/>
    <splash height="1536" src="resources/ios/splash/Default-Landscape@2x~ipad.png" width="2048"/>
    <splash height="2048" src="resources/ios/splash/Default-Landscape@~ipadpro.png" width="2732"/>
    <splash height="768" src="resources/ios/splash/Default-Landscape~ipad.png" width="1024"/>
    <splash height="2048" src="resources/ios/splash/Default-Portrait@2x~ipad.png" width="1536"/>
    <splash height="2732" src="resources/ios/splash/Default-Portrait@~ipadpro.png" width="2048"/>
    <splash height="1024" src="resources/ios/splash/Default-Portrait~ipad.png" width="768"/>
    <splash height="960" src="resources/ios/splash/Default@2x~iphone.png" width="640"/>
    <splash height="480" src="resources/ios/splash/Default~iphone.png" width="320"/>
    <splash height="2732" src="resources/ios/splash/Default@2x~universal~anyany.png" width="2732"/>
    <splash src="resources/ios/splash/Default@2x~ipad~anyany.png" />
    <splash src="resources/ios/splash/Default@2x~iphone~anyany.png" />
    <splash src="resources/ios/splash/Default@3x~iphone~anyany.png" />
  </platform>
</widget>

    package.json:
--- Start of Cordova JSON Snippet ---
{
  "platforms": [
    "ios"
  ],
  "plugins": {
    "cordova-plugin-dialogs": {},
    "cordova-plugin-inappbrowser": {},
    "cordova-plugin-splashscreen": {},
    "cordova-plugin-geolocation": {
      "GPS_REQUIRED": "true"
    },
    "cordova-plugin-keyboard": {},
    "cordova-plugin-localization-strings": {
      "TRANSLATION_PATH": "translations/app/"
    },
    "@havesource/cordova-plugin-push": {
      "IOS_FIREBASE_MESSAGING_VERSION": "10.24.0"
    },
    "cordova-plugin-firebase-analytics": {
      "ANALYTICS_COLLECTION_ENABLED": "true",
      "AUTOMATIC_SCREEN_REPORTING_ENABLED": "true",
      "IOS_FIREBASE_POD_VERSION": "10.24.0"
    },
    "cordova-plugin-taptic-engine": {}
  }
}
--- End of Cordova JSON Snippet ---

Sample Push Data Payload - NotificationEventResponse

{
  "message": "example message",
  "title": "example title",
  "additionalData": {
    "foreground": false,
    "coldstart": false,
    "notification_id": "2bf6055c-1355-45aa-a494-efb286a1a8ac",
    "notification_name": "foo",
    "senders": {
      "123456789": {
        "target_name": "EXAMPLE",
        "target_id": "EXAMPLE-ID-123456789"
      }
    }
  }
}

Sample Code that illustrates the problem

push.on('notification', (data) => {
  if (data.additionalData.coldstart) {
    // do stuff only if user click notification while app is closed
  }
});

Logs taken while reproducing problem

geoidesic commented 3 days ago

I can confirm this behaviour. coldstart is false if the app is closed when a push notification for the app is clicked.

erisu commented 2 days ago

@lorenzodianni @geoidesic I pushed PR #319, which should resolve the issue where coldstart is always false.

The root cause appears to be that when the app becomes active and launchNotification is set, the notificationMessage property is updated to match launchNotification. However, isInline and coldstart are also reset to NO before the message is processed in the notificationReceived method.

The notificationReceived method is responsible for creating the JSON object that is returned to the front end.

By the time the process reaches notificationReceived, the native properties isInline (foreground) and coldstart (coldstart) have already been reset. As a result, they always return false.