Does the registration token refresh whenever the app is launched in iOS? - ios

When I install app for the first time and it asks for the permission for getting notification, if I press yes then the Device token is generated, but whenever I relaunch the app, I get the error:
Failed to fetch APNS token Error Domain=com.firebase.iid Code=1001 "(null)
Can anyone tell whether the device token is generated every time the app is launched?

Nope. A (new) token is not generated every time the app is launched. It is generated on initial launch (i.e. first launch after app install). From the docs:
By default, the FCM SDK generates a registration token for the client app instance on initial startup of your app.
But it may change because of the following scenarios (also from the docs):
The registration token may change when:
The app deletes Instance ID
The app is restored on a new device
The user uninstalls/reinstall the app
The user clears app data.

Today the doc says https://firebase.google.com/docs/cloud-messaging/ios/client#access_the_registration_token
The app is restored on a new device
The user uninstalls/reinstall the app
The user clears app data.
Seems like FCM Token is not generated when we delete the instance ID.

Related

ios device stops receiving notifications after user logs out and logs in the application(react-native app with react-native-messaging)

I am building a react native application which uses react-native-firebase for notifications. I am trying to get notifications from firebase messaging service. everthing works fine until user is logged in for the first time, but as soon as I log out from the account and log into another account, the ios device stops receiving notifications.
Code flow:
when user logs in, a device token is generated.
when user logs out, the already generated device token is deleted and another device token is generated which is stored in local storage and used for another login.
when logged in from another account, no notification is received.
Things tested:
device token is getting fetched properly and stored in the our server.
the notification is showing a success when triggered, but not getting received on iOS device
All working fine as expected in Android.
Expected Result:
user logs in, gets notification.
user logs out, token stored in server for next login.
when user logs in again, he should get notifications.
Only issue arising in iOS device after account is switched.
Any idea why this must be happening?

iOS-GCM integration: does registration need to happen every time

Do you need to register your APNS device token with GCM every time via:
tokenWithAuthorizedEntity:scope:options:handler
every time the app is launched, EVEN IF the device token is identical.
According to a sample app for GCM, every time the app is launched, [[GGLInstanceID sharedInstance] tokenWithAuthorizedEntity:_gcmSenderID [1] is called. This is because the registration is invoked from didRegisterForRemoteNotificationsWithDeviceToken which does need to be called every time the app is launched - "Device tokens can change, so your app needs to reregister every time it is launched and pass the received token back to your server." [2]
Can I store the registrationToken and deviceToken and only reregister when the deviceToken changes? The presence of GGLInstanceIDDelegate#onTokenRefresh seems to suggest that there is a mechanism to be notified when the registrationToken changes for your deviceToken, but I would like this to be confirmed.
[1]https://github.com/googlesamples/google-services/blob/master/ios/gcm/GcmExample/AppDelegate.m#L151
[2]https://developer.apple.com/library/ios/documentation/NetworkingInternet/Conceptual/RemoteNotificationsPG/Chapters/IPhoneOSClientImp.html
In Google's official GCM Client Demo App, registering every time the app starts ensures that your servers gets the Registration ID even if it lost it somehow, but that shouldn't be an issue if your Registration IDs are persisted properly in the server.
Note that google may periodically refresh the registration ID, so you should design your app that be called multiple times. Your app needs to be able to respond accordingly.
To summarize, you should always treat the Token as if it may change any time you restart your application.

Should we register for iOS push notification every time during application launch?

We are using remote notifications in our iOS application and we are registering to APNS server inside application: didFinishLaunching: delegate method. That means it will be registered with APNS server every time the application launched freshly. Below is the code snippet.
[[UIApplication sharedApplication] registerForRemoteNotifications]
We have noticed that the device token (which is delivered to us by APNS server) is same every time. So, we thought that we can save the token in NSUserDefaults and use it later. In application: didFinishLaunching: method, we can give a check if the device token is present in NSUserDefaults or not. If it's available, we can use the same instead of registering to APNS. Otherwise, we can opt to register with APNS.
But, from the Apple documentation, it's observed that "they encourage us to register for remote notifications each time the application is launched freshly". Below is the screenshot from docs.
When does the device token value change actually? Can I afford to store the device token in NSUserDefaults and use it later instead of registering every time? Please help!! Thanks in advance!!
To answer your question with some apple documentation:
The form of this phase of token trust ensures that only APNs generates the token which it will later honor, and it can assure
itself that a token handed to it by a device is the same token that it
previously provisioned for that particular device—and only for that
device.
If the user restores backup data to a new device or reinstalls the
operating system, the device token changes.
This means you shouldn't really store the token in your NSUserDefaults.
However if you want to save it anyway I recommend to save it in the Keychain.
See this example how to do so:
Store Device Token in Keychain
Another quote from apple documentation:
"By requesting the device token and passing it to the provider every time your application launches, you help to ensure that the provider has the current token for the device. If a user restores a backup to a device other than the one that the backup was created for (for example, the user migrates data to a new device), he or she must launch the application at least once for it to receive notifications again. If the user restores backup data to a new device or reinstalls the operating system, the device token changes. 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 iOS passing the device token to the delegate immediately without incurring additional overhead."
EDIT:
Looks like the above links to Apple's documentation are broken by now. Here is a updated link (thanks to #Enrico Cupellini): https://developer.apple.com/library/content/technotes/tn2265/_index.html

Does device token ever change in Apple Push Notifications

I am developing an iOS app in which i have implemented Push Notification.
Everything is working fine.
But just wish to ask if device Token for my Apple device will ever change??
Also do we need internet connectivity for generating device token.
Thanks
device Token for my Apple device will ever change
-- YES. If you restores backup data to a new device or reinstall the operating system, the device token changes. So my suggestion is to update the server with token
do we need internet connectivity for generating device token
-- as far as I know, YES. When you register user, you call method for registration for push notification. This on successful registration call the delegate method -
- (void)application:(UIApplication *)app didRegisterForRemoteNotificationsWithDeviceToken:(NSData *)deviceToken
indicating you are registered successfully for a push notification or on failure it calls -
- (void)application:(UIApplication*)application didFailToRegisterForRemoteNotificationsWithError:(NSError*)error
indicating failed to register for notification.
You can check it by turning off network and running your application.
Based on the Apple Documentation, the answer is YES:
The form of this phase of token trust ensures that only APNs generates
the token which it will later honor, and it can assure itself that a
token handed to it by a device is the same token that it previously
provisioned for that particular device—and only for that device.
If the user restores backup data to a new device or reinstalls the
operating system, the device token changes.
From the apple docs -
The device token is your key to sending push notifications to your app on a specific device. Device tokens can change, so your app needs to reregister every time it is launched and pass the received token back to your server. If you fail to update the device token, remote notifications might not make their way to the user’s device.
Device tokens always change when the user restores backup data to a new device or computer or reinstalls the operating system. When migrating data to a new device or computer, the user must launch your app once before remote notifications can be delivered to that device.
Never cache a device token; always get the token from the system whenever you need it. If your app previously registered for remote notifications, calling the registerForRemoteNotifications method again does not incur any additional overhead, and iOS returns the existing device token to your app delegate immediately. In addition, iOS calls your delegate method any time the device token changes, not just in response to your app registering or re-registering.
For more APNS Doc
I was recently troubleshooting an issue with push notifications for a user. I was a bit confused by the guidance here, indicating that the token would only change in rare circumstances, such as "moving to a new device" or "re-installing the OS".
While the above events are likely valid events where the token is updated, I also see the token is updated simply when the user updates the OS on their device.
For example:
When my device runs iOS 15.4.1, and I install an application and request a push notification token, I'm provided with token "A".
After I update my device to iOS 15.7, with the same application still installed, and request a push notification token, I'm provided with token "B".
Therefore while rare events like restoring backup data to a new device or re-installing the OS are valid for this case, more frequent events such as a user simply updating the OS of their device are also valid for causing the push notification token to change.
Thus, this will likely change quite frequently and should always be requested from the device and updated on your server.

Apple push notifications - getting empty push token from production app - will push tokens be sent after fix provisioning profile issue

We added receiving push notifications to an iPhone app. Everything was working in the test/sandbox environment, we were getting token id's from the app and could send push notifications from our server.
But now the app is approved and came out of the Appstore we were getting empty push tokens/notifications id's send to our server. We already have over 600 of them... Note that end users do get the popup to approve of receiving notifications in the app, the app is just sending empty tokens to our server after approval. So probably empty tokens are handed out by the APNS server.
The following issue showed us that this is probably due to missing 'push notification' entitlement in the provisioning profile we used to make the build for the app store:
How do I check if an iOS distribution provisioning profile has push notifications enabled?
The missing entitlement was due to a bug in Apple's provisiong protal website, but after 'Modify any existing profile before you download the new one' as mentioned in the following article
http://developer.apple.com/library/mac/#documentation/NetworkingInternet/Conceptual/RemoteNotificationsPG/ProvisioningDevelopment/ProvisioningDevelopment.html
We now have the correct entitlement in our .mobileprovision file:
<key>Entitlements</key>
<dict>
..
<key>aps-environment</key>
<string>production</string>
..
So we are rebuilding our app and adding it to the store. And hoping to get push notifications then.
I hope the above might help some others. But now to get to my actual question: Will we start receiving new push tokens also for the 600+ users that already downloaded the current version when they install the next update out of the store? Or do we need to add some initial code to our app? The registerForRemoteNotifications method is right now only called on application startup. Will it also be triggered when the push token id is changed from empty ('') to an actual token? Of course we do not want to wait another (small) week for the new AppStore approval and only then find out that push notification still aren't working for some users. I'm hoping some expert out there can tell us.
Note: We are using an iPhone app developed in MonoTouch, and using the (old) APNS-Sharp library to send the notifications from our server, but I don't think those details are relevant for this issue.
When those 600+ users install the next update and run the application again, your application will call registerForRemoteNotifications (since you said you call it on startup), and will get the non empty device token (when application:didRegisterForRemoteNotificationsWithDeviceToken: is called).
Apple state in their docs that you should always call this method at startup instead of using a cached copy of the device token, because the device token is not guaranteed to remain the same. So you shouldn't have any problem.
Here's the relevant quote from the APNS docs :
An application should register every time it launches and give its
provider the current token. It calls
the registerForRemoteNotificationTypes: method to kick off the
registration process. The parameter of this method takes a
UIRemoteNotificationType (or, for OS X, a NSRemoteNotificationType)
bit mask that specifies the initial types of notifications that the
application wishes to receive—for example, icon-badging and sounds,
but not alert messages. In iOS, users can thereafter modify the
enabled notification types in the Notifications preference of the
Settings application. In both iOS and OS X, you can retrieve the
currently enabled notification types by calling the
enabledRemoteNotificationTypes method. The operating system does not
badge icons, display alert messages, or play alert sounds if any of
these notifications types are not enabled, even if they are specified
in the notification payload.
This is also relevant :
By requesting the device token and passing it to the provider every
time your application launches, you help to ensure that the provider
has the current token for the device. If a user restores a backup to a
device or computer other than the one that the backup was created for
(for example, the user migrates data to a new device or computer), he
or she must launch the application at least once for it to receive
notifications again. If the user restores backup data to a new device
or computer, or reinstalls the operating system, the device token
changes. 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.

Resources