EddyVerbruggen / nativescript-plugin-firebase

:fire: NativeScript plugin for Firebase
https://firebase.google.com
MIT License
1.01k stars 445 forks source link

FCM token is in a wrong format for iOS #1520

Open gsarme opened 4 years ago

gsarme commented 4 years ago

Hi @EddyVerbruggen, I wrote my app with Nativescript + Angular. I want to send push notification to my device retrieving the push token from firebase and send the notification from my service. Before upgrading to iOS 13 the notifications were sent and received correctly. I searched for the problem and I found that the push token for iOS is in this format

{length=32,bytes=0x10fff92318d1f2e6785440f6168e56ad...00d3405325aa0620}

How can I retrieve a correct push token? I tried to convert 0x10fff92318d1f2e6785440f6168e56ad...00d3405325aa0620 to base64 but it's not the solution because I haven't a correct base64 string

And if it's possible to gain the correct token , how can I check if the device has iOS 12 or iOS 13? The version of Nativescript is 6.2.2

gsarme commented 4 years ago

I solved the problem doing: tns remove plugin nativescript-plugin-firebase tns add plugin nativescript-plugin-firebase tns remove platform ios Then I run npm run config under node_modules/nativescript-plugin-firebase

Now I'm facing another issue, the push token that I receive is a firebase token and not an APNs token so, if I try to send a push notification via feedback.sandbox.push.apple.com I receive nothing, and if I try to send the notification via firebase I get success:1 from the response but the device displays nothing.

EddyVerbruggen commented 4 years ago

Can you share your repo so I can take a quick look?

gsarme commented 4 years ago

@EddyVerbruggen sorry, but I can't share the repo because it's not an open source project but I can show you some parts of the code and give you more information.

I'm using the 6.2.2 version of NativeScript with Angular.

In my app.component.ts I have:


firebase.init({
          showNotifications: true,
          showNotificationsWhenInForeground: true
      });
      messaging.registerForPushNotifications({
        onPushTokenReceivedCallback: (token: string): void => {
          console.log("Firebase plugin received a push token: " + token);
          localStorage.setItem("fcm_token", token);
          if(utils.ios){
            console.log("ios");
            localStorage.setItem("device_type", "ios");
          }else{
            console.log("android");
            localStorage.setItem("device_type", "android");
          }
        },

        onMessageReceivedCallback: (message: Message) => {
          console.log("Push message received: " + message.title);
        },

        // Whether you want this plugin to automatically display the notifications or just notify the callback. Currently used on iOS only. Default true.
        showNotifications: true,

        // Whether you want this plugin to always handle the notifications when the app is in foreground. Currently used on iOS only. Default false.
        showNotificationsWhenInForeground: true
      }).then(() => console.log("Registered for push"));
    }

This is my firebase.nativescript.json:

{
    "using_ios": true,
    "using_android": true,
    "analytics": false,
    "firestore": false,
    "realtimedb": false,
    "authentication": false,
    "remote_config": false,
    "performance_monitoring": false,
    "external_push_client_only": true,
    "messaging": true,
    "in_app_messaging": false,
    "crashlytics": false,
    "storage": false,
    "functions": false,
    "facebook_auth": false,
    "google_auth": false,
    "admob": false,
    "dynamic_links": false,
    "ml_kit": false
}

In my package.json I have "nativescript-plugin-firebase": "^10.3.2" In the Info.plist file:

        <key>UIBackgroundModes</key>
    <array>
        <string>remote-notification</string>
    </array>
    <key>UseExternalPushProvider</key>
    <true/>

Here is my [app-name].entitlements

<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE plist PUBLIC "-//Apple//DTD PLIST 1.0//EN" "http://www.apple.com/DTDs/PropertyList-1.0.dtd">
<plist version="1.0">
<dict>
    <key>aps-environment</key>
    <string>development</string>
</dict>
</plist>

When I execute tns run ios the console gives this error:

[path-to-my-app]/hooks/after-watch/nativescript-dev-webpack.js will NOT be executed because it has invalid arguments

During the night I update the device and now has iOS 13.3

The token that I receive are like this: ewq1APiAA4k:APA91bEVXY8YV5mJRcE9BLaTXS7YCNK24nyhjK4g2aBE3T4RbfdyzNr71wZyVl_NGW5dweJH710SmNedFT1BhkplZX64lKb9RMLD5TQj6msEX7YF2t_Cvu41Wt9I8lU8dgcFijfvq8Pf

Do you need more infos?

EddyVerbruggen commented 4 years ago

These lines in firebase.nativescript.json are mutually exclusive: "external_push_client_only": true, "messaging": true

With external_push_client_only you would get an APNS token, and with messaging an FCM token. Please delete the file and run npm i again to restart the configurator.

gsarme commented 4 years ago

@EddyVerbruggen I didi what you said but now I have this error:

CONSOLE ERROR file:///node_modules/@angular/core/fesm5/core.js:5847:0: ERROR Error: Uncaught (in promise): ReferenceError: Can't find variable: FIROptions file:///node_modules/nativescript-plugin-firebase/firebase.js:279:0 at ZoneAwarePromise(file:///node_modules/nativescript-angular/zone-js/dist/zone-nativescript.js:902:0) at init(file:///node_modules/nativescript-plugin-firebase/firebase.js:261:0) at ngOnInit(file:///src/app/app.component.ts:34:20) at checkAndUpdateDirectiveInline(file:///node_modules/@angular/core/fesm5/core.js:19327:0) at prodCheckAndUpdateNode(file:///node_modules/@angular/core/fesm5/core.js:28088:0) at at checkAndUpdateView(file:///node_modules/@angular/core/fesm5/core.js:27529:0) at detectChanges(file:///node_modules/@angular/core/fesm5/core.js:18916:0) at tick(file:///node_modules/@angular/core/fesm5/core.js:25185:0) at _loadComponent(file:///node_modules/@angular/core/fesm5/core.js:25240:0) at bootstrap(file:///node_modules/@angular/core/fesm5/core.js:25157:0) at forEach([native code]) at _moduleDoBootstrap(file:///node_modules/@angular/core/fesm5/core.js:24872:0) at file:///node_modules/@angular/core/fesm5/core.js:24840:0 at onInvoke(file:///node_modules/@angular/core/fesm5/core.js:24337:0) at run(file:///node_modules/nativescript-angular/zone-js/dist/zone-nativescript.js:138:0) at file:///node_modules/nativescript-angular/zone-js/dist/zone-nativescript.js:883:0 at onInvokeTask(file:///node_modules/@angular/core/fesm5/core.js:24328:0) at runTask(file:///node_modules/nativescript-angular/zone-js/dist/zone-nativescript.js:188:0) at drainMicroTaskQueue(file:///ap\M-b\M^@\M-&

This is my firebase.nativescript.json now:

{
    "using_ios": true,
    "using_android": true,
    "analytics": false,
    "firestore": false,
    "realtimedb": false,
    "authentication": false,
    "remote_config": false,
    "performance_monitoring": false,
    "external_push_client_only": true,
    "messaging": false,
    "in_app_messaging": false,
    "crashlytics": false,
    "storage": false,
    "functions": false,
    "facebook_auth": false,
    "google_auth": false,
    "admob": false,
    "dynamic_links": false,
    "ml_kit": false
}

Do you have any ideas why?

UPDATE I receive more than one correct push token after a while.

EddyVerbruggen commented 4 years ago

Perhaps try rm -rf platforms node_modules before rebuilding.

gsarme commented 4 years ago

I deleted the node_modules, platforms, hooks folders more than one time and then it worked. Thanks!

BoHuCB commented 4 years ago

Hi @EddyVerbruggen and @gsarme. I am following this thread and I have had the same issue. I believe I was getting FCM token (more than 100 characters long). After making the change with your information, now I am getting a token as "63c7b3a33252777eb58055d3a4b804ef6177dca64a1f7bfcb8a8722fa1dec54c". Then I tried to use this token to send push notifications on firebase cloud messaging, I can't never get any notifications. But when I try the same thing on Android it works. Do you guys have any idea why IOS wouldn't work?

EddyVerbruggen commented 4 years ago

@BoHuCB that looks like an APNs token. Try a tool like Pusher to send a message to your device.

BoHuCB commented 4 years ago

@EddyVerbruggen Thanks for the quick response. I download Pusher but the company laptop doesn't allow me to use it since it is not from app store. But I guess, would FCM work if I use FCM to send a push notification by APNs token?

EddyVerbruggen commented 4 years ago

AFAIK you can't use an APNs token with FCM.

BoHuCB commented 4 years ago

@EddyVerbruggen That's what I figured. With this confirmation I think I just need to find a way to send the push notification to APNs to test it. Thank you very much for the responses.

gsarme commented 4 years ago

@BoHuCB if you can't use Pusher, try this pushtry. This helped me.