【问题标题】:How to send FCM Push Notifications to multiple topics如何向多个主题发送 FCM 推送通知
【发布时间】:2017-11-08 19:54:57
【问题描述】:

我正在使用 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);
}

【问题讨论】:

    标签: php firebase push-notification notifications firebase-cloud-messaging


    【解决方案1】:

    您使用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的相关引用:

    要发送到多个主题的组合,应用服务器将条件键设置为指定目标主题的布尔条件。

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

    更新:现在您可以添加 5 个主题。

    您最多可以在条件表达式中包含五个主题,并且 支持括号。支持的运算符:&&、||、!。注意 !的用法:

    阅读文档: https://firebase.google.com/docs/cloud-messaging/send-message

    【讨论】:

    • 感谢您的回复。我应该在字段中使用什么? ('to' => '/topics/foo')。有没有像'/topics/foo/bar/etc'这样的直接方法
    • @user934820 什么都没有。在这种情况下,您不包括该字段。
    猜你喜欢
    • 2019-07-19
    • 1970-01-01
    • 1970-01-01
    • 2022-09-28
    • 1970-01-01
    • 2021-09-24
    • 2018-03-22
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多