Swift with Parse:向appDelegate添加推送通知支持时出错

时间:2015-07-27 13:59:29

标签: ios xcode swift parse-platform

我正在尝试使用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

2 个答案:

答案 0 :(得分:1)

在您使用Apple Developer Console设置推送通知和证书的Parse教程之后,请确保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
    }
}

答案 1 :(得分:0)

 let settings = UIUserNotificationSettings(forTypes: [.Alert,.Badge,.Sound], categories: nil)
    application.registerUserNotificationSettings(settings)
    application.registerForRemoteNotifications()

如果您使用Swift 2.0,这可能很有用,因为有些错误说

  

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