将Firesbase通知发送给多个用户 [英] Sending Firesbase Notification to Multiple Users

查看:76
本文介绍了将Firesbase通知发送给多个用户的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在尝试通过Firebase发送通知,但是它不起作用.我只想将存储在MySQL中的Firebase ID发送给所有注册用户.

I am trying to send notifications through Firebase, but it is not working. I just want to send to all my registered users with their Firebase ID stored in 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);

 ?>

它工作正常,但仅用于发送给单个用户.我需要发送给多个用户.

It's working fine, but it's only for sending to a single user. What I need is for sending to multiple users.

推荐答案

您可以通过三种方式将其发送到多个设备.使用 registration_ids 参数代替 to .请注意,每个参数最多可以有1000个注册令牌.如果您有超过1000个注册令牌,则必须进行批处理请求(每个请求1000个).

There are three ways you can send to multiple devices. Make use of the registration_ids parameter instead of to. Do note that there is a maximum number of 1000 registration tokens per this parameter. If you have more than 1000 registration tokens, then you must do batch requests (1000 per each request).

(并且根据我在可能重复的帖子中的回答)

取决于您的用例,您可以使用主题消息设备组消息.

Depending also on your use case, you may use either Topic Messaging or Device Group Messaging.

主题消息

Firebase Cloud Messaging(FCM)主题消息传递,您可以将消息发送到已选择加入特定主题的多个设备.基于发布/订阅模型,主题消息支持每个应用程序无限制的订阅.您可以根据需要编写主题消息,Firebase可以处理消息路由并将消息可靠地传递到正确的设备.

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.

例如,本地天气预报应用的用户可以选择恶劣天气警报"主题并接收风暴威胁特定区域的通知.体育应用程序的用户可以为自己喜欢的球队订阅实时比赛得分的自动更新.开发人员可以选择与正则表达式匹配的任何主题名称:"/topics/[a-zA-Z0-9 -_.〜%] +" .

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-_.~%]+".


设备组消息传递

使用设备组消息传递,应用程序服务器可以将一条消息发送到在属于该组的设备上运行的应用程序的多个实例.通常,组"指的是一组属于单个用户的不同设备.组中的所有设备共享一个公共的通知密钥,这是FCM用来向组中的所有设备散发消息的令牌.

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.

设备组消息传递使组中的每个应用程序实例都能反映最新消息传递状态.除了将消息下游发送到通知键之外,您还可以使设备将上游消息发送到设备组.您可以将设备组消息传递与XMPP或HTTP连接服务器一起使用.发送到iOS设备时,数据有效载荷的限制为2KB,其他平台为4KB.

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.

notification_key 允许的最大成员数量为20.

The maximum number of members allowed for a notification_key is 20.


有关更多详细信息,您可以查看发送到多个设备在FCM 文档中.

这篇关于将Firesbase通知发送给多个用户的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆