Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

getAPNSToken always returns null except right after registration #510

Closed
willlaserlike opened this issue Aug 16, 2017 · 2 comments
Closed

Comments

@willlaserlike
Copy link
Contributor

I'm using the RN 46 and react-native-fcm 7.5.1. Everything works for me in terms that I can get the fcmToken and I can send push messages to ios devices withe the fcmToken. But I'm having trouble to get the apns token from this library.

I have code like this
FCM.getFCMToken()
.then((token) => {console.log('token '+token);})
FCM.getAPNSToken()
.then((apnstoken) => {console.log('apnstoken '+apnstoken);})

For the first time app runs and receives user's permissions for notification, the apnstoken is a valid string. Once the app is killed and restarted, it always gives undefined value for apnstoken.

@willlaserlike
Copy link
Contributor Author

willlaserlike commented Aug 17, 2017

I found out what happened.

  1. [FIRMessagein messaging].APNSToken is supposed to be used for setting the apns token, not reading it. So it's always empty except right after Firebase set it when Firebase gets a new apns token from the system call. To trigger this system call, we need to call [application registerForRemoteNotifications].

  2. APNSToken is not a NSString. It's a NSData bytes. And it cannot be converted into a string with NSUTF8StringEncoding. We have to convert it into a hex string and then pass back to javascript side.
    Current code (return null even if it has a NSData value)
    resolve([FIRMessaging messaging].APNSToken);
    Working code (return a hex string)
    NSData *apnsTokenData = [FIRMessaging messaging].APNSToken; const char *data = [apnsTokenData bytes]; NSMutableString *token = [NSMutableString string]; for (NSUInteger i = 0; i < [apnsTokenData length]; i++) { [token appendFormat:@"%02.2hhX", data[i]]; } resolve([token copy]);

@evollu
Copy link
Owner

evollu commented Aug 28, 2017

fixed

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

2 participants