Reputation: 251
I want to send the PUSH notification to the Multiple USERS through PHP script. When I run my script it shows the result successful, but on my device I am not getting any Notification.
But when I use the FCM console, I got the Notifications on my Device.
PHP script:
<?php
function send_notification ($tokens, $message)
{
$url = 'https://fcm.googleapis.com/fcm/send';
$fields = array(
'registration_ids' => $tokens,
'data' => $message,
'click_action' => ACTIVITY_CIRCULAR
);
$headers = array(
'Authorization:key = my key',
'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;
}
$conn = mysqli_connect("connection set-up");
$sql = "Select FirebaseID From Coordinates";
$result = mysqli_query($conn,$sql);
$tokens = array();
//var_dump(result);
if(mysqli_num_rows($result) > 0 ){
while ($row = mysqli_fetch_assoc($result)) {
$tokens[] = $row["FirebaseID"];
}
}
var_dump($tokens);
mysqli_close($conn);
$message = array("message" => "Hello World");
$message_status = send_notification($tokens, $message);
echo $message_status;
?>
My result:
{"multicast_id":6386552330832519***,"success":2,"failure":0,"canonical_ids":0,"results":[{"message_id":"0:1477******764957%e8a8d907f9fd7ecd"},{"message_id":"0:1477293027764959%e8a8d907f9f***cd"}]}
EDIT : Is there any way to check where the message is failing because it shows the Successful in Result...but it doesn't reach to device
Upvotes: 0
Views: 6542
Reputation: 9
If you user is more than 1k then you have to chunk you array because FCM registaion_ids not allowed more than 1k id at a time :
$value = array_chunk($userList,900);
foreach ($value as $val)
{
$db->sendPushCustomNew("test" . " Posted a Gallery", $createdDate, $val, "1", "Gotcha!", 0, 0, 0);
}
Then You have to configure you're notification send function like bellow :
function sendPushCustomNew($message, $currentDate, $tokens, $notificationType, $title, $latitude, $longitude, $memberID)
{
//error_log("IN_PUSH");
$curl = curl_init();
$fields = array(
'data' => array(
'notificationType' => $notificationType,
'title' => $title,
"message" => "" . $message,
"time" => "" . $currentDate,
'latitude' => $latitude,
'longitude' => $longitude
),
'registration_ids' => $tokens
);
curl_setopt_array($curl, array(
CURLOPT_URL => "https://fcm.googleapis.com/fcm/send",
CURLOPT_RETURNTRANSFER => true,
CURLOPT_ENCODING => "",
CURLOPT_MAXREDIRS => 10,
CURLOPT_TIMEOUT => 30,
CURLOPT_HTTP_VERSION => CURL_HTTP_VERSION_1_1,
CURLOPT_CUSTOMREQUEST => "POST",
CURLOPT_POSTFIELDS => json_encode($fields),
CURLOPT_HTTPHEADER => array(
"authorization: ******",
"cache-control: no-cache",
"content-type: application/json",
"postman-token: ***"
)
));
$response = curl_exec($curl);
$err = curl_error($curl);
curl_close($curl);
}
Upvotes: 1
Reputation: 12
inside your project you are adding registration id that you get when you create a google api console account from there you ll get server key and android key https://console.developers.google.com/apis/library?project=tabzen-1341,see the screenshot and read the google guidl
Upvotes: 0
Reputation: 362
use this php code its works in your case
<?php
// API access key from Google API's Console
define( 'API_ACCESS_KEY', 'YOUR-API-ACCESS-KEY-GOES-HERE' );
$registrationIds = array( $_GET['id'] );
// prep the bundle
$msg = array
(
'message' => 'here is a message. message',
'title' => 'This is a title. title',
'subtitle' => 'This is a subtitle. subtitle',
'tickerText' => 'Ticker text here...Ticker text here...Ticker text here',
'vibrate' => 1,
'sound' => 1,
'largeIcon' => 'large_icon',
'smallIcon' => 'small_icon'
);
$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://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;
Upvotes: 0
Reputation: 12
please check that you have saved the device id properly or not ,and also check that you have used the same server key as per project android key ,check your google account
Upvotes: 0