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

Posted

技术标签:

【中文标题】解析注册推送通知 - 如果用户未登录则崩溃【英文标题】:Parse Register for Push Notification - Crash if user not signed in 【发布时间】:2016-01-01 02:19:07 【问题描述】:

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

这是我的代码

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】:

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

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

【讨论】:

以上是关于解析注册推送通知 - 如果用户未登录则崩溃的主要内容,如果未能解决你的问题,请参考以下文章

MFP 应用程序在应用程序未运行时在推送通知期间点击时崩溃

在打开推送通知时解析推送“未找到活动”

应用程序未运行时的远程推送通知崩溃

关闭应用程序后解析推送通知崩溃

有推送通知问题的登录/注销用户

登录/注册后如何注册推送通知?