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!
}