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