Parse Register for Push Notification - 如果用户未登录则崩溃

时间:2016-01-01 02:19:08

标签: swift parse-platform push-notification

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

这是我的代码

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()。提前致谢

1 个答案:

答案 0 :(得分:1)

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

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