Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

iOS get Device Token after device has been registered for push notifications?

I know that you can get the Device Token with the method:-

- (void)application:(UIApplication *)app didRegisterForRemoteNotificationsWithDeviceToken:(NSData *)deviceToken

However if I understand correctly this method only gets called the first time the app runs and the user accepts the notification request. On subsequent restarts of the app the method never gets called again.

So my question is this - is there any other way to access the Device Token after the user has accepted notifications?

Edit - I know all of that the device is online and the provisional profile is linked to the app id because I can receive Push notifications. Receiving push notifications is not the problem - getting the Device Token and storing it in Parse is what I want to achieve.

Thanks

like image 225
BlinkingCahill Avatar asked Mar 27 '14 13:03

BlinkingCahill


People also ask

What is device token in Apple Push Notification?

'The device token you provide to the server is analogous to a phone number; it contains information that enables APNs to locate the device on which your client app is installed. APNs also uses it to authenticate the routing of a notification. '

Where do I find device token?

Step 1: Go to the Devices --> Devices section from the upper NavBar. Step 2: Select the device. Step 3: From the Device options, click on the "Manage Device tokens" button. Step 4: A drawer will slide from the right.


2 Answers

You're wrong.

From the -application:didRegisterForRemoteNotificationsWithDeviceToken: documentation:

The delegate receives this message after the registerForRemoteNotificationTypes: method of UIApplication is invoked and there is no error in the registration process [...] This method could be called in other rare circumstances, such as when the user launches an app after having restored a device from data that is not the device’s backup data. In this exceptional case, the app won’t know the new device’s token until the user launches it.

From the Apple Push notification guide:

Moreover, never cache a device token and give that to your provider; always get the token from the system whenever you need it. If your application has previously registered, calling registerForRemoteNotificationTypes: results in the operating system passing the device token to the delegate immediately without incurring additional overhead. Also note that the delegate method may be called any time the device token changes, not just in response to your app registering or re-registering.

In other words that method is called whenever you call registerForRemoteNotificationTypes: and the operation is successful, as well as at other times when the token changes

EDIT:

If a cellular or Wi-Fi connection is not available, neither the application:didRegisterForRemoteNotificationsWithDeviceToken: method or the application:didFailToRegisterForRemoteNotificationsWithError: method is called. For Wi-Fi connections, this sometimes occurs when the device cannot connect with APNs over port 5223.

Alternatively make sure you are building with a provisioning profile linked to an app id that has Push notification entitlements

like image 172
jackslash Avatar answered Nov 02 '22 22:11

jackslash


Hmm, try checking your provisioning profile again. It sounds like you may be using one with a wildcard. You might be receiving pushes from the server because you originally deployed with a correct profile, however if you've changed to using a wildcard profile (a team profile for example), you won't be registering for remote notifications correctly...

like image 42
Smikey Avatar answered Nov 02 '22 23:11

Smikey