Skip to content

iOS: FCM registration token not received the first time the app is started #357

@dpalou

Description

@dpalou

Bug Report

We're using FCM in iOS instead of just APNS. The first time the app is launched in a device, the FCM token is not obtained. If you restart the app, then the token is obtained correctly.

I've added some debugging and I saw that, when calling requestAuthorizationWithOptions, the callback always receives granted=false even if the user has accepted the notifications permission. I'm not using forceRegister, so registerForRemoteNotifications isn't called and the FCM token is not obtained.

If I restart the app, getNotificationSettingsWithCompletionHandler detects that the permission has been granted and then it registers for remote notifications, correctly obtaining the FCM token.

I'm testing this with an iPad using iPadOS 18.1.1. I tried updating it to iPadOS 18.6, but I have the same problem.

We've been using the 4.0.0 version of the plugin for a long time, and it works fine in that version. With that version, requestAuthorizationWithOptions also returns granted=false, but after that the event pushPluginApplicationDidBecomeActiveNotification is triggered, which calls handleNotificationSettings, and this second checks detects that the permission has been granted already and registers for remote notifications.

Expected Behaviour

The app should correctly obtain the FCM registration token when the user grants the permission.

Actual Behaviour

Even if the user grants the permission, the FCM token is not obtained unless the user restarts the app.

Reproduce Scenario (including but not limited to)

Steps to Reproduce

  1. Configure an app to use FCM in iOS.
  2. In the app's code, after the app is ready, call PushNotification.init with the following options:
{
    ios: {
        alert: true,
        badge: true,
        sound: true,
   }
}
  1. Install the app in an iOS device and check the log in XCode.
  2. In the app, when prompted for notifications, click "Allow".
  3. In the XCode console you should see the message: [PushPlugin] Notification permissions denied.

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

iPadOS 18.1.1 and iPadOS 18.6

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

cordova info Printout

Cordova Packages:

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

Project Installed Platforms:

ios: 7.1.1

Project Installed Plugins:

cordova-clipboard: 1.3.0
cordova-plugin-add-swift-support: 2.0.2
cordova-plugin-androidx-adapter: 1.1.3
cordova-plugin-badge: 0.8.9
cordova-plugin-chooser: 1.3.1
cordova-plugin-customurlscheme: 5.0.2
cordova-plugin-device: 2.1.0
cordova-plugin-file: 8.1.3
cordova-plugin-local-notification: 1.0.0
cordova-plugin-moodleapp: 0.0.0
cordova-plugin-network-information: 3.0.0
cordova-plugin-prevent-override: 1.0.1
cordova-plugin-screen-orientation: 3.0.4
cordova-plugin-wkuserscript: 1.0.1
cordova-plugin-wkwebview-cookies: 1.0.1
cordova-sqlite-storage: 7.0.0
nl.kingsquare.cordova.background-audio: 1.0.1
@havesource/cordova-plugin-push: 6.0.1
@moodlehq/cordova-plugin-advanced-http: 3.3.1-moodle.1
@moodlehq/cordova-plugin-camera: 7.0.0-moodle.1
@moodlehq/cordova-plugin-file-opener: 4.0.0-moodle.1
@moodlehq/cordova-plugin-file-transfer: 2.0.0-moodle.2
@moodlehq/cordova-plugin-inappbrowser: 6.0.0-moodle.1
@moodlehq/cordova-plugin-intent: 2.2.0-moodle.3
@moodlehq/cordova-plugin-ionic-keyboard: 2.2.0-moodle.3
@moodlehq/cordova-plugin-ionic-webview: 5.0.0-moodle.5
@moodlehq/cordova-plugin-media-capture: 5.0.0-moodle.1
@moodlehq/cordova-plugin-qrscanner: 3.0.1-moodle.5
@moodlehq/cordova-plugin-statusbar: 4.0.0-moodle.5
@moodlehq/cordova-plugin-zip: 3.1.0-moodle.1
@totalpave/cordova-plugin-insets: 0.3.2

Environment:

OS: Sequoia 15.5 (24F74) (darwin 24.5.0) arm64
Node: v22.17.0
npm: 10.9.2

ios Environment:

xcodebuild:

Xcode 16.2
Build version 16C5032a

Project Setting Files:

   package.json:
--- Start of Cordova JSON Snippet ---
{
  "platforms": [
    "android",
    "ios"
  ],
  "plugins": {
    "@moodlehq/cordova-plugin-advanced-http": {
      "ANDROIDBLACKLISTSECURESOCKETPROTOCOLS": "SSLv3,TLSv1"
    },
    "@moodlehq/cordova-plugin-camera": {
      "ANDROIDX_CORE_VERSION": "1.6.+"
    },
    "@moodlehq/cordova-plugin-file-opener": {},
    "@moodlehq/cordova-plugin-file-transfer": {},
    "@moodlehq/cordova-plugin-inappbrowser": {},
    "@moodlehq/cordova-plugin-intent": {},
    "@moodlehq/cordova-plugin-ionic-keyboard": {},
    "@moodlehq/cordova-plugin-ionic-webview": {},
    "@moodlehq/cordova-plugin-media-capture": {},
    "@moodlehq/cordova-plugin-qrscanner": {},
    "@moodlehq/cordova-plugin-statusbar": {},
    "@moodlehq/cordova-plugin-zip": {},
    "@totalpave/cordova-plugin-insets": {},
    "cordova-clipboard": {},
    "cordova-plugin-androidx-adapter": {},
    "cordova-plugin-badge": {},
    "cordova-plugin-chooser": {},
    "cordova-plugin-customurlscheme": {
      "URL_SCHEME": "moodlemobile",
      "ANDROID_SCHEME": " ",
      "ANDROID_HOST": " ",
      "ANDROID_PATHPREFIX": "/"
    },
    "cordova-plugin-device": {},
    "cordova-plugin-local-notification": {
      "ANDROID_SUPPORT_V4_VERSION": "26.+"
    },
    "cordova-plugin-moodleapp": {
      "ANDROIDX_VERSION": "1.0.0",
      "ANDROIDX_APPCOMPAT_VERSION": "1.3.1"
    },
    "cordova-plugin-network-information": {},
    "cordova-plugin-prevent-override": {},
    "cordova-plugin-screen-orientation": {},
    "cordova-plugin-wkuserscript": {},
    "cordova-plugin-wkwebview-cookies": {},
    "cordova-sqlite-storage": {},
    "nl.kingsquare.cordova.background-audio": {},
    "@havesource/cordova-plugin-push": {
      "IOS_FIREBASE_MESSAGING_VERSION": "10.24.0"
    }
  }
}
--- End of Cordova JSON Snippet ---

Logs taken while reproducing problem

[PushPlugin] FCM is enabled and Sender ID is available.
[PushPlugin] Configuring Firebase App for FCM
[PushPlugin] pushPluginOnApplicationDidBecomeActive
[PushPlugin] application first launch: remove badge icon number
[PushPlugin] skip clear badge
[PushPlugin] Settings: {
    alert = 1;
    badge = 1;
    categories = "{(\n)}";
    clearBadge = 0;
    critical = 0;
    fcmTopics =     (
    );
    forceRegister = 0;
    forceShow = 0;
    sound = 1;
    voip = 0;
}
[PushPlugin] VoIP missing or false
[PushPlugin] register called
[PushPlugin] Notification permissions denied.
[PushPlugin] pushPluginOnApplicationDidBecomeActive
[PushPlugin] skip clear badge

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions