带有PHP的FCM未在控制台中显示additonalData

问题描述 投票:-1回答:1

我使用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个回答
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消息!

© www.soinside.com 2019 - 2024. All rights reserved.