question-mark
Stuck on an issue?

Lightrun Answers was designed to reduce the constant googling that comes with debugging 3rd party libraries. It collects links to all the places you might be looking at while hunting down a tough bug.

And, if you’re still stuck at the end, we’re happy to hop on a call to see how we can help out.

[iOS] how to access the APNs Token

See original GitHub issue

Question

Is there any method I can call to access the APNs token?

While testing on the simulator, requesting permissions after a hard device reset, onRegister does not appear to be called, and invoking requestPermissions only returns an object containing permissions. My question is:

What method(s), if any, can I call on a simulator or device to reliably return the APNs token once it exists, and what form do those return values take? And is there any way to access the token outside of onRegister?

e.g. something like requestPermissions().then(results => console.log('token: ', results.token))

Thanks!

Issue Analytics

  • State:open
  • Created 3 years ago
  • Reactions:3
  • Comments:14

github_iconTop GitHub Comments

5reactions
santiagogarzacommented, Oct 15, 2020

+1 How do we get the token sometime after onRegister gets triggered?

What if a user denies the system prompt and then later enables push notifications in settings? How do we get the token then? Or what if registering the token with the push notification provider fails? There must be a getToken method we can call at any point, right?

The Apple documentation recommends we get the token each time as opposed to storing it:

Important

Never cache device tokens in local storage. APNs issues a new token when the user restores a device from a backup, when the user installs your app on a new device, and when the user reinstalls the operating system. If you ask the system to provide the token each time, you are guaranteed to get an up-to-date token.

4reactions
th0r0nd0rcommented, Oct 20, 2020

Thanks for the answers, everyone.

@vinagrito-getpocketful my understanding and the testing I’ve done so far lead me to believe that onRegister is only invoked after allowing permissions from the system prompt, or allowing them from settings for the first time if the prompt was denied. We are currently calling PushNotificaion.configure on every app open, which does allow us to define a new, arbitrary callback to pass to the onRegister method, but it doesn’t force onRegister to fire and give access to the token.

So that answers @santiagogarza’s first question, however we’ve just tested and found that if the initial onRegister method fails due to network issues, it will never fire again. Maybe there’s a way to solve this with onRegistrationError or some other method but I don’t know when that method will fire so I’ll have to do more testing. As of now it seems there is no way to access the push token if the initial call to onRegister fails.

Read more comments on GitHub >

github_iconTop Results From Across the Web

Registering Your App with APNs - Apple Developer
Register your app with APNs and receive a globally unique device token, which is effectively the address of your app on the current...
Read more >
Get device token for push notification - ios - Stack Overflow
To get Token Device you can do by some steps: 1) Enable APNS (Apple Push Notification Service) for both Developer Certification and ...
Read more >
how to get iOS Device token(APNs Token)? - OutSystems
In your implementation of this method, send the contents of the deviceToken parameter to the server that you use to generate remote ...
Read more >
How to start using APNS Token-Based Authentication.
Go to Certificates, Identifiers & Profiles . · Click on Keys from the Menu on the left. · Add a new key -...
Read more >
Token-based authentication for iOS applications with ... - AWS
Any iOS application that is capable of receiving push notifications must register with APNs. Upon successful registration, APNs returns a device ...
Read more >

github_iconTop Related Medium Post

No results found

github_iconTop Related StackOverflow Question

No results found

github_iconTroubleshoot Live Code

Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free

github_iconTop Related Reddit Thread

No results found

github_iconTop Related Hackernoon Post

No results found

github_iconTop Related Tweet

No results found

github_iconTop Related Dev.to Post

No results found

github_iconTop Related Hashnode Post

No results found