gpt4 book ai didi

ios - didRegisterForRemoteNotificationsWithDeviceToken 不返回 deviceToken

转载 作者:行者123 更新时间:2023-11-28 21:44:09 25 4
gpt4 key购买 nike

我以这种方式通过 Parse 注册推送通知:

func application(application: UIApplication, didFinishLaunchingWithOptions launchOptions: [NSObject: AnyObject]?) -> Bool {
Parse.setApplicationId("RfSGbVes0FGIX1sfxTEb3iybVsKgKPrfDuxco3vC", clientKey: "3pFBMar6vO6iUJouqTMt4VJVKZaXUc6p9RgHzTep")
if application.applicationState != UIApplicationState.Background {
let preBackgroundPush = !application.respondsToSelector("backgroundRefreshStatus")
let oldPushHandlerOnly = !self.respondsToSelector("application:didReceiveRemoteNotification:fetchCompletionHandler:")
var pushPayload = false
if let options = launchOptions {
pushPayload = options[UIApplicationLaunchOptionsRemoteNotificationKey] != nil
}
if (preBackgroundPush || oldPushHandlerOnly || pushPayload) {
PFAnalytics.trackAppOpenedWithLaunchOptionsInBackground(launchOptions, block: nil)
}
}
let userNotificationTypes = UIUserNotificationType.Alert | UIUserNotificationType.Badge | UIUserNotificationType.Sound
let settings = UIUserNotificationSettings(forTypes: userNotificationTypes, categories: nil)
application.registerUserNotificationSettings(settings)
application.registerForRemoteNotifications()
return true
}

func application(application: UIApplication, didRegisterForRemoteNotificationsWithDeviceToken deviceToken: NSData) {
let installation = PFInstallation.currentInstallation()
installation.badge = 0
println(installation.deviceToken) //deviceToken is nil
let standardUserDefaults = NSUserDefaults.standardUserDefaults()
standardUserDefaults.setObject(installation.deviceToken, forKey: "parseDeviceToken")
standardUserDefaults.synchronize()
delegate?.didFinishSettingToken()
installation.setDeviceTokenFromData(deviceToken)
installation.saveInBackgroundWithBlock(nil)
}

当我运行应用程序时,我从 println(installation.deviceToken)

得到一个 nil 值

也许还有其他方法可以处理deviceToken

编辑:如果我停止应用程序并再次运行它,它会给出 deviceToken。我只有在第一次运行应用程序时才收到 deviceToken

最佳答案

根据苹果documentation :

The first time you register your app’s preferred notification types, the system asks the user whether your app should be allowed to deliver notifications and stores the user’s response. The system does not prompt the user during subsequent registration attempts. The user can always change the notification preferences using the Settings app.

optional func application(_ application: UIApplication, didRegisterUserNotificationSettings notificationSettings: UIUserNotificationSettings)
{
application.registerForRemoteNotifications()
}

你还必须添加

UIApplication.sharedApplication().registerForRemoteNotificationSettings(settings)

对于 iOS 7 和 8 版本问题,请查看此 tutorial .

希望对您有所帮助。

关于ios - didRegisterForRemoteNotificationsWithDeviceToken 不返回 deviceToken,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/30953457/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com