I'm using UUID
as a unique id for push notification services, this id passing to server and receiving push notification. But this ID(UUID)
will be changing when unstalling and reinstalling application on device. Is there any solution to save the Device id and use it while re-installing the application?

- 1,814
- 1
- 17
- 28

- 25
- 9
3 Answers
YES, you need to generate UUID and store in Keychain. UUID is same if you uninstall the application from your device. It is same as RESET your whole device. APN token is not unique.
//Generate Device UUID
func CreateApplicationDeviceUUID() -> String{
let DeviceUUID = NSUUID().uuidString
print("DeviceUUD==\(DeviceUUID)")
return DeviceUUID
}
//Retrive Device Unique UUID
let keyChainID = Locksmith.loadDataForUserAccount(userAccount: Bundle.main.object(forInfoDictionaryKey:"CFBundleName") as! String)
let retriveuuid = keyChainID?[RDGlobalFunction.deviceAppUUID] //RDGlobalFunction.deviceAppUUID is a Key of KeyChain Value Storage
if(retriveuuid == nil){
let uuid = CreateApplicationDeviceUUID()
do{
try Locksmith.saveData(data: [RDGlobalFunction.deviceAppUUID : uuid], forUserAccount: Bundle.main.object(forInfoDictionaryKey: "CFBundleName") as! String) //Locksmith - SSkeyChain Thirdparty KeyChain Wrapper
}catch{
//Catch Error
}
}
Happy Coding! :)

- 69,473
- 35
- 181
- 253

- 10,642
- 2
- 31
- 56
-
Thank you renish.can i use this stored id for receiving notification.because after re installation app will have different id know. – kavitha G Jan 10 '18 at 07:18
-
No, You need to use Token for the notification. UUID is just for identify unique devices. – Renish Dadhaniya Jan 10 '18 at 08:21
-
Thank you Renish. – kavitha G Jan 10 '18 at 09:24
-
@kavithaG, Welcome...Happy to help & coding :) – Renish Dadhaniya Jan 10 '18 at 10:39
-
I really don’t understand why the UUID is required?! Push notifications flow doesn’t require to store it.... – Yitzchak Jan 10 '18 at 19:03
-
@Yitzchak, Thanks for your comments. She needs a unique Identity of a device. UUID is not used for PUSH notification and so on. It just gives unique Identity, execute some function and many more regarding Device. – Renish Dadhaniya Jan 11 '18 at 04:31
You cannot save the device token forever, the user has the right to disconnect from you.
The correct implementation should keep the special token that APNS gives per device per application, and when you send your push notification APNS will respond to you which ID is not valid anymore so you remove it from your server (link to docs of APNs possible responses), see reasons:
APNs can issue a new device token for a variety of reasons:
User installs your app on a new device
- User restores device from a backup
- User reinstalls the operating system
- Other system-defined events
As a result, apps must request the device token at launch time
Your application with every launch should query that ID and send it to your server
And your server part:
Receiving, via APNs, globally-unique, app-specific device tokens and other relevant data from instances of your app on user devices. This allows a provider to know about each running instance your app.
That is the correct implementation. Link to docs
You can save your device UUID in keychain and fetch during new app installation this can resolve able to resolve your above issue,
Please find the below Stackoverflow answer link, which helped me to resolve same issue to maintain unique identifier.

- 968
- 8
- 22