Nativescript-plugin-firebase: Not receiving device token

Created on 21 Nov 2018  路  34Comments  路  Source: EddyVerbruggen/nativescript-plugin-firebase

Using with Vue nativescript.

Am not getting the device token after allowing push notification. This is how my main.js looks like

import { messaging, Message } from "nativescript-plugin-firebase/messaging";

Vue.prototype.$router = router

Vue.prototype.$goto = function (to, options) {
this.$navigateTo(this.$router[to], options)
}

console.log(messaging);

messaging.registerForPushNotifications({
onPushTokenReceivedCallback: (token) => {
console.log("Firebase plugin received a push token: " + token);
},

onMessageReceivedCallback: (message) => {
    console.log("Push message received: " + JSON.stringify(message, getCircularReplacer()));
},
showNotifications: true,
showNotificationsWhenInForeground: true

}).then(() => console.log("Registered for push"));

messaging.getCurrentPushToken()
.then(token => console.log(Current push token: ${token}));

[org.nativescript.events-mobi] Requested authorization [ didGrant: 1 hasError: 0 hasCompletionHandler: 1 ]
[org.nativescript.events-mobi] Requesting token for remote notifications
[org.nativescript.events-mobi] Requested token for remote notifications [ didSucceed: 1 hasError: 0 hasCompletionHandler: 1 ]
[org.nativescript.events-mobi] Did receive remote notification token
[org.nativescript.events-mobi] Sending received device token to delegate UIApplication
HTHangEventCreate: HangTracing is disabled. Not creating a new event.
removing all entries config 0x280898840
Task .<1> resuming, QOS(0x19)
TIC TCP Conn Start [4:0x283dd3240]

Messaging bug iOS

Most helpful comment

Damnit, I forgot to document quite an important piece of the puzzle to make this work (sorry!):

https://github.com/EddyVerbruggen/nativescript-plugin-firebase/blob/d6cc3cc91bd71800b1b7e9d75460682515e58ce4/demo-push/app_resources/iOS/Info.plist#L50-L51

You can try adding that to your project and it'll work. I'm currently considering the best way to either automate or document this.

@rickybrown In your case you also need to add this to main.ts so the iOS appdelegate can be wired correctly (this bit is actually documented, lol) - I've sent a PR to your repo:

require("nativescript-plugin-firebase");

All 34 comments

same problem - no matter what i do, onPushTokenReceivedCallback doesn't fire and getCurrentPushToken always returns undefined

+1

Share a decent repo and I鈥檒l take a look.

Hey @EddyVerbruggen , I'm also running into the same issue as above (got the same logs too) where my app does not seem to get the token (it's undefined).

I'm following this section where I only aim to use the plugin for push notifications for both iOS and Android, and not for other things: https://github.com/EddyVerbruggen/nativescript-plugin-firebase/blob/master/docs/NON_FIREBASE_MESSAGING.md

As the repo is private, I have invited you into the repo. The branch where I added notifications are under the push-notif branch.

Really appreciate any advice on this!

Last but not least I just want to thank you so so much for all the open source work you did on plugins. I'm also using the IQKeyboard which works awesome!

@EddyVerbruggen after trying out to install the whole firebase (but only using messaging) rather than follow the steps of the non-firebase-messaging, I'm now able to get the device token as well as notifications!

I guess perhaps there is something off with the non-firebase-messaging section, or maybe I misunderstood it.

I would recommend everyone here who has that issue to try that out too instead of using the steps of the non-firebase-messaging section.

Yeah noticed that as well. When you go through the NO FIREBASE installation steps you never get a token back. It seems to only work with the cloud messaging set to true in the firebase.nativescript.json file.

I can confirm what @jefflam and @afaridanquah said, doing it this way gets me a token, but unfortunately not the correct token. I'm on iOS, the token should be shorter. I use AWS SNS and it does not accept it. I had to start using the deprecated push-plugin just to get other pieces set up in my app until I can figure out this plugin the switch back over

Guys, please share a repo so I take a look what鈥檚 going on. I use the push demo in this repo to test and don鈥檛 have this issue (at least, not the last time I checked).

will create a fresh repo, try notifications, and share - just a min

here is a fresh repo, following the instructions in NON_FIREBASE_MESSAGING.md:
https://github.com/rickybrown/ns-angular-push

here are my logs, you'll see on the fourth line down the token is undefined:

CONSOLE LOG file:///app/tns_modules/@angular/core/bundles/core.umd.js:13657:24: Angular is running in the development mode. Call enableProdMode() to enable the production mode.
CONSOLE LOG file:///app/app/app.component.js:29:24: Registered for push
CONSOLE LOG file:///app/app/app.component.js:35:24: Current push token: undefined
[io.non.burnerchat] Requested authorization [ didGrant: 0 hasError: 0 hasCompletionHandler: 1 ]
[io.non.burnerchat] Requesting token for remote notifications
[io.non.burnerchat] Requested token for remote notifications [ didSucceed: 1 hasError: 0 hasCompletionHandler: 1 ]
[io.non.burnerchat] Requested authorization [ didGrant: 1 hasError: 0 hasCompletionHandler: 1 ]
[io.non.burnerchat] Requesting token for remote notifications
[io.non.burnerchat] Requested token for remote notifications [ didSucceed: 1 hasError: 0 hasCompletionHandler: 1 ]
[io.non.burnerchat] Did receive remote notification token
[io.non.burnerchat] Sending received device token to delegate UIApplication
[io.non.burnerchat] Did receive remote notification token
[io.non.burnerchat] Sending received device token to delegate UIApplication
HTHangEventCreate: HangTracing is disabled. Not creating a new event.

I'm running it on an iPhone X - same phone and bundle ID that the old push plugin works on

Thanks, @rickybrown, that's highly appreciated. I'll look into this today.

Damnit, I forgot to document quite an important piece of the puzzle to make this work (sorry!):

https://github.com/EddyVerbruggen/nativescript-plugin-firebase/blob/d6cc3cc91bd71800b1b7e9d75460682515e58ce4/demo-push/app_resources/iOS/Info.plist#L50-L51

You can try adding that to your project and it'll work. I'm currently considering the best way to either automate or document this.

@rickybrown In your case you also need to add this to main.ts so the iOS appdelegate can be wired correctly (this bit is actually documented, lol) - I've sent a PR to your repo:

require("nativescript-plugin-firebase");

Opted for updating the docs - I don't want the plugin to mess with your plist file. Users already had to add something to the plist anyway, so adding another line shouldn't hurt. Docs updated.

Note that the reason for this plist property is that the plugin has to wire the iOS app delegate before your app starts (that's why you also need to require the plugin before the app starts).

At that point in the app's lifecycle (so before you can call registerForPushNotifications) the plugin has no other means to determine whether or not you want to use push notifications. When using Firebase Messaging (instead of an external push provider) the plugin can check at runtime whether or not you have the FIRMessaging library available, but when using an external pust provider that library is deliberately not loaded. So the plugin queries your bundle (plist) to see if a certain property is set, and only in that case push notifications will be enabled.

Awesome! I actually did require the plugin in my real repo, just forgot to on this test one. I鈥檒l be able to try adding the key to my info.plist in a couple hours to see if it works.

bingo, I can confirm this works. I'm able to get the token, and also receiving push notifications when the app is closed. I have a separate issue which is odd - not able to receive "foreground" notifications. What happens is, when the app is open the push notification displays as if it's a background/remote notification. Never experience that before so didn't know it was possible. normally I receive a foreground notification and handle the alert in my own custom way. I did notice in my console I'm getting Can't find variable: FIRMessaging. Any direction would be appreciated, otherwise I'll start reading up on other issues. Thanks again!

@rickybrown Thanks for testing! And set this to false to fix the foreground notification issue on iOS.

doh! literally read that, and said to myself it was set correctly... doing too many things at once. my apologies. Thanks!

Hi @EddyVerbruggen getting some long Token like this Current device token: eaZOn5hnx2s:APA91bFJ6cwW1W5t70Q1dyfHAOv164DxLRnJ7mJQEAjZRa_QXKyHZKjp47sfRADJQvLLw2gqMKehrnbrDAilPHtQY6sPVQq61Egza5DoVQhI_kWQHMRzsge4_BBBBB_mi_5

instead of something like this 06da8e33fc0d97240c49b7d03b804f871da9ed7051cd8133dc2bc22a2abc722
Can you please take a look ?

@afaridanquah are you using firebase for your app? If not, can you confirm that your json looks like this:

{
    "external_push_client_only": true,
    "using_ios": true,
    "using_android": true
}

and that you are not using firebase.init?

I ask, because I made that mistake the first time I tried this plugin and received a long device token like that as well. So in that case you would want to follow these instructions:
https://github.com/EddyVerbruggen/nativescript-plugin-firebase/blob/master/docs/NON_FIREBASE_MESSAGING.md

I tried both Cloud and NON FIREBASE. Can you check your package.json and confirm the the version you are running?

I've only tried versions 7.4.2 and 7.4.3 and they both work. Try to run the test repo here:
https://github.com/rickybrown/ns-angular-push

(remember to replace org.nativescript.nsangularpush with your bundle id)

Yeah i did that, not sure what is making me get that kind of token, i will try and start the process in a new project. Thanks @rickybrown

Hi,

So I've tried everything that's been mentioned here and i'm still having the same issue: the token is always "undefined". I configured the package to only use notifications:

{
    "external_push_client_only": true,
    "using_ios": true,
    "using_android": true
}

But when I add require('nativescript-plugin-firebase') before my app starts, I get the GoogleService-Info.plist missing error. If I don't require it, the token is always undefined.

Please note that I'm using Nativescript-Vue, my package version is 7.6.1 (though I've tried with different versions, including the one in @rickybrown 's repo) and I'm using Sidekick's cloud build (though I've also tried using XCode on my mac and got the same result).

If anyone has an idea, it'd be really appreciated :)

@yassipad Share a repo please.

For anyone reading this, the issue could also be another plugin overriding the app delegate.
See this issue I made in another plugin: https://github.com/mkloubert/nativescript-social-login/issues/69
It also contains a fix.

Hi @jerbob92 and thanks for the advice :) I managed to get it to work by removing the social-login plugin entirely, so it is definitely a conflict between those two. How did you get both working at the same time? What version of nativescript-social-login are you using?

Thanks :)

@yassipad You can find the fix in https://github.com/mkloubert/nativescript-social-login/issues/69
The trick is to use that piece of code instead of the code given in the readme ( https://github.com/mkloubert/nativescript-social-login#typescript )

Also make sure you do that piece of code after the wiring of Firebase. (const firebase = require("nativescript-plugin-firebase");)

thanks for the quick answer! i did exactly that, however, when i do, i always get the "invalid GoogleService-Info.plist" error...

Hi @yassipad, I think that error is unrelated to the bug I had.

well, i don't get that error without the nativescript-social-plugin so that's why i was asking for the version you're using

Firebase 7.6.0
Social-login 4.0.1

Hi eveyone in ios onPushTokenReceivedCallback is not caaling so, token is undefined

messaging.registerForPushNotifications({
onPushTokenReceivedCallback: (token: string): void => {
console.log("Firebase plugin received a push token: " + token);
},

    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"));

I ran into the same issue. For my situation, the device token was always undefined but everything else was working. I was adding push notifications after installation, so I needed to add the push notifications feature. It turns out, I needed to do a full reconfigure of the nativescript-plugin-firebase plugin, rather than what is suggested in the documentation.

@EddyVerbruggen, the documentation says:

If you didn't choose this feature during installation you can add "messaging": true to /firebase.nativescript.json and remove the /platforms folder.

But, this does not seem to modify the podfile, so it's incorrectly configured during build. I needed to follow the steps here: https://github.com/EddyVerbruggen/nativescript-plugin-firebase#config

Hope this helps someone!

I finally got the token, you need to set "messaging": false and test your app in a real device, because with the simulator the onPushTokenReceivedCallback is never called and the getCurrentPushToken returns undefined

Was this page helpful?
0 / 5 - 0 ratings