Home > Mobile >  Firebase web push notifications to all users
Firebase web push notifications to all users

Time:08-01

hi I'm using firebase push notifications to send push notifications I'm using the CodeIgniter framework I want to send notifications to all users please help.

$data = [
                "notification" => [
                    "body"  => $body,
                    "title" => $title,
                    "url"   => $link,
                    "link"   => $link,
                    "image" => $imgurl,
                    "icon" => 'https://timages/favicon.ico'
                ],
                "priority" =>  "high",
                "data" => [
                    "click_action"  =>  "FLUTTER_NOTIFICATION_CLICK",
                    "id"            =>  "1",
                    "status"        =>  "done",
                    "info"          =>  [
                        "title"  => $title,
                        "url"   => $link,
                        "image"  => $imgurl,
                        "icon" => 'https://timages/favicon.ico'
                    ]
                ],
                "to" => "/topic/all"
                ];

            $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[] = 'Authorization: key=< key >';
            $headers[] = 'Content-Type: application/json';
            curl_setopt($ch, CURLOPT_HTTPHEADER, $headers);

            $result = curl_exec($ch);
            curl_close ($ch);

CodePudding user response:

For Codeignighter user the Firebase PHP SDK https://github.com/tattersoftware/codeigniter4-firebase

With refance to this documentation - https://firebase-php.readthedocs.io/en/stable/cloud-messaging.html

use Kreait\Firebase\Messaging\WebPushConfig;

// Example from https://firebase.google.com/docs/cloud-messaging/admin/send-messages#webpush_specific_fields
$config = WebPushConfig::fromArray([
    'notification' => [
        'title' => '$GOOG up 1.43% on the day',
        'body' => '$GOOG gained 11.80 points to close at 835.67, up 1.43% on the day.',
        'icon' => 'https://my-server/icon.png',
    ],
    'fcm_options' => [
        'link' => 'https://my-server/some-page',
    ],
]);

$message = $message->withWebPushConfig($config);

https://firebase-php.readthedocs.io/en/stable/cloud-messaging.html#webpush

Firebase notification send to all android with php

  • Related