The notification service is great when sending from the firebase console, but does not work when sending from the API. Even when the result shows success: {"Multicast_id": 5946406103096345260, "success": 1, "failure": 0, "canonical_ids": 0, "Results": [{"message_id": "0: 1480093752122166% 13791f60f9fd7ecd"}] }
Anyway, heres the code:
<?php
$data = array('title' => 'Notification Title' ,'message' => 'Hello World!');
$ids = array('TOKEN');
sendPushNotification($data, $ids);
function sendPushNotification($data, $ids)
{
$apiKey = 'API_KEY';
$post = array(
'registration_ids' => $ids,
'data' => $data,
);
$headers = array(
'Authorization: key=' . $apiKey,
'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_POSTFIELDS, json_encode($post));
$result = curl_exec($ch);
if (curl_errno($ch))
{
echo 'GCM error: ' . curl_error($ch);
}
curl_close($ch);
echo $result;
}
?>
source
share