Device token and push notifications - ios

I'm currently creating an app that needs to receive push notifications from a server. This app has a facebook connect part, and a logout (from facebook) part as well.
When I get the device token, I send it to my server and save it into DB with the FB_id, then when I want to send a notification to a user, I get its FB_id, and then get its device token.
My question is:
If a user decides to lend its device to a friend, I will have two FB_id for one device token. Then, the first user will receive notifications from the second one...
How can I deal with this?
I thought to delete the token on logout, but I'm not sure it's really safe...
Thanks in advance

Related

iOS Push Notifications - device token management

I am adding Push Notifications to my app and I know I need to store the device tokens in my database so that I can send push notifications to specific devices (or all devices).
My question is what is the best practice for maintenance of these device tokens? I can store all device tokens as they are received, but how can I detect and remove old device tokens that are no longer valid? I assume a device token can become invalid if user deletes the app, or if user turns off notifications for the app.
Update - Having a user authentication and linking it to the device token (and updating based on login/logout) makes sense. But what about if the user deletes the app? there is no logout, how do you remove the device token then?
The Apple feedback service is no longer used. Instead we have to look for a response status of 401 from the apple push notification service to determine that a token is invalid.
To test this in a development environment. Use the trick below (it says it's for feedback service, but should work with the new status code from APNS as well)
How to test Apple Push Notifications Feedback Service?
Use the APNS feedback service, to find the device tokens that belonged to an app that was uninstalled.
See this apple documentation
If you are using something like Amazon SNS for push notifications, you can use their API to get list of disabled arns and remove the corresponding device tokens from your database.
Make an webservice which stores user's device token in database for particular user. Call this webservice only if user is logged in or you have identify user as per your requirement. You need to call this webservice when device successfully register for notification and if user is not identify (i.e. not logged-in) then call this service after login api.
Also pass device token when in login and register API if you have according to your flow and replace device token for particular user.
And when user logout just unregister for notification
As you said in your last statement I assume a device token can become invalid if user deletes the app, or if user turns off notifications for the app.
General scenario is when user again login to the app or register to the app you need to again take the device token from the user and need to store it in your database
Suppose I had one app and again I install only that at that time whenever I login again to the app at that time the api must having parameter for deviceID so whenever api call happen for login at that time new device token take place in your database by just replacing the old one. Same thing will happen for new register with that app.
Hope above description help you. :)
Your app server won't know if a particular app has been forcefully deleted & your server would still have let's say token T1 mapped to deleted app/device let's say A1. Now, it may be possible that another valid user (A2) comes up with same device token T1. You just need to make sure that at any point of time one device token (i.e. T1) is mapped to only one device (the device which has provided the T1 latest). All other older device token mappings i.e. A1-T1 shall be deleted at this point else A2 might receive A1's notifications.

Send remote push notification to specific user

I created a code to send remote notification using APNs, I created a development certificate, ssl, etc. The code worked very well, when I call my php script on my server, the script send a notification and my app on my iPhone show the notification.
But I have a doubt, this is a generic code, so this code will send a nofication to all iphones that has my app, but if I need to send a notification to a specific user? Like whatsapp for example? How can I inform to APNs what user that should receive the notification?
You will need either a new table to handle the user_id and its device token or add a column to the user table for device token. When you send a push notification, you can then send directly to the user device token which alert the user.
Some steps that you need to prepare will be a api endpoint to allow user to PUT or POST its device token to its account. Maybe something like api/v1/user/11/token. In your iOS side, you will then just call this endpoint to send the device token to the endpoint when the permission is accept.
I would suggest a separate table to handle the token because some user have different device like android and iOS. So if you want to alert both devices, is will be a lot easier to manage.
Get device token while registering for push notification, send that device token via API to server, then using that device token you would be able to target push notificaion to particular devices only.
Refer - Device Token not received when registering for remote notifications in Swift

Handle push notifications for Multiple Device/Multiple User Login in ios

I integrated push notification in my app. User will able to login/registration in the app.
User can able to login in multiple device simultaneously.
So, how can I manage the push notification in multiple device?
How facebook/popular app handle this?
Give me some guidance
Thank you
Push Notification works on Device tokens, doesn't matter, how many devices are registered per user. You just need to manage number of device per user. You can also maintain session to track if user is logged in into app or not. Your server will send notification to user on multiple devices. Your app should send device tokens to server on which user logged in or register.

Handling APNS Push Token with no network connectivity

Most documentation advises to register for remote notifications on launch, and on receipt of the token, push this to a server with any other app-specific settings (e.g. user, in-app Push settings). However, I am not sure how to handle the following corner case.
If the user launches the app without connectivity, no token will be received. Moments later, connectivity is restored. The user attempts to change some setting in the app, which should prompt a post to my server including the setting and the token. However, the token is still nil.
Would it not be better, instead, to register for notifications every time before attempting to post the setting? It seems unnecessary to request this token on launch since it might not be needed, and even worse, might be nil when it is needed.
I think it's still best to register for push notifications when the app is launched. When you get the device token, you don't have to immediately send it to your server. You can store it locally on the device. When your logic requires to send the device token (as well as other settings) to your server, get the locally saved token. If the saved token is null, try registering again, and send the token to your server once you get it.
Note that when the app is launched you should register for push notifications even if you a have a previous locally stored device token (due to the remote chance that Apple would decide to change the device token).
Apple recommends you should call register every time the app comes to the foreground ( either when you open it or when it comes back from background).
This call does not serve only as a way to get the push notification token but also to let Apple know that this application is still interested in receiving push notification. If you call this method only once, or rarely and you don't send push notifications that often, you could run into a scenario where the token you have been using, that is stored on the server is no longer a valid token (after enough time has passed without a token being acknowledged by a device apple will no longer consider the token valid. )

Sending device token in Apple push notification

The tutorials that I have come across for push notification manages the sending of device token along with the data that user sends for web service requests. My app too uses webservices but for my application, when a record is added to the website, it has to be notified on my app. So when and how do I send my device token from my iphone app? Please help.
I'm sending the device tokken to the server every time the user logs in. I'm making a http-request (on wich i add the device tokken) to check the password & email. If the entered data is ok, i save the tokken in the database.

Resources