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

Rajan Maheshwari
  • 14,465
  • 6
  • 64
  • 98
  • Not related to device token, but could help you in keeping track of device if app is deleted and re-installed http://stackoverflow.com/questions/21878560/how-to-preserve-identifierforvendor-in-ios-after-uninstalling-ios-app-on-device – sbarow Nov 15 '15 at 09:57
  • sbarow. Thanks. Yes,I do know about keychains and UUID's but the main concern was with device tokens. Anyways thanks. – Rajan Maheshwari Nov 19 '15 at 08:43

1 Answers1

2

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.

teamnorge
  • 784
  • 5
  • 9
  • Yes, we may get different tokens. But with iOS 8 i used to get only a single token everytime even if I uninstall the build and reinstall it – Rajan Maheshwari Nov 19 '15 at 08:42
  • @teamnorge But the multiple tokens are valid if i send notification to all devices will be received on the same device this is a bug – khaled Dec 02 '15 at 09:57
  • @khaled I don't think there is a bug there, have you encountered such a situation? Apple recommends using special Feedback Service (https://developer.apple.com/library/ios/documentation/NetworkingInternet/Conceptual/RemoteNotificationsPG/Chapters/CommunicatingWIthAPS.html) for maintaining expired tokens/notifications. You may get the info which tokens expired and in connection with registering new device token on app start this provides you quite a stable notification flow. There are some exceptions of course, but I do not think you can really send two push notifications to the same device. – teamnorge Dec 02 '15 at 13:48
  • I confirm that it is true! Apple sends two or even three! push notifications from my server in such situation. All tokens are valid at the same time on the same device. Very hope it concerns only developer profile. – malex Apr 26 '16 at 18:04