2015-09-28 54 views
0

我正在將分析推送通知集成到應用程序中,並且陷入Swift 2.0轉換。 代碼是:將分析推送通知集成到Xcode 7項目中(Swift)

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

Xcode中抱怨說,「無法將類型的價值‘UIUserNotificationType’預期參數類型‘UIRemoteNotificationType’

回答

2

試試這個代碼...

應用delegate.swift

import UIKit 
import Parse 

@UIApplicationMain 
class AppDelegate: UIResponder, UIApplicationDelegate { 

var window: UIWindow? 

//-------------------------------------- 
// MARK: - UIApplicationDelegate 
//-------------------------------------- 

func application(application: UIApplication, didFinishLaunchingWithOptions launchOptions: [NSObject: AnyObject]?) -> Bool { 
    Parse.setApplicationId("APIKEYHERE", clientKey: "CLIENTKEYHERE") 

    let types:UIUserNotificationType = [UIUserNotificationType.Alert, UIUserNotificationType.Badge, UIUserNotificationType.Sound] 
    let settings:UIUserNotificationSettings = UIUserNotificationSettings(forTypes: types, categories: nil) 

    application.registerUserNotificationSettings(settings) 
    application.registerForRemoteNotifications() 

    return true 
} 

//-------------------------------------- 
// MARK: Push Notifications 
//-------------------------------------- 

func application(application: UIApplication, didRegisterForRemoteNotificationsWithDeviceToken deviceToken: NSData) { 
    let installation = PFInstallation.currentInstallation() 
    installation.setDeviceTokenFromData(deviceToken) 
    installation.saveInBackground() 

    PFPush.subscribeToChannelInBackground("") { (succeeded: Bool, error: NSError?) in 
     if succeeded { 
      print("ParseStarterProject successfully subscribed to push notifications on the broadcast channel.\n"); 
     } else { 
      print("ParseStarterProject failed to subscribe to push notifications on the broadcast channel with error = %@.\n", error) 
     } 
    } 
} 

func application(application: UIApplication, didFailToRegisterForRemoteNotificationsWithError error: NSError) { 
    if error.code == 3010 { 
     print("Push notifications are not supported in the iOS Simulator.\n") 
    } else { 
     print("application:didFailToRegisterForRemoteNotificationsWithError: %@\n", error) 
    } 
} 

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

} 
3

嘗試下面的代碼,雖然Xcode中仍然顯示UIRemoteNotificationType iOS中被棄用警告8.0

let types : UIRemoteNotificationType = [.Badge, .Alert, .Sound] 
application.registerForRemoteNotificationTypes(types) 
6

嘗試下面的代碼,因爲UIRemoteNotification已過時,使用registerUserNotificationSettigns設置通知設置。但要記住配置推送通知在蘋果的會員中心和解析,以便它可以工作。你可以按照這個教程從第1步到第4步,這很好。 https://www.parse.com/tutorials/ios-push-notifications

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