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.

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;

}

Upvotes: 3

Views: 5230

Answers (1)

Frank van Puffelen
Frank van Puffelen

Reputation: 599041

If you want to include additional information in your message, you can add a data array. 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.

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

Upvotes: 6

Related Questions