如何将附加数据发送到 firebase 推送通知

How to send additional data to firebase push notification

我能够使用 php 和 curl.Right 向 Ios/Android 发送推送通知,现在我想更新代码以发送其他参数,例如 user_id, name.So 我可以在我的前端获取这些信息 side.But 我不知道如何传递这些 data.Can 任何人都请帮助我。

function sendPushNotification($token,$push_notification_title=null,$push_notification_body=null){

$url = env('FIREBASE_URL');
$serverKey = env('FIREBASE_SERVER_KEY');
$title = $push_notification_title??"New Physician Review";
$body = $push_notification_body??"Your case has been reviewed";

$notification = array('title' => $title, 'text' => $body, 'body' => $body, 'sound' => 'default', 'badge' => '1');
$arrayToSend = array('to' => $token, 'notification' => $notification, 'priority' => 'high', 'data' => $notification, 'content_available' => true);

$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_POSTFIELDS, $json);
curl_setopt($ch, CURLOPT_POST, true);
curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, false);
curl_setopt($ch, CURLOPT_HEADER, true);
curl_setopt($ch, CURLOPT_HTTPHEADER, $headers);

$response = curl_exec($ch);

curl_close($ch);

return $response;

}

如果您想在邮件中包含其他信息,可以添加一个 data 数组。此 data 数组类似于您已有的 notification 数组,但它 由系统处理,只是传递给您的应用程序代码。

所以像这样:

$notification = array('title' => $title, 'text' => $body, 'body' => $body, 'sound' => 'default', 'badge' => '1');
$data = array('user_id' => 'youruserid', 'name' => 'yohan'); //  new array
$arrayToSend = array('to' => $token, 'notification' => $notification, 
                     'priority' => 'high', 'data' => $data, 'content_available' => true);
                                                    //  use it here