我尝试通过 Firebase 发送通知,但不起作用。我只想将 Firebase ID 发送给所有注册用户,并将其存储在 MySQL 中。
编辑:
下面是代码。
<?php $message="test message"; $title="Test Title";
$path_to_fcm='https://fcm.googleapis.com/fcm/send';
$server_key="AIzaSyxxxxxxxxxxxxxxxxXxxxsk";
$con = mysqli_connect('localhost','username','passs','db');
$email ="hii"; $sql = "SELECT * FROM users WHERE email = '$email'";
$res = mysqli_fetch_array(mysqli_query($con,$sql)); $key=
$res['token']; $headers=array(
'Authorization:key='.$server_key,
'Content-Type:application/json'
); $fields=array('to'=>$key,'notification'=>array('title'=>$title,'body'=>$message));
$payload=json_encode($fields); $curl_session=curl_init(); curl_setopt($curl_session,CURLOPT_URL,$path_to_fcm);
curl_setopt($curl_session,CURLOPT_POST,true);
curl_setopt($curl_session,CURLOPT_HTTPHEADER,$headers);
curl_setopt($curl_session,CURLOPT_RETURNTRANSFER,true);
curl_setopt($curl_session,CURLOPT_SSL_VERIFYPEER,false);
curl_setopt($curl_session,CURLOPT_IPRESOLVE,CURL_IPRESOLVE_V4);
curl_setopt($curl_session,CURLOPT_POSTFIELDS,$payload);
$result=curl_exec($curl_session); curl_close($curl_session);
mysqli_close($con);
?>
它工作正常,但它只能发送给单个用户。我需要的是发送给多个用户。
最佳答案
您可以通过三种方式发送到多个设备。使用 registration_ids
参数而不是 to
。请注意,每个此参数最多有 1000 个注册 token 。如果您有超过 1000 个注册 token ,则必须执行批量请求(每个请求 1000 个)。
(从我在可能重复的帖子中的回答)
还取决于您的用例,您可以使用 Topic Messaging或Device Group Messaging .
<小时/>Topic Messaging
Firebase Cloud Messaging (FCM) topic messaging allows you to send a message to multiple devices that have opted in to a particular topic. Based on the publish/subscribe model, topic messaging supports unlimited subscriptions for each app. You compose topic messages as needed, and Firebase handles message routing and delivering the message reliably to the right devices.
For example, users of a local weather forecasting app could opt in to a "severe weather alerts" topic and receive notifications of storms threatening specified areas. Users of a sports app could subscribe to automatic updates in live game scores for their favorite teams. Developers can choose any topic name that matches the regular expression:
"/topics/[a-zA-Z0-9-_.~%]+"
.
<小时/>Device Group Messaging
With device group messaging, app servers can send a single message to multiple instances of an app running on devices belonging to a group. Typically, "group" refers a set of different devices that belong to a single user. All devices in a group share a common notification key, which is the token that FCM uses to fan out messages to all devices in the group.
Device group messaging makes it possible for every app instance in a group to reflect the latest messaging state. In addition to sending messages downstream to a notification key, you can enable devices to send upstream messages to a device group. You can use device group messaging with either the XMPP or HTTP connection server. The limit on data payload is 2KB when sending to iOS devices, and 4KB for other platforms.
The maximum number of members allowed for a
notification_key
is 20.
更多详情,您可以查看Sending to Multiple Devices in FCM文档。
关于php - 向多个用户发送 Firebase 通知,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/40002248/