Search code snippets, questions, articles...

Firebase push notification curl request in PHP

This is a working example of a firebase push notification in PHP that uses CURL to send a request for a firebase cloud messaging service to send a push notification.
<?php
    $conn = common::createConnection();

    $title = $_POST['title'];
    $body = $_POST['body'];
    $image = $_POST['image'];
    $link = $_POST['link'];
    
    $data = [
        "notification" => [
            "body"  => $body,
            "title" => $title,
            "image" => $image
        ],
        "priority" =>  "high",
        "data" => [
            "click_action"  =>  "FLUTTER_NOTIFICATION_CLICK",
            "id"            =>  "1",
            "status"        =>  "done",
            "info"          =>  [
                "title"  => $title,
                "link"   => $link,
                "image"  => $image
            ]
        ],
        "to" => "<YOUR_FIREBASE_TOKEN>"
    ];

    $ch = curl_init();

    curl_setopt($ch, CURLOPT_URL, 'https://fcm.googleapis.com/fcm/send');
    curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
    curl_setopt($ch, CURLOPT_POSTFIELDS, json_encode($data));
    curl_setopt($ch, CURLOPT_POST, 1);

    $headers = array();
    $headers[] = 'Content-Type: application/json';
    $headers[] = 'Authorization: key=<YOUR-AUTH-KEY>';
    curl_setopt($ch, CURLOPT_HTTPHEADER, $headers);

    $result = curl_exec($ch);
    curl_close ($ch);
    
    echo "request sent";
?>
Best JSON Validator, JSON Tree Viewer, JSON Beautifier at same place.
Was this helpful?
0 Comments