【问题标题】:FCM with PHP not showing additonalData in console带有 PHP 的 FCM 未在控制台中显示附加数据
【发布时间】:2018-09-10 15:21:39
【问题描述】:

我正在使用 php 发送推送通知,我意识到推送通知来自手机,但未能发送我添加到脚本中的附加数据,例如页面等。

<?php

$url = "https://fcm.googleapis.com/fcm/send";
$token = 'device_id here';
$serverKey = 'AIzaSxxxbAGLyxxxx';
$title = "New Message";
$body = 'Hello there';
$notification = array('title' =>$title , 'message' => $body,'priority'=>'high','badge'=>'1','notId'=>''.time(), 'id' => '33','page' => 'news');
$arrayToSend = array('to' => $token, 'notification' => $notification);
$json = json_encode($arrayToSend);
$headers = array();
$headers[] = 'Content-Type: application/json';
$headers[] = 'Authorization: key='. $serverKey;
$ch = curl_init();
curl_setopt($ch, CURLOPT_URL, $url);

curl_setopt($ch, CURLOPT_CUSTOMREQUEST,"POST");
curl_setopt($ch, CURLOPT_POSTFIELDS, $json);
curl_setopt($ch, CURLOPT_HTTPHEADER,$headers);
//Send the request
$response = curl_exec($ch);
//Close request
if ($response === FALSE) {
die('FCM Send Error: ' . curl_error($ch));
}
curl_close($ch);

?>

【问题讨论】:

    标签: php android firebase firebase-cloud-messaging


    【解决方案1】:

    您正在尝试将自定义数据字段添加到 Notification 消息。 Notification 消息只允许某些字段。如果您想发送自定义数据,则需要将您的消息设为 Data 消息或带有数据负载的 Notification 消息。

    从 FCM 文档中,Notification 消息与 Android 数据负载的组合可能如下所示:

    {
      "to":"bk3RNwTe3H0:CI2k_HHwgIpoDKCIZvvDMExUdFQ3P1...",
      "notification":{
          "title":"New Message",
          "body":"Hello there"
        },
        "data" : {
          "notId" : 201801,
          "id" : 33,
          "page" : "news",
        }
    }
    

    对消息结构进行以下更改:

    $notification = array('title' =>$title , 'message' => $body);
    $data = array('notId'=>''.time(), 'id' => '33','page' => 'news');
    $arrayToSend = array('to' => $token, 'notification' => $notification, 'data' => $data);
    

    您需要更改您的 android 代码以适应 data 字段并相应地解析数据。

    请仔细阅读 FCM 文档以了解此更改会对您的项目产生哪些影响。最重要的是,当您的应用在后台时如何处理data 消息!

    【讨论】:

      猜你喜欢
      • 2018-09-23
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2017-02-02
      相关资源
      最近更新 更多