1

As many of you know, in order to give support for push notifications in my app I need two things to be stored in MySQL:

  • device id
  • device token

which I respectively get with:

  • let device = UIDevice.currentDevice().identifierForVendor!.UUIDString
  • let deviceTokenString: String = ( deviceToken.description as NSString ).stringByTrimmingCharactersInSet( characterSet ).stringByReplacingOccurrencesOfString( " ", withString: "" ) as String

Always as you know, each time the app is launched it tries to register into the apposite MySQL table push notifications but if it is already present (its device id is already present) then the new registration is discarded. Anyway, I have just understood (as explained here) the device id changes each time a new version of the application is re-installed in my iPhone. The risk is to populate the table eith too many useless entries. Is there a way to keep the same device id? Or, what is the smartest way to handle this?

Community
  • 1
  • 1
SagittariusA
  • 5,289
  • 15
  • 73
  • 127
  • 1
    do you have any sort of user account in your system? usually push token is connected to particular user account and if push token is changed you just need to update one record in server db – heximal Mar 30 '16 at 15:23
  • yes, I have an account in my iphone (the classic apple account) but I am afraid it's useless because the device id changes when a new version of the app is installed. Read the link I posted – SagittariusA Mar 30 '16 at 15:29

2 Answers2

1

No device identifier is available for iOS project code. The only possibility to remove 'dead' push token is to check whether push token is still valid. It's reasonable to make when you send a push. If it's not valid then delete it from you Mysql table. Look at this SO Question and answer. It's also important to validate push tokens from the perspective that Apple may apply sanctions to those developers who's sending a lot of push requests to dead tokens.

Community
  • 1
  • 1
heximal
  • 10,327
  • 5
  • 46
  • 69
  • I am really afraid you're right. So, how can I understand at PHP side if a token is not valid anymore? maybe when `stream_socket_client` fails or when the sending of the message fails? – SagittariusA Mar 30 '16 at 15:35
  • it's better to use failed sending as an attribute of invalid token. even if it's false alarm and you accidentally delete this token, next time this device will send you this token again since it's alive and you will store it to database again. – heximal Mar 30 '16 at 15:43
  • Maybe this is the solution: http://stackoverflow.com/questions/15943671/how-get-feedback-from-apns-when-sending-push-notification – SagittariusA Mar 30 '16 at 16:15
  • forgive me, just the last question: I have read that a connection to feedback.push.apple.com returns the invalid tokens only since the last time you connected to it. So, if during deletion of invalid tokens from mysql, my php application crashes then I cannot re-ask invalid token to feedback.push.apple.com because it would return null. Is that correct? – SagittariusA Mar 30 '16 at 18:56
0

Push notification works with 2 things: one is device id(the identifier of device/user, which is mostly implemented by the owner of the application) and other is GCM token(which is generated and maintained by Google GCM server for each installation).

Device id can be any identifier using which you identify a user/device etc, device token is generated by google server, which is regenerated every time the app is installed.

Now suppose app is installed in your phone, so in your MySql database will contain 1 row containing your deviceId and GCM Token, GCM will work by this.

Now suppose you reinstall the app, so a new token is generated, but device identifier is same. So in your server side code, you need to put a check whether the device id exists or not in your table, if it exists, then compare the new token and old token, if they are same, don't do anything. if they are different, replace the old token with new one. If the device id is not present, then you should insert. In this way there will not be unnecessary entries.

This is what I could understand from your question. Hope this helps.

Alok Gupta
  • 1,353
  • 1
  • 13
  • 29
  • Thank you for your answer but I am afraid it's not like you've said. If you read here (http://stackoverflow.com/questions/25925481/how-to-get-a-unique-device-id-in-swift) it is clearly said that device id changes EACH TIME the app is re-installed in the iphone. So it does not remain the same...I have written the server side control you mentioned but it does not always work because with a new installation/release of the app, also the device id changes. see the link – SagittariusA Mar 30 '16 at 15:28
  • Friend! you are doing it in a wrong way. May be in swift the device id changes with every installation, but the way in which push notifications are implemented is same. User identifier is constant whereas token changes with every installation. – Alok Gupta Mar 30 '16 at 15:30
  • **There is no longer a way to uniquely identify a device after the user uninstalled the app(s).** The documentation says: "The value in this property remains the same while the app (or another app from the same vendor) is installed on the iOS device. The value changes when the user deletes all of that vendor’s apps from the device and subsequently reinstalls one or more of them." – SagittariusA Mar 30 '16 at 15:31