java - firebase - php - 向每个人发送通知

标签 java php android android-notifications firebase-notifications

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

这是我的代码:

    <?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;
?>

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

最佳答案

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

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

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

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

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

关于java - firebase - php - 向每个人发送通知,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/52620137/

相关文章:

java - 增量超时;重试后设置总是成功

java - 在 Clojure 命名空间中排除 java.lang.*

html - php mail 添加随机空格和 float 元素

java - 启动一个基于字符串的 Intent

android - 多次调用 startforeground 时通知 remoteview 重复

java - Android 图像大小最佳实践

java - 无法将简单的 PNG 下载脚本从 PHP 转换为 Java

java - 使用 UNION (Jaybird) 时,GetColumName(i) 返回 ""

php - jQuery如何将类添加到具有特定类名的第三级子div的父div

javascript - 如何在不重新加载页面的情况下获取 PHP 文件中的 javascript 数据