send notification for multiple device using fcm
<?php
include_once('config.php');
define( 'AIzaSyCYpVYwK42m4k_WAE3Tc-EZwRLo9_A89vA', 'AAAA0r_x_cs:APA91bEMJ8IpI-n1rxE7VwRUbnAFzm9WULKFHfeYehnkhu2RtXc0Tl0YmupuL6mkp8p3mDytINr5fGzJCDKdePKAzmkn1A7bjHletWonepazDSDCU8K4UJFkq-NXbEUlzKB5lvH1AzxX' );
function send_notification ($tokens)
{
$url = 'https://fcm.googleapis.com/fcm/send';
$msg = array(
'body' => 'Couppon notification',
'title' => 'Title Of Notification',
'icon' => 'myicon',/*Default Icon*/
'sound' => 'mySound'/*Default sound*/
);
$fields = array(
'registration_ids' => $tokens,
'notification' => $msg,
'priority'=>'high'
/* 'click_action' => ACTIVITY_CIRCULAR */
);
$headers = array
(
'Authorization: key=AAAA0r_x_cs:APA91bEMJ8IpI-n1rxE7VwRUbnAFzm9WULKFHfeYehnkhu2RtXc0Tl0YmupuL6mkp8p3mDytINr5fGzJCDKdePKAzmkn1A7bjHletWonepazDSDCU8K4UJFkq-NXbEUlzKB5lvH1AzxX',
'Content-Type: application/json'
);
$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('Curl failed: ' . curl_error($ch));
}
curl_close($ch);
return $result;
}
$sql = "SELECT device_id
FROM app_enrollment_user
WHERE app_enrollment_user_id
IN ( 26,14 )";
$result = mysql_query($sql);
$tokens = array();
if(mysql_num_rows($result) > 0 ){
while ($row = mysql_fetch_assoc($result)) {
$tokens[] = $row["device_id"];
}
}
var_dump($tokens);
mysql_close($conn);
$message_status = send_notification($tokens);
echo $message_status;
?>
include_once('config.php');
define( 'AIzaSyCYpVYwK42m4k_WAE3Tc-EZwRLo9_A89vA', 'AAAA0r_x_cs:APA91bEMJ8IpI-n1rxE7VwRUbnAFzm9WULKFHfeYehnkhu2RtXc0Tl0YmupuL6mkp8p3mDytINr5fGzJCDKdePKAzmkn1A7bjHletWonepazDSDCU8K4UJFkq-NXbEUlzKB5lvH1AzxX' );
function send_notification ($tokens)
{
$url = 'https://fcm.googleapis.com/fcm/send';
$msg = array(
'body' => 'Couppon notification',
'title' => 'Title Of Notification',
'icon' => 'myicon',/*Default Icon*/
'sound' => 'mySound'/*Default sound*/
);
$fields = array(
'registration_ids' => $tokens,
'notification' => $msg,
'priority'=>'high'
/* 'click_action' => ACTIVITY_CIRCULAR */
);
$headers = array
(
'Authorization: key=AAAA0r_x_cs:APA91bEMJ8IpI-n1rxE7VwRUbnAFzm9WULKFHfeYehnkhu2RtXc0Tl0YmupuL6mkp8p3mDytINr5fGzJCDKdePKAzmkn1A7bjHletWonepazDSDCU8K4UJFkq-NXbEUlzKB5lvH1AzxX',
'Content-Type: application/json'
);
$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('Curl failed: ' . curl_error($ch));
}
curl_close($ch);
return $result;
}
$sql = "SELECT device_id
FROM app_enrollment_user
WHERE app_enrollment_user_id
IN ( 26,14 )";
$result = mysql_query($sql);
$tokens = array();
if(mysql_num_rows($result) > 0 ){
while ($row = mysql_fetch_assoc($result)) {
$tokens[] = $row["device_id"];
}
}
var_dump($tokens);
mysql_close($conn);
$message_status = send_notification($tokens);
echo $message_status;
?>
Comments
Post a Comment