Rajan Maheshwari
Rajan Maheshwari

Reputation: 14571

Multiple device tokens for single bundleId in development mode iOS 9.1

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.

enter image description here

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

Upvotes: 1

Views: 1271

Answers (1)

teamnorge
teamnorge

Reputation: 794

With reference to: Local and Remote Notification Programming Guide

Device tokens can change, so your app needs to reregister every time it is launched.

So, Apple never guaranteed the device token is the same for the same device, I would simply suggest adjusting your logic. Who knows how this could behave in further iOS releases. This is not a bug.

Upvotes: 2

Related Questions