2015-09-28 49 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() 
}