首页 文章

Google FCM Server:200但未向手机发送通知

提问于
浏览
3

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

我正在使用 Headers (授权:key = APP_SERVER_KEY,Content-Type: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"
 }]
}

一切都很好,但我没有在手机上收到通知?我怎么能解决这个问题,有谁知道我错过了什么?

3 回答

  • 0

    找到了解决方案!

    但在我提供解决方案之前,对于可能发现自己处于类似问题的其他人来说,这里有更多背景知识 . 我能够发布并向Android设备发送通知,我能够通过Firebase控制台向Android和iOS设备发送推送,因此唯一不起作用的是将推送发送到专门的iOS设备通过POST HTTP请求(非常困惑) .

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

    所以它看起来像这样:

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

    我相信github讨论会说明这些添加的请求主体参数如何允许FCM通过APNS推送到iOS .

  • 1

    感谢您的回答并解释您的背景 . 在我的背景下,它无论如何都没有工作 .

    所以我添加了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)
    }
    
  • 6

    使用php脚本向iOS设备发送推送通知的替代工作解决方案 . 只需按照注释更改字段数组中键'到'的值和来自 Headers 数组的授权

    //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 );
    
    
    ?>
    

相关问题