Retrieve already aquired push notification token - ios

We are working on an app that is suposed to receive push notifications. On our test device when we were prompted for an ok to send notifications we clicked ok but failed to store the token that we received. (The request to the server on our end was not handled properly and the token was not stored anywhere else by us).
We have tried the following to have the function didRegisterForRemoteNotificationsWithDeviceToken trigger again (to no avail):
Completely remove the app and reinstall
Turn off notifications for our app in the iPhone settings
However, we cant get it to 'prompt' us again, and the APN development server seems to have already registered so I am assuming that is why the didRegisterForRemoteNotificationsWithDeviceToken doesnt trigger again.
In the end we cannot get a new token, and I don't know how to retrieve the already registered token. Does anyone know how to retrieve the already registered token?

if you call registerForRemoteNotificationTypes: method of UIApplication every time your application launch, the application:didRegisterForRemoteNotificationsWithDeviceToken: delegate method will be called every time also. When the first time registration, iOS will ask user if they want receive notifications, and iOS connect the Apple Notification Service to register and get device token. After that, registerForRemoteNotificationTypes: method call will neither ask user nor connect APN, iOS will immediately call delegate method with the already registered token.
if you want to the Application launching to ask user again, call the unregisterForRemoteNotifications , then call registerForRemoteNotificationTypes: method again.
some detail information related: iOS Application Client Side Device Token Management With Apple Push Notification

I found that the app will always call didRegisterForRemoteTrigger when the application is fully closed and started again, and this will give you the already registered token. I am not sure why it did not call this method when I first tried to recover the token, but this seems to be the solution.

Related

How to determine when to update deviceToken for push notifications / how to get it?

I've had various struggles with push notifications over the course of my app that seem to be fixed by uninstalling. I believe I've narrowed it down to expired deviceTokens.
Reading Apple's Push notification documentation, I found this:
Registration Succeeded But No Notifications Received
. . .
Your app may have sent an incorrect device token to your provider.
Your app should always ask for the device token by registering with
the push service each time it is launched. Don't store a device token
from your app and try to reuse it, because the token can change. Your
provider should then pass that same token on to the push service.
They suggest registering each time the app is launched. They also suggest best practice for push notifications is not to do that, since users don't like getting bombarded with access requests before even getting to see your app. So, just throwing the registration call into the app delegate isn't the best option. However, I am not seeing any more info on when a deviceToken expires or how to see if it has expired.
The closest thing I can find is this documentation on UIApplication's instance method isRegisteredForRemoteNotifications:
Return Value YES if the app is registered for remote notifications and
received its device token or NO if registration has not occurred, has
failed, or has been denied by the user.
My understanding is that this is the method to call to check if a user has enabled push notification services. I understand that permissions for specific notification types could all be turned off and this could still be true if the user allows push notifications. But the wording looks like it requires the app to be registered with the current deviceToken. Does this mean that I could call
[[UIApplication currentApplication] isRegisteredForRemoteNotifications]
inside the appDelegate, and if it's true, register remote notifications to update my deviceToken on my server and make sure my push notifications don't expire? Or, is this function going to run into the same thing that I currently am, where eventually the deviceToken is going to expire and this method will begin to return false instead of true, even though the user had allowed push notification?
tl;dr - Apple says deviceTokens eventually expire for push notifications. They suggest registering every time the app launches. I don't want to bombard new users with that alert. How do I ensure only users who have already accepted push notifications get re-registered?
The alert will only be shown once.
If the user previously Allowed notifications, your code will get the new token without needing any interaction from (or UI notice to) the user.
OK, the best practice for this are as follows, which we follow without failure
Always register for the Push on app launch, or in suitable AppDelegate lifecycle methods, using the following code
[[UIApplication sharedApplication] registerUserNotificationSettings: [UIUserNotificationSettings settingsForTypes:(UIUserNotificationTypeAlert | UIUserNotificationTypeBadge | UIUserNotificationTypeSound) categories:nil]];
[[UIApplication sharedApplication] registerForRemoteNotifications];
The delegate for Push if it is successfully registered will get called with deviceToken
- (void)application:(UIApplication*)application didRegisterForRemoteNotificationsWithDeviceToken:(NSData*)deviceToken
The delegate above will get called if there is change in deviceToken always. So whenever you get a deviceToken update it in the server.
In the server make sure you hit correct mode if you are running in Sandbox, or Production mode, discard values from Simulators they normally fails and block the service for sometime.
You can always check with your pem file and device token that Push is working or not with this online service.
Hope it helps.
Cheers.

How to prevent push notifications to app after reinstallation?

Since iOS 9 application changes apns token after reinstallation of an application.
But iOS 8 and 7 doesn't do it.
How can I distinguish next situation using iOS 8 and iOS 7?
User installed an app.
User launched app and allowed push notifications in the app. (I getted some token)
User logged in/registered. (I connected token with registered user).
User deleted the app.
User installed the app, launched it once and allowed push notifications. (getted the same token, )
After these actions, this unregisterd user will get notifications about some actions in his account without needs to log in.
If it were a bank/finance app, it would influence the security of registered user.
How to prevent this?
You can call
[[UIApplication sharedApplication] unregisterForRemoteNotifications];
in
-application:didFinishLaunchingWithOptions: method. Then, when user logs in, register for remote notifications.
But anyway, if app is uninstalled and installed again, it will not receive push notifications until -registerForRemoteNotifications get called, and you must call this method ONLY after user authentication. Even if token is same as previous one, it will associated with this new user, and your server should handle that.

Get Device Registration Token iPhone before registering

Is it possible to get the device token (the one that is set in DidRegisterForRemoteNotifications) of an iPhone Device before you call registerForRemoteNotifications? If so, how can you do this?
I want to get this token and send it to my server. I may need to get this token before the push notification actually happens, in case the person doesn't allow push notifications at that time.
Thanks
No, you can't get the token before you request it. That is the purpose of the registration process.
You can't send push notifications until the user has allowed your application to receive them. The user may also withdraw permission for your application to show notifications at any time.
Calling -registerForRemoteNotifications is how you get the device token in the first place; . In fact, -application:didRegisterForRemoteNotificationsWithDeviceToken: is called in response to invoking -registerForRemoteNotifications. You generally do this inside of your -application:didFinishLaunchingWithOptions:, and no notifications will be delivered to the device until it has registered.
For reference, see https://developer.apple.com/library/ios/documentation/NetworkingInternet/Conceptual/RemoteNotificationsPG/Chapters/IPhoneOSClientImp.html#//apple_ref/doc/uid/TP40008194-CH103-SW2.
Register your app’s supported interaction types as described in Registering Your iOS App's Supported User Interaction Types.
Call the registerForRemoteNotifications method to register your app for remote notifications. (In OS X, you use the registerForRemoteNotificationTypes: method to register your app’s interaction types and register for remote notifications in one step.)
Use your app delegate’s application:didRegisterForRemoteNotificationsWithDeviceToken: method to receive the device token needed to deliver remote notifications. Use the application:didFailToRegisterForRemoteNotificationsWithError: method to process errors.
If registration was successful, send the device token to the server you use to generate remote notifications.

APNS didRegisterForRemoteNotifications called before user allows notifications on iOS

I'm working on some iOS apps, all under the same publisher, that all have push notifications enabled. When I call registerForRemoteNotificationTypes, I get the user prompt to allow or disallow push notifications, but I application:didRegisterForRemoteNotificationsWithDeviceToken: gets called with token data before the user chooses an option. And it gets called again when they press OK. Is this normal?
Also of note: multiple apps appear to get the same token when running on the same device.
This is from Apple docs.
When you send this message, the device initiates the registration
process with Apple Push Service. If it succeeds, the application
delegate receives a device token in the
application:didRegisterForRemoteNotificationsWithDeviceToken: method;
if registration doesn’t succeed, the delegate is informed via the
application:didFailToRegisterForRemoteNotificationsWithError: method.
If the application delegate receives a device token, it should connect
with its provider and pass it the token.
AND
The first time a push-enabled app registers for push notifications,
iOS asks the user if they wish to receive notifications for that app.
Once the user has responded to this alert it is not presented again
unless the device is restored or the app has been uninstalled for at
least a day.
I think what you are observing is normal. There is no mention that application:didRegisterForRemoteNotificationsWithDeviceToken
will only be called if user grants permission. I think you can optimize it by caching device token in NSUserDefaults and in this method check if the new token not equal to cached token.
It's an old question, but I've just encountered this issue and it seems to be related to remote-notification background mode. application:didRegisterForRemoteNotificationsWithDeviceToken is called on my iPhone before accepting push notification permission only when this background mode is turned on.

When is didRegisterForRemoteNotificationsWithDeviceToken called?

There are a lot of questions about didRegisterForRemoteNotificationsWithDeviceToken but they all sidestep a very direct question which I cannot seem to find an exact answer to.
For an app which is properly set up for notifications in all other ways and has proper network connectivity: when is didRegisterForRemoteNotificationsWithDeviceToken called? Some possible choices might be:
Every time the app starts
Only after the initial prompt to the user to accept push notifications
Something else?
The application delegate will call the method upon successful registration of remote notification after you call this method in your UIApplication:
(void)registerForRemoteNotificationTypes:(UIRemoteNotificationType)types
According to: http://developer.apple.com/library/ios/#DOCUMENTATION/UIKit/Reference/UIApplication_Class/Reference/Reference.html
When you send this message, the device initiates the registration process with Apple Push Service. If it succeeds, the application delegate receives a device token in the application:didRegisterForRemoteNotificationsWithDeviceToken: method; if registration doesn’t succeed, the delegate is informed via the application:didFailToRegisterForRemoteNotificationsWithError: method. If the application delegate receives a device token, it should connect with its provider and pass it the token.
Now, to elaborate further, normally an app will call the registerForRemoteNotificationTypes in your didFinishLaunchingWithOptions:(NSDictionary *)launchOptions in your application delegate. And therefore, the application:didRegisterForRemoteNotificationsWithDeviceToken is then usually called moments after the launch of the application.
Edit: The application:didRegisterForRemoteNotificationsWithDeviceToken still gets called for subsequents registration after the first.
When the app is first run it will ask the user whether they will allow remote notifications. If they say yes then it will fire didRegisterForRemoteNotificationsWithDeviceToken at that time and every time after it will fire this function when the app is first opened. If they say no then it will not be fired unless they went into settings and allowed notifications on the app.
There can be many reasons, check some reasons
If you run the app in the simulator, the
application:didFailToRegisterForRemoteNotificationsWithError: method
will not be called as push notifications are not supported in the
simulator using a device token. You can still send push notifications to your simulator using xcrun simctl push <device> com.example.my-app ExamplePush.apns"
Check your deice internet connection if not connect it.
For more info check Link

Resources