ios - 解析 - 设置设备 token

标签 ios xcode swift parse-platform

我正在尝试在解析中设置设备 token 。官方文档给出了如下代码来实现这一点

官方文档( objective-c )

- (void)application:(UIApplication *)application didRegisterForRemoteNotificationsWithDeviceToken:(NSData *)deviceToken {
  // Store the deviceToken in the current installation and save it to Parse.
  PFInstallation *currentInstallation = [PFInstallation currentInstallation];
  [currentInstallation setDeviceTokenFromData:deviceToken];
  currentInstallation.channels = @[ @"global" ];
  [currentInstallation saveInBackground];
}

我已按如下方式转换代码但收到错误。

我的代码(Swift)

func application( applcation: UIApplication!, didRegisterForRemoteNotificationsWithDeviceToken deviceToken: NSData! ) {
    println(deviceToken)

  let currentInstallation = PFInstallation.currentInstallation()

            currentInstallation .setDeviceTokenFromData(deviceToken)
            currentInstallation .setObject(PFUser.currentUser(), forKey: "owner")
            currentInstallation .addUniqueObject("Test", forKey: "channels")
            currentInstallation .save()

我在运行我的代码时收到以下错误:

 Break on warnBlockingOperationOnMainThread() to debug.
2014-11-13 03:44:01.306 Meetr[8855:2084537] Error: invalid type for key deviceToken, expected array, but got string (Code: 111, Version: 1.4.2)
sent

谁能帮我看看这是为什么?我很困惑,因为我只是将原始 objective-c 代码转换为 swift。

提前致谢。

最佳答案

我刚刚设置了我的 Parse 通知,没有任何问题。你有错误的那一行正是我的错误所在,所以我认为这不是问题所在。这是我在 App Delegate 中的所有内容:

    func application(application: UIApplication!, didFinishLaunchingWithOptions launchOptions: NSDictionary!) -> Bool {
    // Override point for customization after application launch.

    //ENABLE PUSH NOTIFICATIONS
    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!) {
    //Store the deviceToken in the current installation and save it to Parse.

    let currentInstallation: PFInstallation = PFInstallation.currentInstallation()
    currentInstallation.setDeviceTokenFromData(deviceToken)
    currentInstallation.saveInBackground()
}

func application(application: UIApplication!, didReceiveRemoteNotification userInfo: NSDictionary!) {
    PFPush.handlePush(userInfo)
}

关于ios - 解析 - 设置设备 token ,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/26901004/

相关文章:

ios - App Transport Security 在允许时阻止 http 请求

ios - Xcode 6.3.2 - 无法通过 MapKit 获取位置

xcode - 如何找到碰撞约束?

objective-c - 在 ARC 中保留循环

Swift:表格 View 中的动态单元格

ios - 在 Swift 中使用 Google Places API 从 JSON 数据创建对象

ios - Swift:声音在模拟器上播放但不在设备上播放

javascript - 如何在iPhone 6上安装expo.ipa?

ios - 如何在可见之前开始滚动 UIPickerView?

ios - UITableView的第一个cell是空白的(UIView中的TableView)