Quay lại

Push Notification tới thiết bị Android và IOS dùng Firebase kết hợp với PHP Chuyên mục PHP và Laravel    2024-02-29    79 Lượt xem    56 Lượt thích    comment-3 Created with Sketch Beta. 0 Bình luận

Push Notification tới thiết bị Android và IOS dùng Firebase kết hợp với PHP

Hi! Tất cả mọi người, gần đây mình có làm một dự án cần phải gửi notifications đến người dùng trên thiết bị Android và IOS, Mình đã quyết định dùng Firebase Cloud Messaging hay còn gọi (FCM) để gửi thông báo đến người dùng, thằng FCM này nó có 2 cách để hỗ trợ chúng ta gửi notifications Cloud Messaging API (Legacy) Firebase Cloud Messaging API (V1), với thằng Legacy nó đã quá cũ rồi,và dừng hoạt động từ ngày 20 tháng 6 năm 2023 và sẽ bị xóa vào tháng 6 năm 2024. Nên ở bài hướng dẫn này chúng ta chỉ tập trung vào API (V1), còn bạn nào muốn migrate từ Legacy lên V1 thì xem bài trên trang doc của FCM này nhé!

Để nói về thằng FCM này thì nó có nhiều tính năng, mình liệt kê dưới đây để cho các bạn biết thêm hoặc lên trang doc của FCM để đọc nhé:

  1. Gửi thông báo hoặc tin nhắn dữ liệu: Gửi các thông báo hiển thị cho người dùng của bạn.
  2. Định dạng thông điệp linh hoạt: Phân phối các messages đến các clients của bạn bằng một trong ba cách— đến các thiết bị đơn lẻ, đến group thiết bị hoặc đến các thiết bị đã đăng ký(subcribe) vào chủ đề (topic).
  3. Gửi các thông điệp từ máy khách: Gửi confirmations, chats, và các messages khác từ thiết bị của bạn trở lại server của bạn qua FCM's reliable, battery-saving của FCM.

Khi nói đến việc gửi thông báo từ aplication của chúng ta tới users, có nhiều cách để thực hiện điều này. Bạn có thể gửi email, tin nhắn SMS, hoặc thậm chí là Push Notification. Các chanel này giúp chúng ta tạo ra các giải pháp cung cấp thông báo tùy chỉnh cho users với quy mô lớn.

Push Notification đã trở thành các tính năng cần thiết cho các ứng dụng web và di động hiện đại. Chúng cung cấp trải nghiệm người dùng cá nhân hóa vì chúng có thể được điều chỉnh để gửi khi người dùng đang sử dụng thiết bị của họ.

Trong bài viết này, chúng ta sẽ bàn về cách gửi Push Notification sử dụng Firebase, PHP. Và mình sẽ hướng dẫn các bạn gửi noti đến một người dùng cụ thể và một nhóm người dùng (topic) thông qua device token trên thiết bị của user ( IOS và Android ).

Tạo Firebase Project

Để ứng dụng của chúng ta có thể gửi được noti thì điều kiện bắt buộc chúng ta cần phải tạo một Firebase project, để làm được điều này các bạn lên console.firebase.google.com , Sau đó thì làm theo các bước hướng dẫn sau:

Bước 1: Tạo Project:

Bước 2: Thêm IOS và Anroid App vào Project.

Một Project chúng ta có thể thêm nhiều ứng dụng, với demo chúng ta sẽ thêm 2 ứng dụng là IOS và Android. Sau khi chúng ta thêm ứng dụng IOS, thì các bạn lại tiếp tục thêm ứng dụng Anroid nữa nhé! Trong bài mình chỉ hướng dẫn tạo ứng dụng IOS thôi.

Nhập thông tin và làm theo theo hướng dẫn của Firebase console.

Lưu ý: Bạn có thể tìm thấy Bundle Identifier trong General tab trong Xcode cho primary app của bạn.

Bước 3: Tải Firebase Admin SDK

Firebase Admin SDK là một bộ công cụ cung cấp bởi Firebase để quản lý và tương tác với các dịch vụ Firebase từ máy chủ hoặc backend của ứng dụng. Nó cung cấp các tính năng mạnh mẽ để thực hiện các tác vụ quản lý dữ liệu, xác thực người dùng, và gửi thông báo. Để tải về được các bạn hãy vào phần setting của Project mà các bạn vừa tạo và sau đó click vào Generate new private key để tải account_credential.json.

Sau khi tải về các bạn hãy để nó trong thư mục nào đó trong dự án của bạn. Ví dụ mình đặt trong thư mục config trong dự án của mình như này.

Sau đó thì các bạn định nghĩa các HẰNG SỐ để cấu hình kết nối và xác thực với dịch vụ Firebase Cloud Messaging (FCM), hãy định nghĩa chúng trong .ENV, hoặc một global config để chúng ta có thể sử dụng trên toàn bộ hệ thống:

// Firebase configuration
define('FCM_PROJECT_ID', "fcm_project_id");
define('FCM_ACCOUNT_CREDENTIAL', __DIR__ .'/fcm_account_credentials/pro_account_credential.json');
  1. FCM_PROJECT_ID: Đây là ID dự án của ứng dụng trên Firebase. Mỗi dự án Firebase sẽ có một ID duy nhất để xác định nó. (Các bạn nhớ lấy ID này trong Project của bạn trên Firebase console).

  2. FCM_ACCOUNT_CREDENTIAL: Đây là đường dẫn tới tệp tin chứa thông tin xác thực cho tài khoản dịch vụ. Tệp tin JSON này chứa thông tin cần thiết để xác thực với dịch vụ Firebase.

Chúng ta sẽ sử dụng 2 hằng số này trong phần tiếp theo, nên các bạn chắc chắn một điều phải config đúng 2 hằng số này.

Ok vậy là đã hoàn thành cấu hình trên Firebase console, bây giờ chúng ta sẽ chuyển sang phần kết hợp nó với PHP nhé!

Xử lý push notification trong PHP

Cách này mình đang làm trên php thuần, còn nếu các bạn sử dụng cho Laravel Firebase Symfony Firebase hay các framework khác mà hỗ trợ composer thì cũng làm tương tự mà thôi! Dưới đây là các bước mình làm, bạn nào đã từng làm rồi và chỉ muốn tham khảo code thôi thì hãy kéo xuống bài để đọc code luôn nhé! 

Bước 1: Cài đặt Firebase SDK và DB:

Bạn cần cài đặt Firebase SDK cho PHP. Bạn có thể sử dụng Composer để cài đặt SDK Firebase cho PHP thông qua package kreait/firebase-php. Thực chất mà nói mình cũng chỉ đọc doc trên Firebase Admin SDK for PHP mà làm thôi, các bạn cũng có thể lên đó để tham khảo nhé!

Dưới đây là cách cài đặt thư viện Firebase SDK cho PHP.

composer require kreait/firebase-php

Bên dưới là DB của mình:

users
- id
- name
- email
- ...

device_tokens
- id
- user_id
- token
- device_type // IOS or Android
- ...

Bước 2: Xác thực với Firebase

Các bạn có thể viết thêm một file AppFcmHelper.php để chuyên xử lý API gửi notifications đến người dùng, file này sẽ bao gồm: Xác thực với Firebase các function để xử lý gửi notifications đến users.

Dưới đây là cách xác thực:

<?php

namespace R3;

use Google\Client as Google_Client;
use Kreait\Firebase\Factory;
use Kreait\Firebase\ServiceAccount;

/******************************************************************
 * AppFcmHelper class
******************************************************************/
    class AppFcmHelper
    {
       // Hàm này được sử dụng để lấy access token từ dịch vụ Firebase, cần thiết để xác thực các request API đến FCM.
        private static function getAccessToken() {
            $client = new Google_Client();
            $client->setAuthConfig(FCM_ACCOUNT_CREDENTIAL);
            $client->setScopes(['https://www.googleapis.com/auth/firebase.messaging']);

            // Get the access token
            $accessToken = $client->fetchAccessTokenWithAssertion();

            return $accessToken ? $accessToken['access_token'] : null;
        }


        // Hàm này được sử dụng để thiết lập và trả về một phiên bản của Firebase Messaging SDK, sử dụng để gửi thông báo đến các thiết bị qua FCM.
        private static function setupMessageSDK() {
            $serviceAccount = ServiceAccount::fromJsonFile(FCM_ACCOUNT_CREDENTIAL); // Replace with your service account key
            $firebase = (new Factory)
                ->withServiceAccount($serviceAccount)
                ->create();

            // Get the FCM messaging instance
            $messaging = $firebase->getMessaging();

            return $messaging;
        }
    }
?>

Bước 3 : Xử lý push notifications

  1. Gửi noti đến các device là Android: Trong hàm này các bạn có thể gửi đến 1 hoặc nhiều thiết bị là Android, các bạn chú ý với phiên bản V1 này nếu muốn gửi đến nhiều device (multiple device) thì chúng ta cần phải tạo ra 1 TOPIC, sau đó chúng ta sẽ cho tất cả device này subcrible vào TOPIC, Sau khi gửi xong noti đến users thì chúng ta lại Unsubcribe đi, đây là cách mình làm, còn các bạn có cách nào hay hơn thì cứ sử dụng nhé!
    • public static function send_notify_firebase_android($device_token, $data) {
          $url = 'https://fcm.googleapis.com/v1/projects/'.FCM_PROJECT_ID.'/messages:send';
          $fields = array();
          $fields['data'] = $data;
          if (is_array($device_token)) {
              $topic = 'specific_push_notification_android';
              $messaging = self::setupMessageSDK();
              $messaging->subscribeToTopic($topic, $device_token);
              $result = self::send_notify_firebase_topic_android($data, $topic);
              $messaging->unsubscribeFromTopic($topic, $device_token);
              return $result;
          } else {
              $fields['token'] = $device_token; //send a device
          }
      
          $fields['android']['priority'] = 'high';
          $headers = array(
              'Content-Type:application/json',
              'Authorization: Bearer ' . self::getAccessToken(),
          );
      
          $params = array();
          $params['message'] = $fields;
      
          $ch = curl_init();
          curl_setopt($ch, CURLOPT_URL, $url);
          curl_setopt($ch, CURLOPT_POST, true);
          curl_setopt($ch, CURLOPT_HTTPHEADER, $headers);
          curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
          curl_setopt($ch, CURLOPT_SSL_VERIFYHOST, 0);
          curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, false);
          curl_setopt($ch, CURLOPT_POSTFIELDS, json_encode($params));
      
          $result = curl_exec($ch);
          if ($result === FALSE) {
              die('FCM Send Error: '  .  curl_error($ch));
          }
          curl_close($ch);
      
          return $result;//true or false
      }
  2. Gửi noti đến các device là IOS: Nó cũng tương tự như Android mà thôi, nhưng nó sẽ có một số thuộc tính khác, cái này thì các bạn có thể đọc thêm doc để có thể ứng dụng vào dự án của bạn nhé!.
    • public static function send_notify_firebase_ios($device_token, $data, $notification){
          $url = 'https://fcm.googleapis.com/v1/projects/'.FCM_PROJECT_ID.'/messages:send';
          $fields = array();
          $fields['data'] = $notification;
          $fields['notification'] = $data;
          $fields['apns']['payload']['aps']["mutable_content"] = 1;
          $fields['apns']['fcm_options']['image'] = $notification['image_url'];
          if (is_array($device_token)) {
              $topic = 'specific_push_notification_ios';
              $messaging = self::setupMessageSDK();
              $messaging->subscribeToTopic($topic, $device_token);
              $result = self::send_notify_firebase_topic_ios($data, $notification, $topic);
              $messaging->unsubscribeFromTopic($topic, $device_token);
              return $result;
          } else {
              $fields['token'] = $device_token; //send a device
          }
      
          $headers = array(
              'Content-Type:application/json',
              'Authorization: Bearer ' . self::getAccessToken(),
          );
      
          $params = array();
          $params['message'] = $fields;
      
          $ch = curl_init();
          curl_setopt($ch, CURLOPT_URL, $url);
          curl_setopt($ch, CURLOPT_POST, true);
          curl_setopt($ch, CURLOPT_HTTPHEADER, $headers);
          curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
          curl_setopt($ch, CURLOPT_SSL_VERIFYHOST, 0);
          curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, false);
          curl_setopt($ch, CURLOPT_POSTFIELDS, json_encode($params));
      
          $result = curl_exec($ch);
          if ($result === FALSE) {
              die('FCM Send Error: '  .  curl_error($ch));
          }
          curl_close($ch);
      
          return $result;//true or false
      }
  3. Gửi noti đến các device là Android qua Topic
    • public static function send_notify_firebase_topic_android($notification, $topic) {
          $url = 'https://fcm.googleapis.com/v1/projects/'.FCM_PROJECT_ID.'/messages:send';
          $fields = array();
          $fields['data'] = $notification;
          $fields['topic'] = $topic; //send topic
          $fields['android']['priority'] = 'high';
          $headers = array(
              'Content-Type:application/json',
              'Authorization: Bearer ' . self::getAccessToken(),
          );
      
          $params = array();
          $params['message'] = $fields;
      
          $ch = curl_init();
          curl_setopt($ch, CURLOPT_URL, $url);
          curl_setopt($ch, CURLOPT_POST, true);
          curl_setopt($ch, CURLOPT_HTTPHEADER, $headers);
          curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
          curl_setopt($ch, CURLOPT_SSL_VERIFYHOST, 0);
          curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, false);
          curl_setopt($ch, CURLOPT_POSTFIELDS, json_encode($params));
      
          $result = curl_exec($ch);
          if ($result === FALSE) {
              die('FCM Send Error: '  .  curl_error($ch));
          }
          curl_close($ch);
      
          return $result;//true or false
      }
  4. Gửi noti đến các device là IOS qua Topic
    • public static function send_notify_firebase_topic_ios($data, $notification, $topic){
          $url = 'https://fcm.googleapis.com/v1/projects/'.FCM_PROJECT_ID.'/messages:send';
          $fields = array();
          $fields['data'] = $notification;
          $fields['notification'] = $data;
          $fields['apns']['payload']['aps']["mutable_content"] = 1;
          $fields['apns']['fcm_options']['image'] = $notification['image_url'];
          $fields['topic'] = $topic; //send topic ios
          $headers = array(
              'Content-Type:application/json',
              'Authorization: Bearer ' . self::getAccessToken(),
          );
      
          $params = array();
          $params['message'] = $fields;
      
          $ch = curl_init();
          curl_setopt($ch, CURLOPT_URL, $url);
          curl_setopt($ch, CURLOPT_POST, true);
          curl_setopt($ch, CURLOPT_HTTPHEADER, $headers);
          curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
          curl_setopt($ch, CURLOPT_SSL_VERIFYHOST, 0);
          curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, false);
          curl_setopt($ch, CURLOPT_POSTFIELDS, json_encode($params));
      
          $result = curl_exec($ch);
          if ($result === FALSE) {
              die('FCM Send Error: '  .  curl_error($ch));
          }
          curl_close($ch);
      
          return $result;//true or false
      }

Bước 4: Cách sử dụng:

Định nghĩa data sample để gửi noti.

<?php

use R3\AppFcmHelper;

// Định nghĩa data sample để gửi noti
$pushNotification = [
    'type' => "notice",
    'body' => "Test",
    'url' => "your_url",
    "image_url" => "image_url",
];

?>

TH nếu gửi notifictions đến một hoặc nhiều devices với thiết bị Android:

<?php

use R3\AppFcmHelper;

// Định nghĩa data sample để gửi noti
$pushNotification = [
    'type' => "notice",
    'body' => "Test",
    'url' => "your_url",
    "image_url" => "image_url",
];

/* *** Gửi multiple devices *** */
$arrDeviceTokenAndroid = [];
// xử lý logic để push device token vào mảng $arrDeviceTokenAndroid
if( count($arrDeviceTokenAndroid)) {
 echo AppFcmHelper::send_notify_firebase_android($arrDeviceTokenAndroid, $pushNotification);
}

/* *** Gửi đến một device cụ thể *** */
$token = "abc_11";

if($token) {
 echo AppFcmHelper::send_notify_firebase_android($token, $pushNotification);
}
?>

TH nếu gửi notifictions đến một hoặc nhiều devices với thiết bị IOS:

<?php

use R3\AppFcmHelper;

// Định nghĩa data sample để gửi noti
$pushNotification = [
    'type' => "notice",
    'body' => "Test",
    'url' => "your_url",
    "image_url" => "image_url",
];

/* *** Gửi multiple devices token *** */
$arrDeviceTokenIos = [];
// xử lý logic để push device token vào mảng $arrDeviceTokenIos 
if( count($arrDeviceTokenIos)) {
 echo AppFcmHelper::send_notify_firebase_ios($arrDeviceTokenIos , $pushNotification);
}

/* *** Gửi đến một device token cụ thể *** */
$token = "abc_11";

if($token) {
 echo AppFcmHelper::send_notify_firebase_ios($token, $pushNotification);
}
?>

TH nếu gửi notifictions đến một topic với thiết bị IOS hoặc Android:

<?php

use R3\AppFcmHelper;

$data_notify = [
    "body" => "test",
];

// Định nghĩa data sample để gửi noti
$pushNotification = [
    'type' => "notice",
    'body' => "Test",
    'url' => "your_url",
    "image_url" => "image_url",
];

/* *** Gửi noti đến users thông qua topic với thiết bị là Android  *** */
echo AppFcmHelper::send_notify_firebase_topic_android($pushNotification, 'push_notification');

/* *** Gửi noti đến users thông qua topic với thiết bị là IOS *** */
echo AppFcmHelper::send_notify_firebase_topic_ios($data_notify, $pushNotification, 'push_notification_ios');
?>

Tổng kết

Xin lỗi anh em là mình chưa optimize lại code, để nó ngắn gọn hơn, mọi người có thể viết ra một hàm chung để xử lý chỗ cURL call API tới Firebase nhá nhá!

<?php

namespace R3;

use Google\Client as Google_Client;
use Kreait\Firebase\Factory;
use Kreait\Firebase\ServiceAccount;
use Kreait\Firebase\Messaging\CloudMessage;

/******************************************************************
 * AppHelper class
******************************************************************/
    class AppHelper
    {
        public static function send_notify_firebase_android($device_token, $data) {
            $url = 'https://fcm.googleapis.com/v1/projects/'.FCM_PROJECT_ID.'/messages:send';
            $fields = array();
            $fields['data'] = $data;
            if (is_array($device_token)) {
                $topic = 'specific_push_notification_android';
                $messaging = self::setupMessageSDK();
                $messaging->subscribeToTopic($topic, $device_token);
                $result = self::send_notify_firebase_topic_android($data, $topic);
                $messaging->unsubscribeFromTopic($topic, $device_token);
                return $result;
            } else {
                $fields['token'] = $device_token; //send a device
            }

            $fields['android']['priority'] = 'high';
            $headers = array(
                'Content-Type:application/json',
                'Authorization: Bearer ' . self::getAccessToken(),
            );

            $params = array();
            $params['message'] = $fields;

            $ch = curl_init();
            curl_setopt($ch, CURLOPT_URL, $url);
            curl_setopt($ch, CURLOPT_POST, true);
            curl_setopt($ch, CURLOPT_HTTPHEADER, $headers);
            curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
            curl_setopt($ch, CURLOPT_SSL_VERIFYHOST, 0);
            curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, false);
            curl_setopt($ch, CURLOPT_POSTFIELDS, json_encode($params));

            $result = curl_exec($ch);
            if ($result === FALSE) {
                die('FCM Send Error: '  .  curl_error($ch));
            }
            curl_close($ch);

            return $result;//true or false
        }

        public static function send_notify_firebase_ios($device_token, $data, $notification){
            $url = 'https://fcm.googleapis.com/v1/projects/'.FCM_PROJECT_ID.'/messages:send';
            $fields = array();
            $fields['data'] = $notification;
            $fields['notification'] = $data;
            $fields['apns']['payload']['aps']["mutable_content"] = 1;
            $fields['apns']['fcm_options']['image'] = $notification['image_url'];
            if (is_array($device_token)) {
                $topic = 'specific_push_notification_ios';
                $messaging = self::setupMessageSDK();
                $messaging->subscribeToTopic($topic, $device_token);
                $result = self::send_notify_firebase_topic_ios($data, $notification, $topic);
                $messaging->unsubscribeFromTopic($topic, $device_token);
                return $result;
            } else {
                $fields['token'] = $device_token; //send a device
            }

            $headers = array(
                'Content-Type:application/json',
                'Authorization: Bearer ' . self::getAccessToken(),
            );

            $params = array();
            $params['message'] = $fields;

            $ch = curl_init();
            curl_setopt($ch, CURLOPT_URL, $url);
            curl_setopt($ch, CURLOPT_POST, true);
            curl_setopt($ch, CURLOPT_HTTPHEADER, $headers);
            curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
            curl_setopt($ch, CURLOPT_SSL_VERIFYHOST, 0);
            curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, false);
            curl_setopt($ch, CURLOPT_POSTFIELDS, json_encode($params));

            $result = curl_exec($ch);
            if ($result === FALSE) {
                die('FCM Send Error: '  .  curl_error($ch));
            }
            curl_close($ch);

            return $result;//true or false
        }

        public static function send_notify_firebase_topic_android($notification, $topic) {
            $url = 'https://fcm.googleapis.com/v1/projects/'.FCM_PROJECT_ID.'/messages:send';
            $fields = array();
            $fields['data'] = $notification;
            $fields['topic'] = $topic; //send topic
            $fields['android']['priority'] = 'high';
            $headers = array(
                'Content-Type:application/json',
                'Authorization: Bearer ' . self::getAccessToken(),
            );

            $params = array();
            $params['message'] = $fields;

            $ch = curl_init();
            curl_setopt($ch, CURLOPT_URL, $url);
            curl_setopt($ch, CURLOPT_POST, true);
            curl_setopt($ch, CURLOPT_HTTPHEADER, $headers);
            curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
            curl_setopt($ch, CURLOPT_SSL_VERIFYHOST, 0);
            curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, false);
            curl_setopt($ch, CURLOPT_POSTFIELDS, json_encode($params));

            $result = curl_exec($ch);
            if ($result === FALSE) {
                die('FCM Send Error: '  .  curl_error($ch));
            }
            curl_close($ch);

            return $result;//true or false
        }

        public static function send_notify_firebase_topic_ios($data, $notification, $topic){
            $url = 'https://fcm.googleapis.com/v1/projects/'.FCM_PROJECT_ID.'/messages:send';
            $fields = array();
            $fields['data'] = $notification;
            $fields['notification'] = $data;
            $fields['apns']['payload']['aps']["mutable_content"] = 1;
            $fields['apns']['fcm_options']['image'] = $notification['image_url'];
            $fields['topic'] = $topic; //send topic ios
            $headers = array(
                'Content-Type:application/json',
                'Authorization: Bearer ' . self::getAccessToken(),
            );

            $params = array();
            $params['message'] = $fields;

            $ch = curl_init();
            curl_setopt($ch, CURLOPT_URL, $url);
            curl_setopt($ch, CURLOPT_POST, true);
            curl_setopt($ch, CURLOPT_HTTPHEADER, $headers);
            curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
            curl_setopt($ch, CURLOPT_SSL_VERIFYHOST, 0);
            curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, false);
            curl_setopt($ch, CURLOPT_POSTFIELDS, json_encode($params));

            $result = curl_exec($ch);
            if ($result === FALSE) {
                die('FCM Send Error: '  .  curl_error($ch));
            }
            curl_close($ch);

            return $result;//true or false
        }

        private static function getAccessToken() {
            $client = new Google_Client();
            $client->setAuthConfig(FCM_ACCOUNT_CREDENTIAL);
            $client->setScopes(['https://www.googleapis.com/auth/firebase.messaging']);

            // Get the access token
            $accessToken = $client->fetchAccessTokenWithAssertion();

            return $accessToken ? $accessToken['access_token'] : null;
        }

        private static function setupMessageSDK() {
            $serviceAccount = ServiceAccount::fromJsonFile(FCM_ACCOUNT_CREDENTIAL); // Replace with your service account key
            $firebase = (new Factory)
                ->withServiceAccount($serviceAccount)
                ->create();

            // Get the FCM messaging instance
            $messaging = $firebase->getMessaging();

            return $messaging;
        }
    }
?>
 
 
 
 
 
 
 

Bình luận (0)

Michael Gough
Michael Gough
Michael Gough
Michael Gough
Michael Gough
Michael Gough
Michael Gough
Michael Gough
Michael Gough
Michael Gough
Michael Gough
Michael Gough
Michael Gough
Michael Gough
Michael Gough
Michael Gough

Bài viết liên quan

Learning English Everyday