简体   繁体   English

GCM iOS未收到推送通知

[英]GCM iOS not receiving push notification

Using GCM for push notification in iOS. 在iOS中使用GCM进行推送通知。 Everything is set up nicely. 一切设置都很好。 I'm getting registrationToken value & also subscribeToTopic successfully. 我正在获取registrationToken值,并且还成功了subscriptionToTopic。 Connected to GCM. 已连接到GCM。 didRegisterForRemoteNotificationsWithDeviceToken method also called. didRegisterForRemoteNotificationsWithDeviceToken方法也被调用。 But not getting push notification. 但是没有得到推送通知。 didReceiveRemoteNotification method never called. didReceiveRemoteNotification方法从未调用过。 On my android app I'm getting the push notification without any problem. 在我的Android应用程序上,我收到了推送通知,没有任何问题。 But in iOS notification never received. 但是在iOS中,从未收到通知。 Here is the source code: 这是源代码:

    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")
            // ...
        }
    })

}

} }

What am I doing wrong. 我究竟做错了什么。 Can anyone suggest?? 谁能建议?

you should have the option to turn on push from capabilities section. 您应该可以选择从功能部分启用推送。 You also need to build separate key/cert pair in the apple member center and have those included on your machine. 您还需要在apple会员中心构建单独的密钥/证书对,并将它们包含在计算机中。 here is a screenshot from a new project i made. 这是我制作的新项目的屏幕截图。

功能部分

It sounds like maybe one of the steps in the process might not be done. 听起来好像过程中的步骤之一可能没有完成。

Start over again and try to figure out what your missing. 重新开始,尝试找出您的缺失。 here is a ray wenderlich tutorial that walks you through the steps here 这是ray wenderlich教程,可指导您完成此处的步骤

And the link to Apple developer site that talks about it in depth here 以及指向苹果开发者网站的链接, 此处对此进行了深入讨论

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM