首页 > 解决方案 > 如何向 Firebase 推送通知发送其他数据

问题描述

我能够使用 php 和 curl 向 Ios/Android 发送推送通知。现在我想更新代码以发送其他参数,例如 user_id、name。所以我可以在前端获取这些信息。但我没有不知道如何传递这些数据。任何人都可以帮助我。

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;

}

标签: phplaravelfirebasecurlfirebase-cloud-messaging

解决方案


如果您想在消息中包含其他信息,您可以添加一个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

推荐阅读