#51 - Send FCM push notification from PHP

Date: 2019-03-09 12:00 - PHP

PHP Function to show how to use curl to send a Firebase Cloud Messaging Push Notification.

<?php
function sendPush($to, $data) {
    $headers = array(
        'Authorization: key=<your key>',
        'Content-Type: application/json',
    );

    $data = array(
        'to' => $to,
        'data' => $data
    );

    $ch = curl_init();
    curl_setopt($ch, CURLOPT_HTTPHEADER, $headers);
    curl_setopt($ch, CURLOPT_URL, "https://fcm.googleapis.com/fcm/send");
    curl_setopt($ch, CURLOPT_SSL_VERIFYHOST, 0);
    curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, 0);
    curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
    curl_setopt($ch, CURLOPT_POSTFIELDS, json_encode($data));

    $response = curl_exec($ch);
    curl_close($ch);
    return $response;
}

sendPush('<id of the user>', ['key' => 'value']);

Previous snippet | Next snippet