I am integrating android push notification is working fine on the client side. Do I want to make server push notification using PHP? Please suggest if you know about the same.
Asked
Active
Viewed 1.0k times
3 Answers
3
This is the source code that I have been referring to for my development. You can try it on phpfiddle. Reference: Push notification (PHP). You can check payload notification for firebase here.
<?php
define( 'API_ACCESS_KEY', 'AIza......Xhdsnkf' ); // get API access
key from Google/Firebase API's Console
$registrationIds = array( 'cyMSGTKBzwU:APA91...xMKgjgN32WfoJY6mI' ); //Replace this with your device token
// Modify custom payload here
$msg = array
(
'mesgTitle' => 'SMART TESTING',
'alert' => 'This is sample notification'
);
$fields = array
(
'registration_ids' => $registrationIds,
'data' => $msg
);
$headers = array
(
'Authorization: key=' . API_ACCESS_KEY,
'Content-Type: application/json'
);
$ch = curl_init();
curl_setopt( $ch,CURLOPT_URL, 'https://android.googleapis.com/gcm/send' ); //For firebase, use 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 );
curl_close( $ch );
echo $result;
?>

Amad Yus
- 2,856
- 1
- 24
- 32
-
Thanks. Can you say how to set android notification badge on the same message fields?? – Vinil Lakkavatri Nov 30 '17 at 09:26
2
This piece of code to send notification to android devices from Firebase using PHP
$server_key=""; // get this from Firebase project settings->Cloud Messaging
$user_token=""; // Token generated from Android device after setting up firebase
$title="New Message";
$n_msg="The is a message";
$ndata = array('title'=>$title,'body'=>$n_msg);
$url = 'https://fcm.googleapis.com/fcm/send';
$fields = array();
$fields['data'] = $ndata;
$fields['to'] = $user_token;
$headers = array(
'Content-Type:application/json',
'Authorization:key='.$server_key
);
$ch = curl_init();
curl_setopt($ch, CURLOPT_URL, $url);
curl_setopt($ch, CURLOPT_POST, true);
curl_setopt($ch, CURLOPT_HTTPHEADER, $headers);
curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
curl_setopt($ch, CURLOPT_SSL_VERIFYHOST, 0);
curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, false);
curl_setopt($ch, CURLOPT_POSTFIELDS, json_encode($fields));
$result = curl_exec($ch);
if ($result === FALSE) {
die('FCM Send Error: ' . curl_error($ch));
}
curl_close($ch);

Naz141
- 433
- 1
- 8
- 31
-1
define( 'API_ACCESS_KEY', 'add_your_server_firebase_access_key');
<!-- set payload here -->
$msg =
[
'type' => "123",
'body' => "By Developer",
'title' => "Push Testing",
'sound' => "default",
'badge' => "0",
'id' => "100"
];
$headers = array
(
'Authorization: key='.API_ACCESS_KEY,
'Content-Type: application/json'
);
$firebaseKey=array('firebase_key_01','firebase_key_02','firebase_key_03'); <!-- your all user firebase token id pass here -->
try {
foreach ($firebaseKey as $findedKey)
{
$fields = array('registration_ids' => array($findedKey),
'data' => $msg,
);
if (!function_exists('curl_version')) {
throw new Exception("CURL is not installed in this server..!");
}
$ch = curl_init();
if (!$ch) {
throw new Exception("CURL intialization fails..!");
}
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 );
curl_close( $ch );
pre($result);
}
if (!$result) {
$error = curl_error($ch) || "CURL execution fails..!";
throw new Exception($error);
} if ($result) {
echo "Push notification send successfully..!";
}
}
catch (Exception $e)
{
echo 'Message: Push notification didn't send error :' .$e->getMessage();
}

iamasp
- 87
- 5
-
Please don't post only code as answer, but also provide an explanation what your code does and how it solves the problem of the question. Answers with an explanation are usually more helpful and of better quality, and are more likely to attract upvotes. – Pouria Hemi Nov 24 '20 at 12:17
-
This isn't useful. What is a registration id? They're not defined in your example nor do you give any advice on how to get them. – Richie Jan 27 '21 at 19:10