php - 如何向 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;
}
解决方案
如果您想在消息中包含其他信息,您可以添加一个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
推荐阅读
- elasticsearch - 使用 Spring-data-elasticsearch、ElasticsearchRepository 从 Elasticsearch 获取聚合信息
- reactjs - React 函数对象作为 React 子对象无效
- python - 从多个表单/相同表单的数组传递数据烧瓶wtforms python
- python - 如何打印单词出现?
- outlook-addin - Office 365 默认启用 On-send?
- spring-boot - 使用 @AuthenticationPrincipal 注入自定义 OidcUser 包装器
- html - 我们什么时候需要 div 和 span 可以点击,为什么不使用按钮呢?
- c# - 你好。谁能告诉我为什么我的代码 Section='"+lblSection.Text+"' 参数不起作用
- python - 如何解决“控制消息失败,win 错误:指定了不存在的设备”错误 PyUSB
- linux - 有没有一种简单的方法可以用 sed、grep 或其他任何东西来 grep 特定的字母?