Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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 ?

like image 661
siva krishna Avatar asked Sep 13 '25 19:09

siva krishna


1 Answers

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!
    }
like image 125
Nikhlesh Bagdiya Avatar answered Sep 16 '25 07:09

Nikhlesh Bagdiya