我将GCM集成到我的应用程序中的代码是:
import UIKit
import GoogleMaps
import FBSDKLoginKit
@available(iOS 9.0, *)
@UIApplicationMain
class AppDelegate: UIResponder, UIApplicationDelegate, GGLInstanceIDDelegate, GCMReceiverDelegate {
var window: UIWindow?
var connectedToGCM = false
var gcmSenderID: String?
var registrationOptions = [String: AnyObject]()
func application(application: UIApplication, didFinishLaunchingWithOptions launchOptions: [NSObject: AnyObject]?) -> Bool {
var configureError:NSError?
GGLContext.sharedInstance().configureWithError(&configureError)
assert(configureError == nil, "Error configuring Google services: \(configureError)")
gcmSenderID = GGLContext.sharedInstance().configuration.gcmSenderID
application.registerUserNotificationSettings(UIUserNotificationSettings(forTypes: [.Alert, .Badge, .Sound], categories: nil))
application.registerForRemoteNotifications()
let gcmConfig = GCMConfig.defaultConfig()
gcmConfig.receiverDelegate = self
GCMService.sharedInstance().startWithConfig(gcmConfig)
if(launchOptions?[UIApplicationLaunchOptionsLocationKey] != nil){
LocationService.sharedInstance.startMonitoringLocation()
}
return FBSDKApplicationDelegate.sharedInstance().application(application, didFinishLaunchingWithOptions: launchOptions)
}
func application(application: UIApplication, didRegisterForRemoteNotificationsWithDeviceToken deviceToken: NSData) {
let instanceIDConfig = GGLInstanceIDConfig.defaultConfig()
instanceIDConfig.delegate = self
GGLInstanceID.sharedInstance().startWithConfig(instanceIDConfig)
let registrationOptions = [kGGLInstanceIDRegisterAPNSOption:deviceToken,
kGGLInstanceIDAPNSServerTypeSandboxOption:true]
GGLInstanceID.sharedInstance().tokenWithAuthorizedEntity(gcmSenderID,
scope: kGGLInstanceIDScopeGCM, options: registrationOptions, handler: registrationHandler)
}
func application(application: UIApplication, didReceiveRemoteNotification userInfo: [NSObject : AnyObject], fetchCompletionHandler completionHandler: (UIBackgroundFetchResult) -> Void) {
print(userInfo)
completionHandler(UIBackgroundFetchResult.NewData)
}
func applicationDidEnterBackground(application: UIApplication) {
GCMService.sharedInstance().disconnect()
connectedToGCM = false
LocationService.sharedInstance.restartMonitoringLocation()
}
func applicationDidBecomeActive(application: UIApplication) {
GCMService.sharedInstance().connectWithHandler({(error:NSError?) -> Void in
if let error = error {
print("Could not connect to GCM: \(error.localizedDescription)")
} else {
self.connectedToGCM = true
print("Connected to GCM")
}
})
FBSDKAppEvents.activateApp()
}
func applicationWillTerminate(application: UIApplication) {
// Called when the application is about to terminate. Save data if appropriate. See also applicationDidEnterBackground:.
LocationService.sharedInstance.startUpdatingLocationTerminate()
}
func onTokenRefresh() {
// A rotation of the registration tokens is happening, so the app needs to request a new token.
print("The GCM registration token needs to be changed.")
GGLInstanceID.sharedInstance().tokenWithAuthorizedEntity(gcmSenderID,
scope: kGGLInstanceIDScopeGCM, options: registrationOptions, handler: registrationHandler)
}
func registrationHandler(registrationToken: String!, error: NSError!) {
if (registrationToken != nil) {
Gideon.notifId = registrationToken
print("GCM token: \(registrationToken)")
} else {
print("Registration to GCM failed with error: \(error.localizedDescription)")
}
}
这是我的日志:
2016-04-25 22:57:41.805 TestApp [2044:] App测量 v.2003000启动2016-04-25 22:57:41.805 TestApp [2044:] To enable debug logging设置以下应用程序参数: -GMRDebugEnabled 2016-04-25 22:57:41.848:GCM | GCM库版本1.1.4 2016-04-25 22:57:41.868:GCM |无效 checkin plist中的密钥:GMSInstanceIDDeviceDataVersion GCM令牌: k6uFlJWBSEg:APA91bGASVNpk-TwgturFp8BSVccFmPKtVLIg7MS6tcWNp4nvB26o3zdFVLUq6oqE1DtJOFAISiS6dZFu_Tt_p5c4v1WiQg7kZcq0hNIZKeabhx0Mg-2uzkJIkg-pvmpcvOHSBmtXYVg 2016-04-25 22:57:42.331 TestApp [2044:] App测量 启用2016-04-25 22:58:44.642 TestApp [2044:414848] CFNetwork SSLHandshake失败(-9806)2016-04-25 22:59:13.425 TestApp [2044:414848] CFNetwork SSLHandshake失败(-9806)2016-04-25 22:59:34.918 Gideon [2044:414848] CFNetwork SSLHandshake失败(-9806) 连接到GCM 2016-04-25 23:09:54.593 TestApp [2044:414848] CFNetwork SSLHandshake失败(-9806)2016-04-25 23:10:08.681 TestApp [2044:414848] CFNetwork SSLHandshake失败(-9806)2016-04-25 23:10:27.482 TestApp [2044:414848] CFNetwork SSLHandshake失败(-9806) 无法连接到GCM:操作无法完成。 (com.google.gcm错误2001年。)
如何解决这些错误:
GCM |签入plist中的密钥无效:GMSInstanceIDDeviceDataVersion
CFNetwork SSLHandshake失败(-9806)
无法连接到GCM:操作无法完成。 (com.google.gcm错误2001年。)
提前感谢