我可以使用GCM注册ID向后端注册我的设备。但是我无法在iPad / iPhone上收到GCM消息,即使服务器(.NET)收到成功消息。我可以通过名为APN Tester的第三方工具发送通知。我无法找到这个问题。我检查了所有配置文件,证书,重新安装应用程序。有人可以帮我吗?
在我的AppDelegate.Swift中,
func application(application: UIApplication, didFinishLaunchingWithOptions launchOptions: [NSObject: AnyObject]?) -> Bool {
// Override point for customization after application launch.
dispatch_async(dispatch_get_main_queue(),{
let internet = Server.isConnectedToInternet()
if !internet{
}else{
GMSServices.provideAPIKey(self.googleMapsApiKey)
var configureError:NSError?
GGLContext.sharedInstance().configureWithError(&configureError)
assert(configureError == nil, "Error configuring Google services: \(configureError)")
self.gcmSenderID = GGLContext.sharedInstance().configuration.gcmSenderID
let application = UIApplication.sharedApplication()
let settings: UIUserNotificationSettings =
UIUserNotificationSettings(forTypes: [.Alert, .Badge, .Sound], categories: nil)
application.registerUserNotificationSettings(settings)
application.registerForRemoteNotifications()
GCMService.sharedInstance().startWithConfig(GCMConfig.defaultConfig())
}
})
return true
}
func application( application: UIApplication, didRegisterForRemoteNotificationsWithDeviceToken deviceToken: NSData ) {
print("didRegisterForRemoteNotificationsWithDeviceToken \(deviceToken)")
// [END receive_apns_token]
// [START get_gcm_reg_token]
// Create a config and set a delegate that implements the GGLInstaceIDDelegate protocol.
let instanceIDConfig = GGLInstanceIDConfig.defaultConfig()
instanceIDConfig.delegate = self
// Start the GGLInstanceID shared instance with that config and request a registration
// token to enable reception of notifications
GGLInstanceID.sharedInstance().startWithConfig(instanceIDConfig)
//Change kGGLInstanceIDAPNSServerTypeSandboxOption=true for testing and kGGLInstanceIDAPNSServerTypeSandboxOption=false for production
#if DEBUG
registrationOptions = [kGGLInstanceIDRegisterAPNSOption:deviceToken, kGGLInstanceIDAPNSServerTypeSandboxOption:true]
#else
registrationOptions = [kGGLInstanceIDRegisterAPNSOption:deviceToken, kGGLInstanceIDAPNSServerTypeSandboxOption:false]
#endif
GGLInstanceID.sharedInstance().tokenWithAuthorizedEntity(gcmSenderID, scope: kGGLInstanceIDScopeGCM, options: registrationOptions, handler: registrationHandler)
// [END get_gcm_reg_token]
}
func application( application: UIApplication, didFailToRegisterForRemoteNotificationsWithError error: NSError ) {
print("Registration for remote notification failed with error: \(error.localizedDescription)")
}
func application( application: UIApplication, didReceiveRemoteNotification userInfo: [NSObject : AnyObject], fetchCompletionHandler handler: (UIBackgroundFetchResult) -> Void) {
print("Notification received: \(userInfo)")
MessageHandler().handleMessage(userInfo)
handler(UIBackgroundFetchResult.NoData);
}
func registrationHandler(registrationToken: String!, error: NSError!) {
if (registrationToken != nil) {
self.registrationToken = registrationToken
if parent != nil{
parent?.gcmKey = registrationToken
parent?.saveManagedParent()
sendGCMKeyGBox()
}
print("Registration Token: \(registrationToken)")
} else {
print("Registration to GCM failed with error: \(error.localizedDescription)")
}
}
func sendGCMKeyGBox() {
let senderID = (UIApplication.sharedApplication().delegate as! AppDelegate).gcmSenderID!
print(senderID)
let params: [String:AnyObject] = [
"appId" : (parent!.appId),
"registrationId" : (parent!.gcmKey),
"senderId" : senderID,
"isAndroid" : "0"
]
Server.sendPost(URL.GCMRegistration, params: params, completionHandler: sendGCMSenderIDCompletionHandler)
}
以上是我在AppDelegate Class中使用的GCM函数。