2

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?

Sarabjit Singh
  • 1,814
  • 1
  • 17
  • 28
kavitha G
  • 25
  • 9

3 Answers3

2

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! :)

Eric Aya
  • 69,473
  • 35
  • 181
  • 253
Renish Dadhaniya
  • 10,642
  • 2
  • 31
  • 56
0

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

Community
  • 1
  • 1
Yitzchak
  • 3,303
  • 3
  • 30
  • 50
0

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.

Link: https://stackoverflow.com/a/41017124/7752480