I\'m new to push notifications using FCM from php to Android devices. From android side I have generated FCM reg_id & send it over php script & store into mysql data
To send firebase notifications to multiple users at once
add multiple firebase tokens to an array.
$token_ids = array('token1', 'token2');
Pass the tokens to the below shown function
Using below shown function you can also send images with your notification
if you don't want to send any image then just pass empty string
sendFirebaseNotification($token_ids ,"notification title", "message", "image_url");
I use this function
function sendFirebaseNotification($fb_key_array, $title, $message, $imageURL){
$authorization_key = "your_auth_key";
$finalPostArray = array('registration_ids' => $fb_key_array,
'notification' => array('body' => $message,
'title' => $title,
"image"=> $imageURL),
"data"=> array("click_action"=> "FLUTTER_NOTIFICATION_CLICK",
"sound"=> "default",
"status"=> "done"));
$ch = curl_init();
curl_setopt($ch, CURLOPT_URL,"https://fcm.googleapis.com/fcm/send");
curl_setopt($ch, CURLOPT_POST, 1);
curl_setopt($ch, CURLOPT_POSTFIELDS,json_encode($finalPostArray)); //Post Fields
curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
curl_setopt($ch, CURLOPT_HTTPHEADER, array('Content-Type: application/json', 'Authorization: key='.$authorization_key));
$server_output = curl_exec ($ch);
curl_close ($ch);
//echo $server_output;
}
Below is the simple script to send notification. Try this.
<?php
$tokens = array('token_1','token_2','token_3');
$title = "Title Here";
$msg = "Subtitle or description Here";
//Custom Parameters if any
$customParam = array(
'redirection_id' => '2',
'redirection_type' => 'post_page' //'post_page','category_page','blog_page'
);
push_notification_android($tokens,$title,$msg,$customParam);
function push_notification_android($tokens,$title,$msg,$customParam) {
$url = 'https://fcm.googleapis.com/fcm/send';
$api_key = 'fcm_server_api_key';
$messageArray = array();
$messageArray["notification"] = array (
'title' => $title,
'message' => $msg,
'customParam' => $customParam,
);
$fields = array(
'registration_ids' => $tokens,
'data' => $messageArray,
);
$headers = array(
'Authorization: key=' . $api_key, //GOOGLE_API_KEY
'Content-Type: application/json'
);
// Open connection
$ch = curl_init();
// Set the url, number of POST vars, POST data
curl_setopt($ch, CURLOPT_URL, $url);
curl_setopt($ch, CURLOPT_POST, true);
curl_setopt($ch, CURLOPT_HTTPHEADER, $headers);
curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
// Disabling SSL Certificate support temporarly
curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, false);
curl_setopt($ch, CURLOPT_POSTFIELDS, json_encode($fields));
// Execute post
$result = curl_exec($ch);
if ($result === FALSE) {
echo 'Android: Curl failed: ' . curl_error($ch);
}
// Close connection
curl_close($ch);
return $result;
}
?>
$notification_data = $this->common->get_all_record('table name',array()); //get all id from table
if($notification_data != NULL){
foreach ($notification_data as $notification_data_row) {
$registrationIds = $notification_data_row['token'];
#prep the bundle
$msg = array
(
'body' => 'body msg',
'title' => 'title',
'icon' => 'myicon',/*Default Icon*/
'sound' => 'mySound'/*Default sound*/
);
$fields = array
(
'to' => $registrationIds,
'notification' => $msg
);
$headers = array
(
'Authorization: key=' . "your key",
'Content-Type: application/json'
);
#Send Reponse To FireBase Server
$ch = curl_init();
curl_setopt( $ch,CURLOPT_URL, 'https://fcm.googleapis.com/fcm/send' );
curl_setopt( $ch,CURLOPT_POST, true );
curl_setopt( $ch,CURLOPT_HTTPHEADER, $headers );
curl_setopt( $ch,CURLOPT_RETURNTRANSFER, true );
curl_setopt( $ch,CURLOPT_SSL_VERIFYPEER, false );
curl_setopt( $ch,CURLOPT_POSTFIELDS, json_encode( $fields ) );
$result = curl_exec ( $ch );
// echo "<pre>";print_r($result);exit;
curl_close ( $ch );
}
}
Try to send device ID of multiple devices as an array. In your case,
$registration_ids must be an array of device IDs.
E.g
$registration_ids = array('Device ID 1', 'Device ID 2');
If you were to send notification via terminal the data part of the curl command would look like this:
{
"registration_ids": ["device_token_1", "device_token_2"],
"notification": {
"body": "Hello",
"title": "Hello",
"vibrate": 1,
"sound": 1
}
}
PHP code:
$body = array(
'registration_ids' => array("device_token_1", "device_token_2"),
'notification' => array('body' => 'Hello', 'title' => 'Hello', 'vibrate' => 1, 'sound' => 1)
);