How to send FCM Push Notifications to multiple topics(如何向多个主题发送 FCM 推送通知)
问题描述
我正在使用 php 服务器发送 FCM 推送通知.我想知道如何同时向多个主题发送推送通知.
I am using php server to send FCM push notifications. I want to know how I can send push notifications to multiple topics at the same time.
这是我的代码.
function sendPush($topic,$msg){
$API_ACCESS_KEY = '...';
$msg = array
(
'msg' => $msg
);
$fields = array('to' => '/topics/' . $topic, 'priority' => 'high', 'data' => $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));
$pushResult = curl_exec($ch);
curl_close($ch);
}
推荐答案
您可以使用 condition
键发送到多个主题.
You use the condition
key to send to multiple topics.
例如:
{
"condition": "'dogs' in topics || 'cats' in topics",
"priority" : "high",
"notification" : {
"body" : "This is a Firebase Cloud Messaging Topic Message!",
"title" : "FCM Message",
}
}
这会将消息发送到订阅主题狗"或猫"的设备.
This would send the message to devices subscribed to topics "dogs" or "cats".
来自FCM 文档的相关引用:
要发送到多个主题的组合,应用服务器将条件键设置为指定目标主题的布尔条件.
To send to combinations of multiple topics, the app server sets the condition key to a boolean condition that specifies the target topics.
请注意,您仅限于 2 个布尔条件,这意味着您最多可以向 3 个主题发送一条消息.
Note that you are limited to 2 boolean conditions, which means you can send a single message to at most 3 topics.
更新:现在您可以包含 5 个主题.
Update: Now you can include 5 topics.
您最多可以在条件表达式中包含五个主题,并且支持括号.支持的运算符:&&、||、!.请注意用于 !:
You can include up to five topics in your conditional expression, and parentheses are supported. Supported operators: &&, ||, !. Note the usage for !:
阅读文档:https://firebase.google.com/docs/cloud-messaging/send-message
这篇关于如何向多个主题发送 FCM 推送通知的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持编程学习网!
本文标题为:如何向多个主题发送 FCM 推送通知
基础教程推荐
- 在 yii2 中迁移时出现异常“找不到驱动程序" 2022-01-01
- HTTP 与 FTP 上传 2021-01-01
- 在 CakePHP 2.0 中使用 Html Helper 时未定义的变量 2021-01-01
- 如何在 Symfony 和 Doctrine 中实现多对多和一对多? 2022-01-01
- 如何在 XAMPP 上启用 mysqli? 2021-01-01
- PHP 守护进程/worker 环境 2022-01-01
- 找不到类“AppHttpControllersDB",我也无法使用新模型 2022-01-01
- Doctrine 2 - 在多对多关系中记录更改 2022-01-01
- 使用 PDO 转义列名 2021-01-01
- phpmyadmin 错误“#1062 - 密钥 1 的重复条目‘1’" 2022-01-01