douxing8939 2016-10-12 15:15
浏览 79

向多个用户发送Firebase通知

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.

EDIT:

Here is the code below.

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

  • 写回答

1条回答 默认 最新

  • duanhuan7750 2016-10-13 08:53
    关注

    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).

    (And as from my answer in the possible duplicate post)

    Depending also on your use case, you may use either Topic Messaging or 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.


    For more details, you can check out the Sending to Multiple Devices in FCM docs.

    评论

报告相同问题?

悬赏问题

  • ¥15 c程序不知道为什么得不到结果
  • ¥40 复杂的限制性的商函数处理
  • ¥15 程序不包含适用于入口点的静态Main方法
  • ¥15 素材场景中光线烘焙后灯光失效
  • ¥15 请教一下各位,为什么我这个没有实现模拟点击
  • ¥15 执行 virtuoso 命令后,界面没有,cadence 启动不起来
  • ¥50 comfyui下连接animatediff节点生成视频质量非常差的原因
  • ¥20 有关区间dp的问题求解
  • ¥15 多电路系统共用电源的串扰问题
  • ¥15 slam rangenet++配置