I tried to send FCM using PHP code/web browser.
But the problem is when I send it using PHP web browser:
- FCM notification only appear on virtual devices.
- FCM notification does not appear on real phone devices.
And I can only send FCM notifications to real phone devices using Firebase Console.
Can somebody help? The code is below.
<?php
require "init.php";
global $con;
if(isset($_POST['Submit'])){
$message = $_POST['message'];
$title = $_POST['title'];
$path_to_fcm = 'https://fcm.googleapis.com/fcm/send';
$server_key = "AAAA2gV_U_I:APA91bHA28EUGmA3BrDXFInGy-snx8wW6eZ_RUE7EtOyM99pbfrVZU_ME-FU0O9_dUxYpM30OYF8KWYlixod_PfwbgLNoovzdkdJ4F-30vY8X_tBz0CMrajCIAgbNVRfw203YdRGli";
$sql = "SELECT fcm_token FROM fcm_table";
$result = mysqli_query($con, $sql);
$row = mysqli_fetch_row($result);
$key = $row[0];
$headers = array('Authorization:key=' .$server_key, 'Content-Type:application/json');
$fields = array('to' => $key, 'notification' => array('title' => $title, 'body'=> $message));
$payload = json_encode($fields);
$curl_session = curl_init();
curl_setopt($curl_session, CURLOPT_URL, $path_to_fcm);
curl_setopt($curl_session, CURLOPT_POST, true);
curl_setopt($curl_session, CURLOPT_HTTPHEADER, $headers);
curl_setopt($curl_session, CURLOPT_RETURNTRANSFER, true);
curl_setopt($curl_session, CURLOPT_SSL_VERIFYPEER, false);
curl_setopt($curl_session, CURLOPT_IPRESOLVE, CURL_IPRESOLVE_V4);
curl_setopt($curl_session, CURLOPT_POSTFIELDS, $payload);
$result = curl_exec($curl_session);
curl_close($curl_session);
mysqli_close($con);
}
?>
<!DOCTYPE html>
<html>
<head>
<title>FCM Notification</title>
</head>
<body>
<form action='fcm_notification.php' method="POST">
<table>
<tr>
<td>Title : </td>
<td><input type="text" name="title" required="required" /></td>
</tr>
<tr>
<td>Message : </td>
<td><input type="text" name="message" required="required" /></td>
</tr>
<tr>
<td><input type="submit" name="Submit" value="Send notification"></td>
</tr>
</table>
</form>
</body>
</html>
Thanks.
you can create function which send push notification for devices .
for more details refer this
to – Type String – (Optional) [Recipient of a message] The value must be a single registration token, notification key, or topic. Do not set this field when sending to multiple topics
registration_ids – Type String array – (Optional) [Recipients of a message] Multiple registration tokens, min 1 max 1000.
priority– Type String – (Optional) [ default normal] Allowed values normal and high.
delay_while_idle – Type boolean – (Optional) [default value false] true indicates that the message should not be sent until the device becomes active.
time_to_live – Type JSON number – (Optional) [default value 4 week maximum 4 week] This parameter specifies how long (in seconds) the message should be kept in FCM storage if the device is offline
data – Type JSON Object Specifies the custom key-value pairs of the message’s payload. eg. {“post_id”:”1234″,”post_title”:”A Blog Post Title”}
In Android you can receive it in onMessageReceived() as Map data…
When in the background – Apps receive the notification payload in the notification tray, and only handle the data payload when the user taps on the notification.
When in the foreground – App receives a message object with both payloads available.
By the following way you can send push notification to mobile using google FCM. For me its works as expected. Add the key
'priority' => 'high'