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
Below is the simple script to send notification. Try this.
'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;
}
?>