【问题标题】:Google FCM Server: 200 but no notification sent to phoneGoogle FCM 服务器:200 但未向手机发送通知
【发布时间】:2016-09-18 22:06:30
【问题描述】:

我正在通过 Postman 向已安装该应用的手机发送 FCM 推送通知。我启用了推送通知,应用程序在后台,并且我有一个有效(已确认)的 Firebase 推送令牌。

我正在使用标题(授权:key=APP_SERVER_KEY,内容类型:application/json)向https://fcm.googleapis.com/fcm/send 发布,我的正文如下所示:

{ "notification": {
"title": "Portugal vs. Denmark",
"text": "5 to 1"
},
 "to": MY_PUSH_TOKEN
}

我收到以下带有正文的 200 OK 响应:

{
 "multicast_id": MULTICAST_ID,
 "success": 1,
 "failure": 0,
 "canonical_ids": 0,
 "results": [{
  "message_id": "0:MESSAGE_ID"
 }]
}

一切正常,但我的手机没有收到通知?我该如何解决这个问题,有人知道我错过了什么吗?

【问题讨论】:

  • 什么设备? Android 还是 iOS?
  • 我已经尝试了您的消息,并且能够在客户端(在本例中为 Android)接收消息。我猜这是客户端问题,因此添加您的接收代码会有所帮助。同样在请求中请使用“body”而不是“text”,因为“text”不是通知有效负载的记录字段。
  • AL,我的目标是 iOS。 @ArthurThompson,将其更改为文本。我还在 Android 设备上尝试过它,它可以工作,但 iOS 仍然没有运气。你指的是什么接收代码?我正在尝试通过 FCM 服务器通过邮递员 HTTP 请求发送。
  • 接收代码将是您的 AppDelegate,您可以在其中注册远程通知等。

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


【解决方案1】:

找到了解决办法!

但在我提供解决方案之前,这里有一些关于这种情况的背景信息,供可能遇到类似问题的其他人参考。我能够 POST 并向 Android 设备发送通知,并且能够通过 Firebase 控制台向 Android 和 iOS 设备发送推送,所以唯一没有用的是将推送发送到专门的 iOS 设备通过 POST HTTP 请求(非常令人费解)。

我在这里找到了这个讨论:https://github.com/firebase/quickstart-ios/issues/21。基本上对于 iOS,我在请求正文中缺少两个其他参数。我需要将 content_available 设置为 true,并将优先级设置为 high。

所以它看起来像这样:

{
    "notification": {
        "title": "Portugal vs. Denmark",
        "body": "5 to 1"
    },
    "content_available": true,
    "priority": "high",
    "to": MY_PUSH_TOKEN
}

我相信 github 的讨论说明了这些添加的请求正文参数如何允许 FCM 通过 iOS 的 APNS 推送。

【讨论】:

    【解决方案2】:

    感谢您的回答并解释您的背景。 在我的上下文中,它无论如何都不起作用。

    所以我添加了 content_available 并将“text”替换为“body”

    如果有人还在与之抗争,这就是我的处理方式。

    干杯!

    import request from 'request'
    
    const KEYS = require('../hardcodekeys')
    
    export function send (title, message, ids) {
    
      //POST: https://fcm.googleapis.com/fcm/send
      //HEADER: Content-Type: application/json
      //HEADER: Authorization: key=AIzaSy*******************
      let push = {
        "notification" : {
          "body" : "great match!",
          "title" : "Portugal vs. Denmark",
          "icon" : "myicon"
        },
        content_available: true,
        to: ids
        priority: 'high'
      }
    
      let options = {
        method: 'POST',
        url: 'https://fcm.googleapis.com/fcm/send',
        headers: {
          'Content-Type': 'application/json',
          'Authorization' : `key=${KEYS.FCM_KEY}`
        },
        body: JSON.stringify(push)
      }
    
      function callback(error, response, body) {
        console.log(response.statusCode)
        console.log(body)
        console.log(error)
        if (!error && response.statusCode == 200) {
          var info = JSON.parse(body)
          console.log(info)
        }
      }
    
      request(options, callback)
    }
    

    【讨论】:

      【解决方案3】:

      使用 php 脚本向 iOS 设备发送推送通知的替代工作解决方案。 只需根据评论更改字段数组中键“to”的值和标题数组中的授权值

      //To Execute this this from Mac Terminal type php thisFilename.php
      
      
      $url = 'https://fcm.googleapis.com/fcm/send';
      $fields = array (
              'to' => 'fq5fI_wcXNY:APA91bGpR-qCYW01wNJ8pnp1ftfgR3DHqPk3ViXDqTYrq-p7MUhry9cPcpXEl7z4GFHGUPcTduww656Rks8cOpSZR-FjrseDX4S-eGdzGaBEqI46KWF8ZJmJpegbf3tzVZwILmnf64aU',//Replace with FIRInstanceID.instanceID().token() this you can get in Appdelegate, note this is NOT token received in didRegisterForRemoteNotificationsWithDeviceToken
              'notification' => array (
                      "body" => "message",
                      "title" => "Title",
                      "icon" => "myicon"
              )
      );
      $fields = json_encode ( $fields );
      $headers = array (
              'Authorization: key=' . "AIdfdfzaSyC_0F8sqVqOgdg3Es4trWFcrNcrLpBjG6w06w",//This is Server Key, you can get it from Firebase console ->  App Setting ->  Cloud Messaging Tab - Legacy server key
              'Content-Type: application/json'
      );
      
      $ch = curl_init ();
      curl_setopt ( $ch, CURLOPT_URL, $url );
      curl_setopt ( $ch, CURLOPT_POST, true );
      curl_setopt ( $ch, CURLOPT_HTTPHEADER, $headers );
      curl_setopt ( $ch, CURLOPT_RETURNTRANSFER, true );
      curl_setopt ( $ch, CURLOPT_POSTFIELDS, $fields );
      
      $result = curl_exec ( $ch );
      curl_close ( $ch );
      
      
      ?>
      

      【讨论】:

        猜你喜欢
        • 2019-08-07
        • 1970-01-01
        • 1970-01-01
        • 2016-09-27
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 2020-09-14
        • 1970-01-01
        相关资源
        最近更新 更多