开发模式下单个 bundleId 的多个设备令牌 iOS 9.1
Multiple device tokens for single bundleId in development mode iOS 9.1
我正在使用 Xcode 7.1.1 和 iOS 9.1.
我正面临这个奇怪的问题。我必须在应用程序中实现推送通知。我已经成功创建了证书和配置文件,并为开发模式启用了推送通知。
我在我的应用程序中保留了相同的包 ID。代码在我的 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) {
}
我成功获取了设备令牌并打印了它。但是每当我删除应用程序并重建并重新安装它时,设备令牌都会发生变化。然而,在我删除应用程序之前,它保持不变。但是每次我删除我都会得到一个新的设备令牌。这在 iOS 8.x 中没有发生。我不知道为什么它会发生在 iOS 9.1。
在 iOS 8.x 的情况下,即使我删除了应用程序,我也会使用相同的设备令牌。任何人都遇到过这个问题。这正常吗
注意:我正在使用 xcode 7.1.1,iPhone 和 iOS 9.1,并且只使用 development证书和配置文件。
任何帮助表示赞赏。谢谢
参考:Local and Remote Notification Programming Guide
Device tokens can change, so your app needs to reregister every time
it is launched.
因此,Apple 从未保证同一设备的设备令牌相同,我只是建议调整您的逻辑。谁知道这在进一步的 iOS 版本中会如何表现。这不是错误。
我正在使用 Xcode 7.1.1 和 iOS 9.1.
我正面临这个奇怪的问题。我必须在应用程序中实现推送通知。我已经成功创建了证书和配置文件,并为开发模式启用了推送通知。
我在我的应用程序中保留了相同的包 ID。代码在我的 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) {
}
我成功获取了设备令牌并打印了它。但是每当我删除应用程序并重建并重新安装它时,设备令牌都会发生变化。然而,在我删除应用程序之前,它保持不变。但是每次我删除我都会得到一个新的设备令牌。这在 iOS 8.x 中没有发生。我不知道为什么它会发生在 iOS 9.1。
在 iOS 8.x 的情况下,即使我删除了应用程序,我也会使用相同的设备令牌。任何人都遇到过这个问题。这正常吗
注意:我正在使用 xcode 7.1.1,iPhone 和 iOS 9.1,并且只使用 development证书和配置文件。
任何帮助表示赞赏。谢谢
参考:Local and Remote Notification Programming Guide
Device tokens can change, so your app needs to reregister every time it is launched.
因此,Apple 从未保证同一设备的设备令牌相同,我只是建议调整您的逻辑。谁知道这在进一步的 iOS 版本中会如何表现。这不是错误。