简体   繁体   English

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

[英]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.

相关问题 如何使用OneSignal API和PHP在推送通知中发送“其他数据” - How to send “additional data” in push notification with OneSignal API and PHP 如何使用触发器发送Firebase推送通知 - How to send firebase push notification using trigger 如何将推送通知从 Firebase 发送到 iOS 设备上的 web 应用程序? - How to send push notification from Firebase to web app on iOS device? 如何在Laravel中使用Kreait / Firebase云消息向多个设备发送发送推送通知 - How to send send push notification to multiple device using kreait/firebase cloud message in laravel Firebase 如何发送主题通知 - Firebase how to send Topic Notification 如何为 Firebase 中的每个令牌发送带有不同数据的推送? - How to send Push with different data for each token in Firebase? 我们如何使用Firebase云消息传递(fcm)在Android中发送推送通知来发送图像和图标 - How we can send image and icon by using Firebase cloud messaging(fcm) for push notification in android Firebase Cloud Messaging PHP集成发送推送通知 - Firebase Cloud Messaging PHP Integration send push notification 无法使用 fcm 令牌 firebase 向 ios 发送推送通知 - unable to send push notification to ios using fcm token firebase 将Firebase推送通知发送给不工作的多个用户-php - Send firebase push notification to multiple users not working -php
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM