【问题标题】:Swift with Parse: Error when adding push notification support to appDelegateSwift with Parse:向 appDelegate 添加推送通知支持时出错
【发布时间】:2015-07-27 13:59:28
【问题描述】:

我正在尝试使用 Swift 将解析添加到我的 appDelegate。我收到一个错误提示

无法使用“(UIUserNotificationType)”类型的参数列表调用“registerForRemoteNotifications”

这是我的代码。怎么了?

    if application.respondsToSelector("registerUserNotificationSettings:") {
        let userNotificationTypes = UIUserNotificationType.Alert | UIUserNotificationType.Badge | UIUserNotificationType.Sound
        let settings = UIUserNotificationSettings(forTypes: userNotificationTypes, categories: nil)
        application.registerUserNotificationSettings(settings)
        application.registerForRemoteNotifications()
    } else {
        let types = UIUserNotificationType.Badge | UIUserNotificationType.Alert | UIUserNotificationType.Sound
        application.registerForRemoteNotifications(types)
    }
    return true

【问题讨论】:

  • 你被写了两次`application.registerForRemoteNotifications(OoO)`,但每次都不同。一个没有 OoO,另一个带有 UIUserNotificationSettings 的标志。根据您的消息,最后一个似乎是错误的。似乎你想要:registerForRemoteNotificationTypes(types)(在 else 测试中)
  • 后者 - 所以 Xcode 说 - 不工作......
  • 因为 iOS8 中的类型也发生了变化。之前是UIRemoteNotificationType 而不是UIUserNotificationType => let types = UIRemoteNotificationType.Badge | UIRemoteNotificationType.Alert | UIRemoteNotificationType.Sound application.registerForRemoteNotificationTypes(types)}

标签: ios xcode swift parse-platform


【解决方案1】:

按照 Parse 教程使用 Apple 开发者控制台设置推送通知和证书后,请确保您的 AppDelegate.swift 如下所示:

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

    Parse.setApplicationId("ID", clientKey:"KEY")

    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 installation = PFInstallation.currentInstallation()
    installation.setDeviceTokenFromData(deviceToken)
    installation.addUniqueObject("Chat", forKey: "channels")
    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)
    }
}

func application(application: UIApplication, didReceiveRemoteNotification userInfo: [NSObject : AnyObject]) {
    if application.applicationState == UIApplicationState.Inactive {
        PFAnalytics.trackAppOpenedWithRemoteNotificationPayload(userInfo)
    }
}

最后一个函数会在用户打开应用时重置徽章计数器:

func applicationDidBecomeActive(application: UIApplication) {
    //Reset badge counter to zero
    var currentInstallation = PFInstallation.currentInstallation()
    if(currentInstallation.badge != 0){
        currentInstallation.badge = 0
    }
}

【讨论】:

    【解决方案2】:
     let settings = UIUserNotificationSettings(forTypes: [.Alert,.Badge,.Sound], categories: nil)
        application.registerUserNotificationSettings(settings)
        application.registerForRemoteNotifications()
    

    如果您使用的是 Swift 2.0,这可能会很有用,因为一些错误提示

    二元运算符不能应用于两个 UIUserNotificationType

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 2012-08-09
      • 1970-01-01
      • 1970-01-01
      • 2023-03-14
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多