简体   繁体   中英

Cordova-Plugin-Firebase, iOS: device receives push notification but does not appear on device

Testing Cordova Firebase plugin: working on Android; iOS, no such luck...

I've created the necessary Ids, Certificates and private keys in my Developer account and on my Mac and set-up the "iOS" app in the Firebase Console. FYI: In the Cloud Messaging config in Firebase I am using the APN Auth Key method and not a dev certificate as Firebase indicates a preference for the Auth Key method.

So I have created a new Cordova app - imaginatively called "pushnotificationsdemo", here are the steps I have used to create the App, configure the FCM and test it:

Create a Cordova app using Firebase plugin:

cordova —version

8.1.2 (cordova-lib@8.1.1)

cordova pushnotificationsdemo com.XXX.pushnotifications.com “Push Notifications Demo”
cd push*
cordova plugin add cordova-plugin-firebase

[copy the “GoogleService-Info.plist” downloaded from the Firebase Console into the new Cordova project's root folder]

cordova platform add ios

*Using cordova-fetch for cordova-ios@~4.5.4

Adding ios project...

Creating Cordova project for the iOS platform:

Path: platforms/ios

Package: com.foliagos.pushnotificationsdemo

Name: Push Notifications Demo

iOS project created with cordova-ios@4.5.5

Installing "cordova-plugin-firebase" for ios

Discovered plugin "cordova-plugin-whitelist" in config.xml. Adding it to the project

Installing "cordova-plugin-whitelist" for ios

Adding cordova-plugin-whitelist to package.json

Saved plugin info for "cordova-plugin-whitelist" to config.xml

Preparing Firebase on iOS

--save flag or autosave detected

Saving ios@~4.5.5 into config.xml file ...*


Open iOS project in Xcode ( version 10.1 ):

  • Set Signing Team in “General” (to stop the Code 65 error when I attempt to build the app using the Cordova command-line)

I am using Xcode10, so while I am there I also set the build type to the “legacy” system (because Cordova 8.1.2 is not compatible with the new build system):

  • File | Workspace Settings

  • Build System: "Legacy Build System"

And because I want to see a lot of debugging output I also set this in Xcode:

  • Product | Scheme | Edit Scheme | tab: Arguments —> Arguments Passed On Launch, add this: “-FIRAnalyticsDebugEnabled”

And added the following into the “Push Notifications Demo-Info.plist” file in the “Resources” project folder:

  • “UIBackgroundModes”

  • with the following key: “remote-notification”

I also added in the following preference into Cordova's config.xml to turn off Cloud backup:

<preferencename="BackupWebStorage"value="none"/>

Now I build the new Cordova app from the command-line (using the legacy build system):

cordova build ios --buildFlag='-UseModernBuildSystem=0'

The archive is built successfully!

Back in Xcode, I build the app and the following is written to the Console:


Nov 8 17:50:54 Push Notifications Demo[414] : - [I-ACS036002] Analytics screen reporting is enabled. Call +[FIRAnalytics setScreenName:setScreenClass:] to set the screen name or override the default screen class name. To disable screen reporting, set the flag FirebaseScreenReportingEnabled to NO (boolean) in the Info.plist

2018-11-08 17:50:54.312 Push Notifications Demo[414:66091] DiskCookieStorage changing policy from 2 to 0, cookie file: file:///private/var/mobile/Containers/Data/Application/8AC0457B-0792-4A8F-8FFA-25655DB5DD8A/Library/Cookies/Cookies.binarycookies

2018-11-08 17:50:54.558 Push Notifications Demo[414:66091] Apache Cordova native platform version 4.5.5 is starting.

2018-11-08 17:50:54.558 Push Notifications Demo[414:66091] Multi-tasking -> Device: YES, App: YES

2018-11-08 17:50:54.691 Push Notifications Demo[414:66091] Using UIWebView

2018-11-08 17:50:54.697 Push Notifications Demo[414:66091] [CDVTimer][console] 0.401020ms

2018-11-08 17:50:54.698 Push Notifications Demo[414:66091] [CDVTimer][handleopenurl] 0.479937ms

2018-11-08 17:50:54.707 Push Notifications Demo[414:66091] [CDVTimer][intentandnavigationfilter] 8.823991ms

2018-11-08 17:50:54.708 Push Notifications Demo[414:66091] [CDVTimer][gesturehandler] 0.429988ms

2018-11-08 17:50:54.708 Push Notifications Demo[414:66091] Starting Firebase plugin

2018-11-08 17:50:54.709 Push Notifications Demo[414:66091] [CDVTimer][firebaseplugin] 0.813961ms

2018-11-08 17:50:54.709 Push Notifications Demo[414:66091] [CDVTimer][TotalPluginStartup] 13.291001ms

2018-11-08 17:50:54.739 Push Notifications Demo[414:66091] GoogleService-Info.plist found, setup: [FIRApp configureWithOptions]

2018-11-08 17:50:54.802 Push Notifications Demo[414:66091] [Crashlytics] Version 3.10.7 (130)

Nov 8 17:50:55 Push Notifications Demo[414] : 5.7.0 - [Firebase/Analytics][I-ACS023007] Analytics v.50101000 started

Nov 8 17:50:55 Push Notifications Demo[414] : 5.7.0 - [Firebase/Messaging][I-FCM001000] FIRMessaging Remote Notifications proxy enabled, will swizzle remote notification receiver handlers. If you'd prefer to manually integrate Firebase Messaging, add "FirebaseAppDelegateProxyEnabled" to your Info.plist, and set it to NO. Follow the instructions at:

https://firebase.google.com/docs/cloud-messaging/ios/client#method_swizzling_in_firebase_messaging

to ensure proper integration.

Nov 8 17:50:55 Push Notifications Demo[414] : 5.7.0 - [Firebase/Messaging][I-FCM002023] The object does not respond to -messaging:didReceiveRegistrationToken:. Please implement -messaging:didReceiveRegistrationToken: to be provided with an FCM token.

Nov 8 17:50:55 Push Notifications Demo[414] : 5.7.0 - [Firebase/Analytics][I-ACS023008] To enable debug logging set the following application argument: -FIRAnalyticsDebugEnabled

2018-11-08 17:50:56.008 Push Notifications Demo[414:66091] Resetting plugins due to page load.

2018-11-08 17:50:56.165 Push Notifications Demo[414:66091] Connected to FCM.

2018-11-08 17:50:56.169 Push Notifications Demo[414:66091] InstanceID token: e2Ds2K2cQbI:APA91bE…MqU

2018-11-08 17:50:56.358 Push Notifications Demo[414:66091] Finished load of: file:///var/containers/Bundle/Application/39065AA6-1AEA-4EF5-A4D9-009185D6BE6D/Push%20Notifications%20Demo.app/www/index.html

2018-11-08 17:50:56.373 Push Notifications Demo[414:66153] [Fabric] failed to download settings Error Domain=FABNetworkError Code=-5 "(null)" UserInfo={type=2, request_id=7f2cefa10b4df773f0466752024ec30a, content_type=application/json; charset=utf-8, status_code=403}

2018-11-08 17:50:56.537 Push Notifications Demo[414:66091] Received Event: deviceready


The app is running on the connected iPhone ( iOS version 9.3.5 ) and is displaying the demo Cordova app's “Device Ready” splash screen.

Now I should be able to send a Push Notification to the iPhone running the Cordova app? To do this I go to the Firebase Console and send a Cloud Message (I am using an APNs Auth Key), I send it specifying the Instance token (shown in the previous Console output) in the “Test on device” option, and I get the following output in the Xcode console:


2018-11-08 18:03:31.091 Push Notifications Demo[414:66091] {
    "collapse_key" = "com.XXX.pushnotificationsdemo";
    from = 293106682585;
    notification =     {
        body = "Test notification to iOS device";
        e = 1;
    };
    tap = 0;
}

Great! The device received the notification. BUT I get no notification pop-up on the device itself.

NOTE: the app has not asked permission to use Push Notifications (I did get this request when I was test using the “fcm” plugin)

OK, so this could be because the App is in the foreground? So I push the Home button on the iPhone to put it into the background and I get this in the Xcode console:


2018-11-08 18:07:05.856 Push Notifications Demo[414:66091] Disconnected from FCM


Not so great, the device has now disconnected from FCM!

I bring the app back into the foreground and voila! The console now reports:


2018-11-08 18:08:26.581 Push Notifications Demo[414:66091] Connected to FCM.

2018-11-08 18:08:26.586 Push Notifications Demo[414:66091] InstanceID token: e2Ds2K2cQbI:APA91bE…MqU


So things are not going very well! It appears my Cordova app is not using APN to push notifications to the Device but is instead using Firebase directly (assumption: Firebase cannot process when the App is in the background and APN can, therefore, APN is not being used)

This is my second day on trying to get APN working through the cordova-plugin-firebase (I've also tried the “fcm” plugin and this came up blank as well), needless to say the Cordova Android version worked out of the box.

Anybody any thoughts on what I am doing wrong?


Instructions I have used to set-up Firebase for use with iOS:

https://medium.com/@felipepucinelli/how-to-add-push-notifications-in-your-cordova-application-using-firebase-69fac067e821

At first in the Xcode Preference Settings, under Location Advanced, select the build location to "Unique". This should help you to take builds directly from the Xcode. Secondly in the FCM registration section, place a platform checker for iOS, if yes then seek permission for notifications in iOS. This could be accomplished using.

getDeviceToken() {
    if (this.platform.is('ios')) {
      this.firebase.grantPermission()
        .then((success) => {
          if (success) {
            this.doGetDeviceToken();
          }
        });
    } else {
      this.doGetDeviceToken();
    }
   }

doGetDeviceToken() {
    this.firebase.getToken()
      .then((token) => this.setToken(token))
      .catch((error) => this.handleError(error));
    this.firebase.onTokenRefresh()
      .subscribe((token) => this.setToken(token));
  }

If the above two steps doesn't resolve your problem, then remove and try adding firebase plugins, as well as iOS platforms. It should work.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM