[英]Swift with Parse: Error when adding push notification support to appDelegate
我正在嘗試使用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
在遵循了Parse教程以使用Apple Developer Console設置推送通知和證書之后,請確保您的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
}
}
let settings = UIUserNotificationSettings(forTypes: [.Alert,.Badge,.Sound], categories: nil)
application.registerUserNotificationSettings(settings)
application.registerForRemoteNotifications()
如果您使用的是Swift 2.0,這可能會很有用,因為某些錯誤提示
二進制運算符不能應用於兩個UIUserNotificationType
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.