[英]How to send additional data to firebase push notification
I was able to send push notifications to both Ios/Android using php and curl.Right now i want to update the code to send additional parameters such as user_id,name.So i can get these information on my frontend side.But i don't know how to pass these data.Can anyone please help me.我能够使用 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;
} }
If you want to include additional information in your message, you can add a data
array.如果要在消息中包含其他信息,可以添加
data
数组。 This data
array is similar to the notification
array you already have, but it is not handled by the system and simply passed to your application code.这个
data
数组类似于你已有的notification
数组,但它不由系统处理,只是传递给你的应用程序代码。
So something like this:所以像这样:
$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
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.