siva krishna
siva krishna

Reputation: 1179

Is the device token changes if Uninstall/reinstall App for same device

Im getting multiple notifications for same app..

if It changes how to delete the old one form our server..

Here is my observation in iOS 9 device

1.There are two fields in DB APPId and Device token If I unstalled the app and installed it again in the same device the device token changes..

2.I tested another app in the same device The Device Token is different even the same device

Im getting 3 notifications to the same device Even If I deleted the first device token one from my DB…. I didn’t understand still whiz the reason for multiple notifications..

What might be the possible reasons ?

Upvotes: 4

Views: 6010

Answers (4)

Nikhlesh Bagdiya
Nikhlesh Bagdiya

Reputation: 4466

The Device token is change in the following conditions.

  • If the user restores backup data to a new device.
  • Reinstalls the App

So my suggestion is to update the server with the new token.

When every time app is launch in didRegisterForRemoteNotificationsWithDeviceToken you have to call api which update the device token if changes.

In your DB create two more fields as device token and APPId so, update device token with respect to APPId.

Get APPId or unique device id from device keychain and send it to your server with device token so, at server update device token with respect to APPId.

keychain value will never change in the above following conditions.

For getting Keychain value follow Keychain

// MARK: - Push Notification Delegate Methods.
    func application(_ application: UIApplication,didRegisterForRemoteNotificationsWithDeviceToken deviceToken: Data) {

        //send this device token to server

        let token = String(data: deviceToken.base64EncodedData(), encoding: .utf8)?.trimmingCharacters(in: CharacterSet.whitespaces).trimmingCharacters(in: CharacterSet(charactersIn: "<>"))

        //Save device to UserDefaults
        let defaults = UserDefaults.standard
        defaults.set(token, forKey: "DeviceToken")
        defaults.synchronize()

        print("token is ---\(token)")

        print("AppId ----\(getUniqueDeviceIdentifierAsString)")

        //Send token value and AppId to server

    }

     var getUniqueDeviceIdentifierAsString : String {

        let appname =  Bundle.main.infoDictionary![kCFBundleNameKey as String] as! String

        var strApplicationUUID: String? = KeychainWrapper.standard.string(forKey: appname)
        if strApplicationUUID == nil {
            strApplicationUUID = UIDevice.current.identifierForVendor?.uuidString
            _ = KeychainWrapper.standard.set(strApplicationUUID!, forKey: appname)
        }

        return strApplicationUUID!
    }

Upvotes: 4

Kumar KL
Kumar KL

Reputation: 15335

iOS9 and later, Device Token might change each time app is re installed. So the best way is to store(update or insert to the DB) the Device token on each launch.

Upvotes: 0

Pavan Gandhi
Pavan Gandhi

Reputation: 1749

@Krishna : Device Token remains Same as You installed the app on first time and uninstalled and once again you installed this app on same device.

if you use second time new Device then device token will be different.

Note : Just inform backend developer to Do below thing :

  1. The ApI which you use for inserting device token in database, in that he need to check if device token is diff and update it new one. otherwise simply use whatever pass with web service.

Its work fine.

Upvotes: 0

Akshay Patel
Akshay Patel

Reputation: 157

Create new web service for logout session and call this service when you logout and this service told your backend developer to remove your device token from database.

Upvotes: 1

Related Questions