如何将FCM推送通知发送到多个主题

时间:2017-06-07 12:45:25

标签: php firebase firebase-cloud-messaging

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

1 个答案:

答案 0 :(得分:8)

您可以使用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

相关问题