GCM iOS未收到推送通知

时间:2016-03-05 12:39:17

标签: ios swift push-notification google-cloud-messaging apple-push-notifications

在iOS中使用GCM进行推送通知。一切都很好。我获得了registrationToken值&也订阅了ToTopic成功。连接到GCM。 didRegisterForRemoteNotificationsWithDeviceToken方法也被调用。但没有得到推送通知。 didReceiveRemoteNotification方法从未调用过。在我的Android应用程序上,我没有任何问题地获得推送通知。但在iOS通知中从未收到过。这是源代码:

    class AppDelegate: UIResponder, UIApplicationDelegate, GGLInstanceIDDelegate, GCMReceiverDelegate{
var gcmSenderID: String?
var registrationToken: String?
let registrationKey = "onRegistrationCompleted"
let messageKey = "onMessageReceived"
var registrationOptions = [String: AnyObject]()
var connectedToGCM = false
let subscriptionTopic = "/topics/global"
var subscribedToTopic = false

func application(application: UIApplication, didFinishLaunchingWithOptions launchOptions: [NSObject: AnyObject]?) -> Bool {

    var configureError:NSError?

    GGLContext.sharedInstance().configureWithError(&configureError)

    gcmSenderID = GGLContext.sharedInstance().configuration.gcmSenderID

    if application.respondsToSelector("registerUserNotificationSettings:") {

        let types:UIUserNotificationType = (.Alert | .Badge | .Sound)
        let settings:UIUserNotificationSettings = UIUserNotificationSettings(forTypes: types, categories: nil)
        application.registerUserNotificationSettings(settings)
        application.registerForRemoteNotifications()

    } else {
        // Register for Push Notifications before iOS 8
        application.registerForRemoteNotificationTypes(.Alert | .Badge | .Sound)
    }
    let gcmConfig = GCMConfig.defaultConfig()
    gcmConfig.receiverDelegate = self
    GCMService.sharedInstance().startWithConfig(gcmConfig)

    return true

}

func application(application: UIApplication, didReceiveRemoteNotification userInfo: [NSObject : AnyObject]) {
    print("remote notification")
    GCMService.sharedInstance().appDidReceiveMessage(userInfo);

}
func application( application: UIApplication,
    didReceiveRemoteNotification userInfo: [NSObject : AnyObject],
    fetchCompletionHandler handler: (UIBackgroundFetchResult) -> Void) {
        GCMService.sharedInstance().appDidReceiveMessage(userInfo);

   NSNotificationCenter.defaultCenter().postNotificationName(messageKey, object: nil,
            userInfo: userInfo)
        handler(UIBackgroundFetchResult.NoData);


}
func application( application: UIApplication, didRegisterForRemoteNotificationsWithDeviceToken
    deviceToken: NSData ) {

        let instanceIDConfig = GGLInstanceIDConfig.defaultConfig()
        instanceIDConfig.delegate = self

     GGLInstanceID.sharedInstance().startWithConfig(instanceIDConfig)
        registrationOptions = [kGGLInstanceIDRegisterAPNSOption:deviceToken,
            kGGLInstanceIDAPNSServerTypeSandboxOption:true]

        GGLInstanceID.sharedInstance().tokenWithAuthorizedEntity(gcmSenderID,
            scope: kGGLInstanceIDScopeGCM, options: registrationOptions, handler: registrationHandler)
        // [END get_gcm_reg_token]
}
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) {

        self.registrationToken = registrationToken
        print("Registration Token: \(registrationToken)")
        self.subscribeToTopic()
        let userInfo = ["registrationToken": registrationToken]
        NSNotificationCenter.defaultCenter().postNotificationName(
            self.registrationKey, object: nil, userInfo: userInfo)
    } else {
        print("Registration to GCM failed with error: \(error.localizedDescription)")
        let userInfo = ["error": error.localizedDescription]
        NSNotificationCenter.defaultCenter().postNotificationName(
            self.registrationKey, object: nil, userInfo: userInfo)
    }
}
func subscribeToTopic() {

    // topic
    if(registrationToken != nil && connectedToGCM) {
        GCMPubSub.sharedInstance().subscribeWithToken(self.registrationToken, topic: subscriptionTopic,
            options: nil, handler: {(NSError error) -> Void in
                if (error != nil) {
                    // Treat the "already subscribed" error more gently
                    if error.code == 3001 {
                        print("Already subscribed to \(self.subscriptionTopic)")
                    } else {
                        print("Subscription failed: \(error.localizedDescription)");
                    }
                } else {
                    self.subscribedToTopic = true;
                    NSLog("Subscribed to \(self.subscriptionTopic)");
                }
        })
    }
}


func applicationDidBecomeActive(application: UIApplication) {
    print("applicationDidBecomeActive")
    self.globalPrice = CartLocalData.getTotalPrice()
     GCMService.sharedInstance().connectWithHandler({
        (NSError error) -> Void in
        if error != nil {
            print("Could not connect to GCM: \(error.localizedDescription)")

        } else {
            self.connectedToGCM = true
            self.subscribeToTopic()
            print("Connected to GCM")
            // ...
        }
    })

}

}

我做错了什么。谁能建议??

1 个答案:

答案 0 :(得分:0)

您应该可以选择打开功能部分。您还需要在Apple成员中心构建单独的密钥/证书对,并将其包含在您的计算机上。这是我制作的新项目的截图。

capabilities section

听起来好像这个过程中的一个步骤可能没有完成。

重新开始,试着找出你遗失的东西。这是一个ray wenderlich教程,引导您完成步骤here

与Apple开发者网站的链接,深入讨论here