【问题标题】:Parse Push Notifications - Swift Installation Not Working解析推送通知 - Swift 安装不起作用
【发布时间】:2014-12-17 12:25:41
【问题描述】:

我正在尝试让 Parse 推送通知在我的应用程序上运行(全部快速),但在尝试实施时,我收到错误 'PFInstallation' does not have a member named 'saveInBackground'

这是我的代码。

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

    Parse.setApplicationId("APP ID HIDDEN", clientKey: "CLIENT ID HIDDEN")

   // let notificationTypes:UIUserNotificationType = UIUserNotificationType.Alert | UIUserNotificationType.Badge | UIUserNotificationType.Sound
    //let notificationSettings:UIUserNotificationSettings = UIUserNotificationSettings(forTypes: notificationTypes, categories: nil)
    var notificationType: UIUserNotificationType = UIUserNotificationType.Alert | UIUserNotificationType.Badge | UIUserNotificationType.Sound

    var settings: UIUserNotificationSettings = UIUserNotificationSettings(forTypes: notificationType, categories: nil)
    UIApplication.sharedApplication().registerUserNotificationSettings(settings)
    UIApplication.sharedApplication().registerForRemoteNotifications()

    //UIApplication.sharedApplication().registerUserNotificationSettings(notificationSettings)
    // Override point for customization after application launch.
    return true
}

func application(application: UIApplication, didRegisterUserNotificationSettings notificationSettings: UIUserNotificationSettings!) {
   UIApplication.sharedApplication().registerForRemoteNotifications()

}

func application(application: UIApplication, didRegisterForRemoteNotificationsWithDeviceToken deviceToken: NSData) {

    var currentInstallation: PFInstallation = PFInstallation()
    currentInstallation.setDeviceTokenFromData(deviceToken)
    currentInstallation.saveInBackground()

    println("got device id! \(deviceToken)")

}


func application(application: UIApplication, didFailToRegisterForRemoteNotificationsWithError error: NSError) {
    println(error.localizedDescription)
    println("could not register: \(error)")
}

func application(application: UIApplication, didReceiveRemoteNotification userInfo: [NSObject : AnyObject]) {
    PFPush.handlePush(userInfo)
}

当我将 currentInstallation.saveInBackground 更改为 currentInstallation.saveEvenutally() 时,代码编译正常..

但是在尝试成功注册推送通知时,控制台中会弹出错误消息Error: deviceType must be specified in this operation (Code: 135, Version: 1.4.2)

我已经花了几个小时试图解决这个问题,没有骰子,感谢任何帮助。

【问题讨论】:

    标签: swift notifications parse-platform push


    【解决方案1】:

    对于遇到此错误的其他人,请确保将 Bolts 框架导入到您的桥接头文件中

    他们的废话文档中没有概述。

    这解决了问题。

    下面是代码。

    #import <Parse/Parse.h>
    #import <Bolts/Bolts.h>
    

    只需将其添加到您的桥接头中即可。谢谢

    【讨论】:

    • 这仍然无法为我解决问题 :(,这样做 + var currentInstallation: PFInstallation = PFInstallation.currentInstallation() 工作
    • import Bolts 就是这样
    【解决方案2】:

    有效的 PFInstallation 只能通过 [PFInstallation currentInstallation] 实例化,因为所需的标识符字段是只读的。 (source)

    所以而不是:

    var currentInstallation: PFInstallation = PFInstallation()
    

    试试:

    var currentInstallation = PFInstallation.currentInstallation()
    

    【讨论】:

    • 好吧,这没什么用。仍然有错误“'PFInstallation'没有名为'saveInBackground'的成员”
    • 它可以很好地获取设备ID,只是不会在 Parse 端注册为安装。不会收到推送的
    • 好吧,在您使用保存方法之前,您不会收到推送。 currentInstallation 出于某种原因可能是可选的吗?按住 Option 键并单击它,看看它的类型是否为 PFInstallation?
    • 我看不出它是可选的。
    • 我不知道该告诉你什么!您确定 saveInBackground 存在于您正在使用的 Parse 版本中,并且您的对象不是可选的(PFInstallation!PFInstallation?)? Here 是一个有效的实现……
    【解决方案3】:

    只需在 AppDelegate.swift 文件中写入 import Bolts

    【讨论】:

    • 这应该是公认的答案。当前接受的答案不起作用。
    【解决方案4】:

    除了导入螺栓之外,我还通过将函数更改为

    修复了我的应用程序中的相同错误
     func application(application: UIApplication, didRegisterForRemoteNotificationsWithDeviceToken deviceToken: NSData) {
        // Store the deviceToken in the current Installation and save it to Parse
        let installation = PFInstallation.currentInstallation()
        installation.setDeviceTokenFromData(deviceToken)
        installation.saveInBackground()
    }
    

    来自关于推送通知的 Parse 指南(相对于快速入门指南):https://parse.com/docs/ios/guide#push-notifications

    【讨论】:

      猜你喜欢
      • 2016-05-09
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多