1

I just need to send push notifications to a handful of devices. I have their device tokens in an array. Do I need to create topics or groups for this?

I can send push notifications individually using a for loop, but is there a better way to do this?

EDIT: Please do not mark this question as duplicate without understanding. The question

FCM (Firebase Cloud Messaging) Send to multiple devices

has answers to use FCM Token and groups, which I have clearly stated that is not my requirement. As far as 'registration_ids' mentioned by other question is concerned its not mentioned anywhere in the official docs , if it is mentioned then please point it out as a valid answer.

Community
  • 1
  • 1
Utsav Gupta
  • 3,785
  • 5
  • 30
  • 52

3 Answers3

6

Use "registration_ids" instead of "to".

var message = { 
    registration_ids:['id1','id2','id3'],
    notification: {
        title: 'Hello There...!', 
        body: 'this is test notification' 
    }
};

Hope It Works..!!!

Nitin Bhanderi
  • 334
  • 2
  • 9
0

I know this has been answered and not a very latest question.

Sure, you can construct the string array and send it like this as mentioned in the google documentation find here.

HTTP POST request

For example, to add a device with the registration ID 51 to appUser-Chris, you would send this request:

{
   "operation": "add",
   "notification_key_name": "appUser-Chris",
   "notification_key": "APA91bGHXQBB...9QgnYOEURwm0I3lmyqzk2TXQ",
   "registration_ids": ["51"] //you can insert something more of your strings here.
}

Response format

A successful request to either add or remove a device returns a notification_key like the following:

{
   "notification_key": "APA91bGHXQBB...9QgnYOEURwm0I3lmyqzk2TXQ"
}
Coder Absolute
  • 5,417
  • 5
  • 26
  • 41
0
<?php
// Your code here!
class FCM
{
    private $key;

    function __construct($api_key)
    {
        $this->key = $api_key;
    }

    /**
     * Sending Push Notification
     */
    public function send_notification($registatoin_ids, $notification)
    {
        $fields = array ( 'to' => $registatoin_ids,'notification' => $notification); //'restricted_package_name' => "com.apps.firebasenotificationphp");
        
        $headers = array (
                'Authorization: key=' . $this->key,
                'Content-Type: application/json'
        );

       $ch = curl_init ();
       curl_setopt ( $ch, CURLOPT_URL, "https://fcm.googleapis.com/fcm/send" );
       curl_setopt ( $ch, CURLOPT_POST, true );
       curl_setopt ( $ch, CURLOPT_HTTPHEADER, $headers );
       curl_setopt ( $ch, CURLOPT_RETURNTRANSFER, true );
       curl_setopt ( $ch, CURLOPT_POSTFIELDS, json_encode($fields) );
       return curl_exec ( $ch );
       curl_close ( $ch );
    }
}

$api_key = "";
$device_id = "";
$fcm = new FCM($api_key);
$arrNotification = array ("body" => "hii","title" => "hello",'image' => "https://www.gstatic.com/devrel-devsite/prod/vb1c70bbe2f68b543db3deb1075af42e62f8f21e5fc703b8398dc6b9860f1711f/firebase/images/lockup.png",'link' => "",'sound' => "default");

echo $fcm->send_notification($device_id,$arrNotification);
?>