0

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.

AL.
  • 36,815
  • 10
  • 142
  • 281
Lenin
  • 17
  • 1
  • 4

1 Answers1

2

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.

Community
  • 1
  • 1
AL.
  • 36,815
  • 10
  • 142
  • 281
  • can you just php script & android code of topic messaging. – Lenin Oct 15 '16 at 16:43
  • @Lenin Can you elaborate on the scenario? Do you have your own App Server? – AL. Oct 15 '16 at 22:26
  • Yes, I have own app server and i also sent some push notifications using above code for one device and it is successful and now coming to present scenario I need php code that can send push notification to many devices. I read above documentation you have sent it and i am not able to frame the code.Please give me a complete code on " Topic Messaging" . Sorry for my bad English . – Lenin Oct 16 '16 at 05:31
  • @Lenin Pretty sure you can find Code examples by just doig a quick Google seach. Check this https://github.com/Paragraph1/php-fcm. – AL. Oct 16 '16 at 05:44