2

I am using apns notification for my app, and for this i am storing the apns device token in my sql database.

The issue is that every time the user deletes or installs the app, it generates a new device token and this gets stored in the database, since the same user can have multiple devices, I cant update the token for that user and a new record gets created. this has let to many records for the same device and same user. it might lead to performance issue later.

Please help. How do I fix the issue storing multiple tokens for same user.

rmaddy
  • 314,917
  • 42
  • 532
  • 579
ViS
  • 1,357
  • 1
  • 17
  • 36
  • 2
    You need to take data from the APNS feedback service to determine which tokens are no longer active and purge these from your database, however if you get a new token for the same user and device then you can replace the old one. The composite key (user,device) is unique – Paulw11 Dec 16 '15 at 22:35
  • yes, but same user can have multiple devices, like and iphone 6 and 5, same time and app does not stop him from registering with many device same time. so user can have multiple device token and it does not become unique. Also I found many device tokens for same device with active status. e.g. if i delete and install app, 5 times in 10 mins it creates 5 token and all seem to be active, notification messages to all of them return success. – ViS Dec 16 '15 at 22:43
  • 1
    That is why you need to identify the device : store the `identifierForVendor` in your table. Then you can tell if this is a device you have seen before or a new device – Paulw11 Dec 16 '15 at 22:45
  • Oh, I did'nt know this, the identifierForVendor would remain same even if I delete or re-install the app, will it even remain same if the user formats the device? , coz i need to have a single record for user + device combo for that app. – ViS Dec 16 '15 at 22:57
  • 1
    If the device is reformatted the `identifierForVendor` will change. If they re-install the app then it may change. This is why you need to use the APNS feedback service to periodically purge your database of invalid device tokens – Paulw11 Dec 16 '15 at 23:00
  • But the APNS feedback service would not consider these tokens as Invalid, since if i delete and install app, 5 times in 10 mins it creates 5 token and all seem to be active, notification messages to all of them return success. if it returns success means all the tokens are valid ones. – ViS Dec 17 '15 at 15:44
  • Let us [continue this discussion in chat](http://chat.stackoverflow.com/rooms/98256/discussion-between-vis-and-paulw11). – ViS Dec 17 '15 at 16:08
  • It would be great to see how you solved this issue @ViS – Lukas Nov 01 '16 at 08:00

1 Answers1

0

Sorry for replying late, we fixed the issue by running another job which used to delete APNS token stored in database other than the latest one.

Our database stored APNS tokens for each user and each device. Depending upon these uniqueness. only latest tokens for each device is kept in database to prevent multiple notification on same device.

another solution is to use upset (insert or update), if any device already has a token then update the same with new one or insert.

I feel IOS should provide reliable API to handle this. The present Delete token API is not reliable.

ViS
  • 1,357
  • 1
  • 17
  • 36