我想从 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/