gpt4 book ai didi

swift - 解析推送通知注册 - 如果用户未登录则崩溃

转载 作者:行者123 更新时间:2023-11-30 10:08:37 25 4
gpt4 key购买 nike

所以我有推送通知设置,作为注册推送通知的一部分,我需要将当前用户添加到安装表中。这一切正常,直到没有用户登录为止。

这是我的代码

func application(application: UIApplication, didFinishLaunchingWithOptions launchOptions: [NSObject: AnyObject]?) -> Bool {

let types:UIUserNotificationType = [UIUserNotificationType.Alert, UIUserNotificationType.Badge, UIUserNotificationType.Sound]
let settings:UIUserNotificationSettings = UIUserNotificationSettings(forTypes: types, categories: nil)

application.registerUserNotificationSettings(settings)
application.registerForRemoteNotifications()

return true
}

func application(application: UIApplication, didRegisterForRemoteNotificationsWithDeviceToken deviceToken: NSData) {
let installation = PFInstallation.currentInstallation()
installation.setObject(PFUser.currentUser()!, forKey: "user")
installation.setDeviceTokenFromData(deviceToken)
installation.saveInBackground()
}

func application(application: UIApplication, didFailToRegisterForRemoteNotificationsWithError error: NSError) {
if error.code == 3010 {
print("Push notifications are not supported in the iOS Simulator.")
} else {
print("application:didFailToRegisterForRemoteNotificationsWithError: %@", error)
}
}

我需要一种在安装表中注册用户的方法,如果没有用户,它不会崩溃,我会做一个简单的检查以查看是否有用户登录,然后如果有用户则运行代码,但是如果有人向他们发送通知,他们将不会收到通知,因为他们的 PFUser.currentUser() 尚未添加。提前致谢

最佳答案

您是否尝试过调查匿名用户 ( http://blog.parse.com/announcements/protect-user-data-with-new-parse-features/ )?这允许您为注销用户创建 PFUser。

这样,您仍然可以通过 PFUser.currentUser() 调用保存当前安装的 PFUser 引用,但用户不必注册。

关于swift - 解析推送通知注册 - 如果用户未登录则崩溃,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/34553082/

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