Dan*_*elo 2 php firebase firebase-cloud-messaging
我想在运行时在 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);
}
Run Code Online (Sandbox Code Playgroud)
如何在 firebase 上运行时创建主题?
Firebase 消息主题无法自行创建 - 一旦有一台设备订阅它们,它们就会开始存在,而当没有设备订阅时,它们就会停止存在。
\n从服务器的角度来看,您可以将消息发送到您选择的任何主题(只要名称本身有效)。在任何情况下,Firebase 都会接受该消息并将其传送到所有订阅的设备(如果没有订阅设备,则为 0)。
\n如果您想将应用程序提供的可用主题发布到应用程序的所有客户端,则需要与 Firebase 分开执行此操作(例如使用 API 端点)。
\n如果您在应用程序中重命名主题,则需要将客户端重新订阅到新主题(最好是取消订阅旧主题)。您可以使用每个实例的实例 ID API ( https://developers.google.com/instance-id/reference/server ) 来执行此操作。请注意,\xe2\x80\x98s 目前无法检索订阅主题的所有设备的列表。\xe2\x80\x98s 也不可能重命名主题并将所有订阅的设备从一个主题移动到另一个主题。这是您必须在应用程序\xe2\x80\x98s 级别上实现的业务逻辑。
\nFirebase 管理 SDK 提供了管理主题订阅的方法,请参阅https://firebase.google.com/docs/admin/setup以获取官方 SDK 列表。
\n如果您需要/想要坚持使用 PHP,可以在https://github.com/kreait/firebase-php上找到\xe2\x80\x98s 一个非官方的 Admin SDK (免责声明:我\xe2\x80\x98m 是维护者)
\n 归档时间: |
|
查看次数: |
3201 次 |
最近记录: |