如何将FCM推送通知发送到多个主题 [英] How to send FCM Push Notifications to multiple topics

查看:430
本文介绍了如何将FCM推送通知发送到多个主题的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

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



这是我的代码。

 函数sendPush($ topic,$ msg){
$ API_ACCESS_KEY ='...';
$ msg =数组

'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);


解决方案















$ b $
condition:'dogs'in topics ||'cats'in topics,
priority:high,
通知:{
body:这是Firebase云消息传递主题消息!,
title:FCM消息,
}
}

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

来自 FCM文档的相关引用
$ b


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


请注意,您仅限于2个布尔条件,这意味着您最多可以发送3个主题的消息。 >

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.

Here is my code.

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);
}

解决方案

You use the condition key to send to multiple topics.

For example:

{
  "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".

Relevant quote from 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.

Note that you are limited to 2 boolean conditions, which means you can send a single message to at most 3 topics.

这篇关于如何将FCM推送通知发送到多个主题的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆