Firebase Cloud Messaging停止工作

时间:2017-03-19 11:50:21

标签: ios firebase push-notification firebase-cloud-messaging firebase-notifications

我成功设置了Firebase云消息传递,并能够使用我设备的FCM注册令牌在通知控制台中向自己发送一些推送消息。

我正在使用云功能尝试使用this example发送一些推送通知。我无法使它工作,所以当我回去尝试使用控制台向自己发送一些推送通知时,这也停止了工作。在此期间,我不相信我对我的代码做了任何更改。

我跟着this guide进行调试,我能够使用curl命令成功发送并查看推送通知

> curl --http2 --cert ./myapp-push-cert.pem \
-H "apns-topic: com.example.yourapp.bundleID" \
-d '{"aps":{"alert":"Hello from APNs!","sound":"default"}}' \
https://api.development.push.apple.com/3/device/ab8293ad...c0d

我也尝试使用curl

直接拨打FCM
> curl --header "Content-Type: application/json" \
--header "Authorization: key=AU...the rest of your server key...s38txvmxME-W1N4" \
https://fcm.googleapis.com/fcm/send \
-d '{"notification": {"body": "Hello from curl via FCM!", "sound": "default"},
"priority": "high",
"to": "gJHcrfzW2Y:APA91...the rest of your FCM token...-JgS70Jm"}' 

并获得以下成功消息:

{
 "multicast_id":8128425267989767985,
 "success":1,"failure":0,
 "canonical_ids":0,"results":[
   {"message_id":"0:141292123136432%e3eee07da3edd08d"}
 ]
}

但我没有看到任何来自此卷曲的通知。 Firebase Status Dashboard显示没有问题。我已尝试使用相同结果的第二台设备。我试过保持应用程序的前景和背景。可能是什么问题?

编辑:以下是我用来设置和接收通知的代码:

  //called from applicationDidLaunch
  private func setupNotifications(_ application: UIApplication) {
    print(FIRInstanceID.instanceID().token())
    // For iOS 10 display notification (sent via APNS)
    UNUserNotificationCenter.current().delegate = self

    // For iOS 10 data message (sent via FCM)
    FIRMessaging.messaging().remoteMessageDelegate = self

    NotificationCenter.default.addObserver(self, selector: #selector(tokenRefreshNotification), name: NSNotification.Name.firInstanceIDTokenRefresh, object: nil)
    application.registerForRemoteNotifications()


  }

  //this is successfully called
  @objc private func tokenRefreshNotification(_ notification: Notification) {
    if let refreshedToken = FIRInstanceID.instanceID().token() {
      firebaseController.setTokenInDatabase()
      print("InstanceID token: \(refreshedToken)")
    }

    connectToFcm()
  }

  //just like in the blog post ive tried both with and without the connectToFcm() method
  private func connectToFcm() {
    // Won't connect since there is no token
    guard FIRInstanceID.instanceID().token() != nil else {
      return
    }

    // Disconnect previous FCM connection if it exists.
    FIRMessaging.messaging().disconnect()

    FIRMessaging.messaging().connect { (error) in
      if error != nil {
        print("Unable to connect with FCM. \(error)")
      } else {
        print("Connected to FCM.")
      }
    }
  }

  func application(_ application: UIApplication,
                   didFailToRegisterForRemoteNotificationsWithError error: Error) {
    print("Oh no! Failed to register for remote notifications with error \(error)")
  }

  //this is called successfully too
  func application(_ application: UIApplication,
                   didRegisterForRemoteNotificationsWithDeviceToken deviceToken: Data) {
    var readableToken: String = ""
    for i in 0..<deviceToken.count {
      readableToken += String(format: "%02.2hhx", deviceToken[i] as CVarArg)
    }
    print("Received an APNs device token: \(readableToken)")
  }

  //neither of the following methods are no longer called
  let gcmMessageIDKey = "gcm.message_id"
  func application(_ application: UIApplication, didReceiveRemoteNotification userInfo: [AnyHashable: Any]) {

    // Print message ID.
    if let messageID = userInfo[gcmMessageIDKey] {
      print("Message ID: \(messageID)")
    }

    // Print full message.
    print(userInfo)
  }

  func application(_ application: UIApplication, didReceiveRemoteNotification userInfo: [AnyHashable: Any],
                   fetchCompletionHandler completionHandler: @escaping (UIBackgroundFetchResult) -> Void) {

    // Print message ID.
    if let messageID = userInfo[gcmMessageIDKey] {
      print("Message ID: \(messageID)")
    }

    // Print full message.
    print(userInfo)

    completionHandler(UIBackgroundFetchResult.newData)
  }

  func applicationReceivedRemoteMessage(_ remoteMessage: FIRMessagingRemoteMessage) {
    print(remoteMessage.appData)
  }

  func userNotificationCenter(_ center: UNUserNotificationCenter, didReceive response: UNNotificationResponse, withCompletionHandler completionHandler: @escaping () -> Void) {
    print("didReceive", response.notification.request.content.userInfo)
  }

  func userNotificationCenter(_ center: UNUserNotificationCenter, willPresent notification: UNNotification, withCompletionHandler completionHandler: @escaping (UNNotificationPresentationOptions) -> Void) {
    print("willPresent", notification.request.content.userInfo)
    completionHandler([.alert])
  }

0 个答案:

没有答案