在没有 Firebase 控制台的情况下向所有设备发送推送通知

Sending Push Notifications to all devices without Firebase Console

我正在制作一个工作应用程序,该应用程序旨在成为在 Android Studio 中使用 Java 的顾客的门户。我已经设置了 Firebase 云消息传递、身份验证和一个实时数据库来存储用户。我可以轻松地从 Cloud Console 发送推送通知,但我想知道是否有一种方法可以在不使用控制台的情况下向所有设备发送推送通知?

我的理由是我不希望 HR 中的某个人不小心删除用户,但我希望他们能够针对关闭等情况发送推送通知。有没有办法做到这一点,要么通过远程推送到所有设备,要么通过控制台的受限版本以便无法进行更改?我对此很陌生,如果这是一个简单的问题,请原谅我。

您可以将 Firebase Admin SDK from a backend or other machine that you fully control. It has an API for sending messages 用于已收集其设备 ID 令牌的设备。

没有用于发送消息的额外控制台或 GUI - 如果控制台不执行您想要的操作,您将不得不编写代码。

您可以向所有设备发送通知,前提是所有设备都订阅了一个主题。

您可以创建一个 php 文件,负责使用 api 令牌将信息发送到 firebase。

如果对您有用,请您认可我的评论,此致。

Notification.php:

    $token = 'test'; // Topic or Token devices here
    $url = "https://fcm.googleapis.com/fcm/send";

    // api key
    $serverKey = ''; // add api key here

    $notification = array('title' => 'Welcome Whosebug' , 'body' => 'Testing body', 'sound' => 'default', 'badge' => '1');
    $data = array('extraInfo'=> 'DomingoMG');
    $arrayToSend = array('to' => $token, 'notification' => $notification, 'priority'=>'high', 'data'=> $data);
    $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);