Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Get device token with react native

Is there any way to get the device token for notifications on demand with react native? It seems, from the docs, like the only time the token is exposed is on the PushNotification register event.

More generally, what's the common practice for handling device tokens?

If one user logs into my app, the app requests permissions from PushNotification, the register event is fired and I can associate that device with the logged in user. So far so good, but if that user logs out, and I break that association to stop the notifications, what do I do when another user logs in? The app already has permissions, so register won't fire again. How do I get the device token to associate it with the new user?

Or am I thinking about this the wrong way?

like image 418
nicholas Avatar asked Feb 14 '16 01:02

nicholas


1 Answers

It seems my assumption that the register event only fires when the user grants access was the problem. The register event will fire in response to a call to requestPermissions whether or not the user was prompted. So by requesting permissions and responding to the register event when the app loads, you can always get the device id. Like so:

PushNotificationIOS.addEventListener('register', (token) => {

  ... store or use the token here ... 

});

PushNotificationIOS.requestPermissions();
like image 180
nicholas Avatar answered Oct 06 '22 00:10

nicholas