Google FCM Server: 200 but no notification sent to phone

a 夏天 提交于 2020-02-21 18:25:13

问题


I am working via Postman to send a FCM push notification to my phone that already has the app installed. I have push notifications enabled, the app is on the background, and I have a valid (confirmed) Firebase push token.

I'm POSTing to https://fcm.googleapis.com/fcm/send with the headers (Authorization: key=APP_SERVER_KEY, Content-Type: application/json), and my body looks like:

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

I'm getting the following 200 OK response with body:

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

everything looks fine, but I'm not getting a notification on my phone? How can I troubleshoot this, does anyone know what I'm missing?


回答1:


Found the solution!

But before I offer the solution, here is a bit more background on the situation for others who might find themselves in a similar problem. I was able to POST and send a notification to an Android device, and I was able to send a push via the Firebase console to both Android and iOS device, so the only thing didn't work was sending the push to specifically an iOS device via a POST HTTP request (quite baffling).

I found this discussion here: https://github.com/firebase/quickstart-ios/issues/21. Basically for iOS, I was missing two othere parameters in the request body. I needed to set content_available to true, and priority to high.

so it looked like this:

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

I believe the github discussion said something about how those added request body parameters allows FCM to push via APNS for iOS.




回答2:


Thank you for your answer and explaining your context. In my context, it wasnt working anyways.

So i added content_available and also replaced "text" with "body"

If anyone is still fighting with that, here it's how i handle it.

Cheers man !

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:


Alternative working solution to send push notification to iOS device using php script. Just change value for keys 'to' from fields array and Authorization from header array as per comment

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


?>


来源:https://stackoverflow.com/questions/39563115/google-fcm-server-200-but-no-notification-sent-to-phone

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!