使用 PHP 在 firebase 上创建主题
Create a TOPIC on firebase using PHP
我想在运行时创建关于 firebase 的主题。管理员在我的数据库中创建特定记录后,我将创建相应的主题,以便与该主题关联的用户可以使用 firebase 接收通知。
在固定主题的情况下,我可以像这样成功发送符号:
public static function SendFireBaseBroadCast($topicName, $title, $body) {
#API access key from Google API's Console
define('API_ACCESS_KEY', 'API_KEY');
$msg = array
(
'body' => $body,
'title' => $title,
'icon' => 'myicon', /* Default Icon */
'sound' => 'mySound'/* Default sound */
);
$fields = array
(
'to' => "/topics/" . $topicName,
'notification' => $msg
);
$headers = array
(
'Authorization: key=' . API_ACCESS_KEY,
'Content-Type: application/json'
);
$ch = curl_init();
curl_setopt($ch, CURLOPT_URL, 'https://fcm.googleapis.com/fcm/send');
curl_setopt($ch, CURLOPT_POST, true);
curl_setopt($ch, CURLOPT_HTTPHEADER, $headers);
curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, false);
curl_setopt($ch, CURLOPT_POSTFIELDS, json_encode($fields));
$result = curl_exec($ch);
curl_close($ch);
}
如何在 firebase 上运行时创建主题?
无法自行创建 Firebase 消息主题 - 一旦有一台设备订阅它们,它们就会开始存在,并在没有设备订阅时停止存在。
从服务器的角度来看,您可以向您选择的任何主题发送消息(只要名称本身有效)。 Firebase 在任何情况下都会接受该消息并将其传递给所有订阅的设备(如果没有设备订阅则为 0)。
如果您想将应用程序提供的可用主题发布到应用程序的所有客户端,您需要独立于 Firebase 执行此操作(例如使用 API 端点)。
如果您在应用程序中重命名主题,则需要重新为客户订阅新主题(最好取消订阅旧主题)。您可以使用每个实例的实例 ID API (https://developers.google.com/instance-id/reference/server) 来执行此操作。请注意,目前无法检索订阅某个主题的所有设备的列表。也不可能重命名主题并将所有订阅的设备从一个主题移动到另一个主题。这是您必须在应用程序级别实现的业务逻辑。
Firebase Admin SDK 提供了管理主题订阅的方法,请参阅https://firebase.google.com/docs/admin/setup 获取官方 SDK 列表。
如果您 need/want 坚持 PHP,https://github.com/kreait/firebase-php 有一个非官方的 Admin SDK(免责声明:我是维护者)
我想在运行时创建关于 firebase 的主题。管理员在我的数据库中创建特定记录后,我将创建相应的主题,以便与该主题关联的用户可以使用 firebase 接收通知。
在固定主题的情况下,我可以像这样成功发送符号:
public static function SendFireBaseBroadCast($topicName, $title, $body) {
#API access key from Google API's Console
define('API_ACCESS_KEY', 'API_KEY');
$msg = array
(
'body' => $body,
'title' => $title,
'icon' => 'myicon', /* Default Icon */
'sound' => 'mySound'/* Default sound */
);
$fields = array
(
'to' => "/topics/" . $topicName,
'notification' => $msg
);
$headers = array
(
'Authorization: key=' . API_ACCESS_KEY,
'Content-Type: application/json'
);
$ch = curl_init();
curl_setopt($ch, CURLOPT_URL, 'https://fcm.googleapis.com/fcm/send');
curl_setopt($ch, CURLOPT_POST, true);
curl_setopt($ch, CURLOPT_HTTPHEADER, $headers);
curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, false);
curl_setopt($ch, CURLOPT_POSTFIELDS, json_encode($fields));
$result = curl_exec($ch);
curl_close($ch);
}
如何在 firebase 上运行时创建主题?
无法自行创建 Firebase 消息主题 - 一旦有一台设备订阅它们,它们就会开始存在,并在没有设备订阅时停止存在。
从服务器的角度来看,您可以向您选择的任何主题发送消息(只要名称本身有效)。 Firebase 在任何情况下都会接受该消息并将其传递给所有订阅的设备(如果没有设备订阅则为 0)。
如果您想将应用程序提供的可用主题发布到应用程序的所有客户端,您需要独立于 Firebase 执行此操作(例如使用 API 端点)。
如果您在应用程序中重命名主题,则需要重新为客户订阅新主题(最好取消订阅旧主题)。您可以使用每个实例的实例 ID API (https://developers.google.com/instance-id/reference/server) 来执行此操作。请注意,目前无法检索订阅某个主题的所有设备的列表。也不可能重命名主题并将所有订阅的设备从一个主题移动到另一个主题。这是您必须在应用程序级别实现的业务逻辑。
Firebase Admin SDK 提供了管理主题订阅的方法,请参阅https://firebase.google.com/docs/admin/setup 获取官方 SDK 列表。
如果您 need/want 坚持 PHP,https://github.com/kreait/firebase-php 有一个非官方的 Admin SDK(免责声明:我是维护者)