我能夠使用 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;
}
uj5u.com熱心網友回復:
如果要在訊息中包含其他資訊,可以添加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
轉載請註明出處,本文鏈接:https://www.uj5u.com/ruanti/359245.html
