gpt4 book ai didi

php - 如何向多个主题发送 FCM 推送通知

转载 作者:可可西里 更新时间:2023-10-31 22:12:15 31 4
gpt4 key购买 nike

我正在使用 php 服务器发送 FCM 推送通知。我想知道如何同时向多个主题发送推送通知。

这是我的代码。

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 键发送到多个主题。

例如:

{
"condition": "'dogs' in topics || 'cats' in topics",
"priority" : "high",
"notification" : {
"body" : "This is a Firebase Cloud Messaging Topic Message!",
"title" : "FCM Message",
}
}

这会将消息发送到订阅主题“狗”或“猫”的设备。

相关引自FCM docs :

To send to combinations of multiple topics, the app server sets the condition key to a boolean condition that specifies the target topics.

请注意,您只能使用 2 个 bool 条件,这意味着您最多可以向 3 个主题发送一条消息。

更新:现在您可以包含 5 个主题。

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

关于php - 如何向多个主题发送 FCM 推送通知,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/44413037/

31 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com