Send Push notification using php(FCM)

FCM
FCM is a new version of GCM (Google Cloud Messaging) service. It is a cross-platform messaging service, used to send notifications that are displayed to the user. We can use any of three ways to send messages to the client app – single devices, gro…


This content originally appeared on DEV Community and was authored by koshti Rahul

FCM
FCM is a new version of GCM (Google Cloud Messaging) service. It is a cross-platform messaging service, used to send notifications that are displayed to the user. We can use any of three ways to send messages to the client app - single devices, groups of devices or devices subscribed to topics. It also sends acknowledgments back to the server. We can use FCM for iOS clients, Android clients, Unity clients, web clients and more. FCM is reliable and battery-efficient.

Just copy and paste the code and change the credentials to yours

<?php
    $url = "https://fcm.googleapis.com/fcm/send";
    $token = "your device token";
    $serverKey = 'your server token of FCM project';
    $title = "Notification title";
    $body = "Hello I am from Your php server";
    $notification = array('title' =>$title , 'body' => $body, 'sound' => 'default', 'badge' => '1');
    $arrayToSend = array('to' => $token, 'notification' => $notification,'priority'=>'high');
    $json = json_encode($arrayToSend);
    $headers = array();
    $headers[] = 'Content-Type: application/json';
    $headers[] = 'Authorization: key='. $serverKey;
    $ch = curl_init();
    curl_setopt($ch, CURLOPT_URL, $url);
    curl_setopt($ch, CURLOPT_CUSTOMREQUEST,"POST");
    curl_setopt($ch, CURLOPT_POSTFIELDS, $json);
    curl_setopt($ch, CURLOPT_HTTPHEADER,$headers);
    //Send the request
    $response = curl_exec($ch);
    //Close request
    if ($response === FALSE) {
    die('FCM Send Error: ' . curl_error($ch));
    }
    curl_close($ch);
?>

Read More :: https://cmsinstallation.blogspot.com/2019/05/send-push-notification-using-phpfcm.html


This content originally appeared on DEV Community and was authored by koshti Rahul


Print Share Comment Cite Upload Translate Updates
APA

koshti Rahul | Sciencx (2021-04-23T06:11:43+00:00) Send Push notification using php(FCM). Retrieved from https://www.scien.cx/2021/04/23/send-push-notification-using-phpfcm/

MLA
" » Send Push notification using php(FCM)." koshti Rahul | Sciencx - Friday April 23, 2021, https://www.scien.cx/2021/04/23/send-push-notification-using-phpfcm/
HARVARD
koshti Rahul | Sciencx Friday April 23, 2021 » Send Push notification using php(FCM)., viewed ,<https://www.scien.cx/2021/04/23/send-push-notification-using-phpfcm/>
VANCOUVER
koshti Rahul | Sciencx - » Send Push notification using php(FCM). [Internet]. [Accessed ]. Available from: https://www.scien.cx/2021/04/23/send-push-notification-using-phpfcm/
CHICAGO
" » Send Push notification using php(FCM)." koshti Rahul | Sciencx - Accessed . https://www.scien.cx/2021/04/23/send-push-notification-using-phpfcm/
IEEE
" » Send Push notification using php(FCM)." koshti Rahul | Sciencx [Online]. Available: https://www.scien.cx/2021/04/23/send-push-notification-using-phpfcm/. [Accessed: ]
rf:citation
» Send Push notification using php(FCM) | koshti Rahul | Sciencx | https://www.scien.cx/2021/04/23/send-push-notification-using-phpfcm/ |

Please log in to upload a file.




There are no updates yet.
Click the Upload button above to add an update.

You must be logged in to translate posts. Please log in or register.