【问题标题】:firebase - php - send notification to everyonefirebase - php - 向所有人发送通知
【发布时间】:2018-10-03 05:19:29
【问题描述】:

我想从 fcm 向我的所有用户发送后台通知。这是我的代码,我面临的唯一问题是我必须输入一个令牌 ID。我需要在不定义令牌值的情况下向所有用户发送通知

这是我的代码:

    <?php
define('API_ACCESS_KEY','Api');
 $fcmUrl = 'https://fcm.googleapis.com/fcm/send';
 $token='token';

     $notification = [
            'title' =>'title',
            'body' => 'body of message.',
            'icon' =>'myIcon', 
            'sound' => 'mySound'
        ];
        $extraNotificationData = ["message" => $notification,"moredata" =>'dd'];

        $fcmNotification = [
            //'registration_ids' => $tokenList, //multple token array
            'to'        => $token, //single token
            'notification' => $notification,
            'data' => $extraNotificationData
        ];

        $headers = [
            'Authorization: key=' . API_ACCESS_KEY,
            'Content-Type: application/json'
        ];


        $ch = curl_init();
        curl_setopt($ch, CURLOPT_URL,$fcmUrl);
        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($fcmNotification));
        $result = curl_exec($ch);
        curl_close($ch);


        echo $result;
?>

它工作正常并成功发送通知,但是如何向所有人发送通知?

【问题讨论】:

    标签: java php android android-notifications firebase-notifications


    【解决方案1】:

    似乎没有向所有设备发送通知的功能。

    但是,使用主题可以解决您的问题。为此,您需要在应用启动期间为所有用户订阅特定主题。

    FirebaseMessaging.getInstance().subscribeToTopic("your_topic");
    

    然后您可以将通知发送到该主题,以便将通知发送给所有用户。

    'to' => '/topics/your_topic', // using topic instead of token
    

    【讨论】:

      【解决方案2】:

      如果我得到你的问题。

      您需要订阅Topic,然后您只需要使用该主题即可。 订阅它的用户将收到通知。

      文档:-

      基于发布/订阅模型,FCM 主题消息传递允许您向已选择加入特定主题的多个设备发送消息。您可以根据需要编写主题消息,FCM 会处理路由并将消息可靠地传递到正确的设备。

      https://firebase.google.com/docs/cloud-messaging/android/topic-messaging

      【讨论】:

        【解决方案3】:

        有两种方法可以向多个用户发送通知。

        1. 创建一个包含所有用户的主题并将推送发送到该主题。 示例:https://firebase.google.com/docs/cloud-messaging/android/send-multiple

        2. 向应用程序发送包 ID 通知。但这甚至会向未经身份验证的用户发送通知。

        【讨论】:

          猜你喜欢
          • 1970-01-01
          • 1970-01-01
          • 2022-07-21
          • 1970-01-01
          • 1970-01-01
          • 2018-08-05
          • 2016-11-09
          • 2020-05-11
          相关资源
          最近更新 更多