没有收到推送通知(并且没有故障排除想法)

Not Receiving Push Notifications (And Out of Troubleshooting Ideas)

我在 iOS 应用程序中有一个看起来应该是有效的推送通知。这是它的核心。 让我抓狂 的是一切似乎都正常,除了我的断点从未在消息接收函数中命中;使用 Google Cloud Notifications 的设备注册成功,初始化代码似乎有效,我什至从 GCM 服务收到 "Success" 消息。

不过,我似乎从来没有真正在我的应用程序中收到消息。请注意,我 运行 它在连接到我的 Mac 的 iPad 中,并且通知设置中的所有内容似乎 kosher/enabled。此外,我正在使用开发证书来匹配 kGGLInstanceIDAPNSServerTypeSandboxOption:true 选项。

我可能遗漏了什么?

import UIKit
import Google

@UIApplicationMain
class AppDelegate: UIResponder, UIApplicationDelegate, GGLInstanceIDDelegate {

    var window: UIWindow?
    var gcmSenderID: String?
    var registrationToken: String?
    var registrationOptions = [String: AnyObject]()

    let messageKey = "onMessageReceived"
    let registrationKey = "onRegistrationCompleted"

    func application( application: UIApplication,
        didReceiveRemoteNotification userInfo: [NSObject : AnyObject]) {
        //********************************
        //*** Never Hits Breakpoint ******
        //********************************

            print("Notification received: \(userInfo)")
            // Handle the received message

            NSNotificationCenter.defaultCenter().postNotificationName(messageKey, object: nil,
                userInfo: userInfo)
    }

    func application( application: UIApplication,
        didReceiveRemoteNotification userInfo: [NSObject : AnyObject],
        fetchCompletionHandler handler: (UIBackgroundFetchResult) -> Void) {
        //********************************
        //*** Never Hits Breakpoint ******
        //********************************

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

    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

        // ...
        // Register for remote notifications
        let settings: UIUserNotificationSettings =
            UIUserNotificationSettings(forTypes: [.Alert, .Badge, .Sound], categories: nil)
        application.registerUserNotificationSettings(settings)
        application.registerForRemoteNotifications()

        // Override point for customization after application launch.
        return true
    }


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

        // 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)
        registrationOptions = [kGGLInstanceIDRegisterAPNSOption:deviceToken,
            kGGLInstanceIDAPNSServerTypeSandboxOption:true]
        GGLInstanceID.sharedInstance().tokenWithAuthorizedEntity(gcmSenderID,
            scope: kGGLInstanceIDScopeGCM, options: registrationOptions, handler: registrationHandler)

    }

    func application( application: UIApplication, didFailToRegisterForRemoteNotificationsWithError
        error: NSError ) {
            print("Registration for remote notification failed with error: \(error.localizedDescription)")
            // [END receive_apns_token_error]
            let userInfo = ["error": error.localizedDescription]
            NSNotificationCenter.defaultCenter().postNotificationName(
                registrationKey, object: nil, userInfo: userInfo)
    }

    func registrationHandler(registrationToken: String!, error: NSError!) {
        if (registrationToken != nil) {
            self.registrationToken = registrationToken
            print("Registration Token: \(registrationToken)")
            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 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)
    }
}

消息已发送至 https://gcm-http.googleapis.com/gcm/send。有效载荷是来自样本的arbitrary/copied。

{ 
    "content-available":1,
    "data": {
    "score": "5x1",
    "time": "15:10"
  },
  "to" : "<<the registration token resulting from the code above>>"
}

...导致回复:

{
  "multicast_id": 12345...,
  "success": 1,
  "failure": 0,
  "canonical_ids": 0,
  "results": [
    {
      "message_id": "some ID"
    }
  ]
}

有什么想法吗?

请尝试在您的终端中执行以下命令,替换为您的 GCM 注册令牌服务器 API 密钥..此命令帮我。

Please make sure payload dictionary must be notification not data

curl --header "Authorization: key=” https://android.googleapis.com/gcm/send -d '{"to”:””,”priority":"high”, “notification":{"title”:”TEST TITLES”,”body":"Wow ! your first message tested.","badge":" 1","sound":"default"}}'

也可以尝试通过应用程序在后台发送来检查 CMD + H