【问题标题】:How to send FCM Multiple notification message to different topics at once如何一次向不同主题发送 FCM Multiple 通知消息
【发布时间】:2019-08-21 19:52:56
【问题描述】:

我检查了很多答案,它们都是关于向不同主题发送通知的,但我想一次向不同主题发送不同的消息。

我有 100 多个主题要发送通知,一一发送需要太多时间。

我没有找到任何 API 文档,所以我尝试了不同的技术,例如 $fields 发送多维数组,但它返回一个错误。

我的代码一次只发送 1 个通知。

如何在一个请求中发送所有消息?

 // Sending through PHP Curl request
 $url = 'https://fcm.googleapis.com/fcm/send';

 $msgs = array();
 $msgs["topic_1"] = array("title"=>"title 1","body"=>"topic 1 message");
 $msgs["topic_2"] = array("title"=>"title 2","body"=>"topic 2 message");
 $msgs["topic_3"] = array("title"=>"title 3","body"=>"topic 3 message");
 ...
 $msgs["topic_100"] = array("title"=>"title 100","body"=>"topic 100 message");


 foreach ($msgs as $topic => $message) {
    $fields = array(
         'to' => "/topics/$topic",
         "priority"=> "high",
         'data' => $message,
         "time_to_live" => 7200
    );

    $headers = array('Authorization:key=API_KEY',
        'Content-Type: application/json'
    );


    $ch = curl_init();
    curl_setopt($ch, CURLOPT_URL, $url);
    curl_setopt($ch, CURLOPT_POST, true);
    curl_setopt($ch, CURLOPT_HTTPHEADER, $headers);
    curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
    curl_setopt($ch, CURLOPT_SSL_VERIFYHOST, 0);  
    curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, false);
    curl_setopt($ch, CURLOPT_POSTFIELDS, json_encode($fields));
    $result = curl_exec($ch);           
    curl_close($ch);
 } // loop end

 //above code only send 1 message at once..

【问题讨论】:

    标签: php android firebase notifications firebase-cloud-messaging


    【解决方案1】:

    参考这个doc

    您可以在按摩中使用 condition 参数向多个主题发送通知

    例如,以下条件将向订阅了 TopicA 和 TopicB 或 TopicC 的设备发送消息:

    “主题中的'TopicA' &&(主题中的'TopicB' || 主题中的'TopicC')”

    完整示例:

    // Define a condition which will send to devices which are subscribed
    // to either the Google stock or the tech industry topics.
    var condition = "'stock-GOOG' in topics || 'industry-tech' in topics";
    
    // See documentation on defining a message payload.
    var message = {
      notification: {
        title: '$GOOG up 1.43% on the day',
        body: '$GOOG gained 11.80 points to close at 835.67, up 1.43% on the day.'
      },
      condition: condition
    };
    
    // Send a message to devices subscribed to the combination of topics
    // specified by the provided condition.
    admin.messaging().send(message)
      .then((response) => {
        // Response is a message ID string.
        console.log('Successfully sent message:', response);
      })
      .catch((error) => {
        console.log('Error sending message:', error);
      });

    注意:您最多可以在条件句中包含 五个 主题 表达。

    【讨论】:

      猜你喜欢
      • 2017-11-08
      • 1970-01-01
      • 2021-04-09
      • 1970-01-01
      • 1970-01-01
      • 2019-07-19
      • 1970-01-01
      • 1970-01-01
      • 2019-11-06
      相关资源
      最近更新 更多