没有收到推送通知(以及故障排除的想法)

时间:2016-04-15 21:21:20

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

我觉得应该是iOS应用中的工作推送通知。这是它的核心。什么 让我疯狂 是一切似乎都有效,除了我的断点永远不会在消息接收函数中被击中;使用Google Cloud Notifications注册设备是成功的,初始化代码似乎有效,我甚至可以获得成功"消息来自GCM服务。

但是,我似乎从来没有真正在我的应用中收到消息。请注意,我在连接到Mac的iPad上运行它,并且通知设置中的所有内容都显示为kosher / enabled。另外,我使用dev证书来匹配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的消息。有效载荷是从样本中任意/复制的。

{ 
    "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"
    }
  ]
}

有什么想法吗?

1 个答案:

答案 0 :(得分:1)

请通过替换 GCM注册令牌服务器API密钥在终端中尝试以下命令..此命令可以帮助我。

  

请确保有效负载字典必须通知   的数据

curl --header&#34;授权:key =“https://android.googleapis.com/gcm/send -d&#39; {&#34; to”:“”,“”priority&#34;:&#34; high“ ,“通知&#34;:{&#34;标题”:“TEST TITLES”,“body&#34;:&#34;哇!您测试的第一条消息。&#34;,&#34;徽章&#34;:&#34; 1&#34;,&#34;声音&#34;:&#34;默认&#34;}}&#39 ;

还可以尝试通过应用程序在后台发送 CMD + H

进行检查