I am using Xcode 7.1.1 and iOS 9.1.
I am facing this weird issue. I have to implement push notifications in the app. I have successfully created the certificates and provisioning profiles with push notifications enabled for development mode.
I have kept the same bundle ID in my app. The code is written fine too in my appdelegate class
func application(application: UIApplication, didFinishLaunchingWithOptions launchOptions: [NSObject: AnyObject]?) -> Bool {
let pushSettings = UIUserNotificationSettings(forTypes: .Alert, categories: nil)
UIApplication.sharedApplication().registerUserNotificationSettings(pushSettings)
UIApplication.sharedApplication().registerForRemoteNotifications()
_ = UIApplication.sharedApplication().applicationIconBadgeNumber
UIApplication.sharedApplication().cancelAllLocalNotifications()
UIApplication.sharedApplication().applicationIconBadgeNumber = 0
return true
}
func application( application: UIApplication, didRegisterForRemoteNotificationsWithDeviceToken deviceToken: NSData ) {
let characterSet: NSCharacterSet = NSCharacterSet( charactersInString: "<>" )
let deviceTokenString: String = ( deviceToken.description as NSString )
.stringByTrimmingCharactersInSet( characterSet )
.stringByReplacingOccurrencesOfString( " ", withString: "" ) as String
print( deviceTokenString )
let defaults = NSUserDefaults.standardUserDefaults()
defaults.setObject(deviceTokenString, forKey: "device_token")
defaults.synchronize()
}
func application( application: UIApplication, didFailToRegisterForRemoteNotificationsWithError error: NSError ) {
print( error.localizedDescription )
}
func application(application: UIApplication, didReceiveRemoteNotification userInfo: [NSObject : AnyObject]) {
}
func application(application: UIApplication, didReceiveRemoteNotification userInfo: [NSObject : AnyObject], fetchCompletionHandler completionHandler: (UIBackgroundFetchResult) -> Void) {
}
I get the device token successfully and print it. But whenever I delete the application and rebuilds and reinstalls it, the device token changes. Yet, it remains the same until I delete the application. But every time I delete I get a new device token. This was not happening in iOS 8.x . I don't know why its happening on iOS 9.1.
In case of iOS 8.x I use to get same device token even if I delete the app. Anyone has faced this issue. Is this normal.
NOTE: I am using xcode 7.1.1, iPhone with iOS 9.1 and using only development certificates and provisioning profile.
Any help is appreciated. Thanks