0

i am using following code to send push notifications to one Android device

function send_notification($message, $token){
    define( 'API_ACCESS_KEY', 'mykey');
     $msg = array
          (
            'body'  =>$message,
            'title' => 'You have a new message ',

          );
    $fields = array
            (
                'to'        => $token, 
                'notification'  => $msg
            );

    $headers = array
            (
                'Authorization: key=' . API_ACCESS_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_SSL_VERIFYPEER, false );
        curl_setopt( $ch,CURLOPT_POSTFIELDS, json_encode( $fields ) );
        $result = curl_exec($ch );
        echo $result;
        curl_close( $ch );
}

i am successfully sending it to one device what if i want to send it to multiple device id's where do i have to loop and what format data would be . Please help

2 Answers 2

2

Docs here show how to specify registration_ids for recipients of multicast messages. Replace your $fields['to'] with a $fields['registration_ids'] which will be an array of strings containing your recipients' tokens.

Sign up to request clarification or add additional context in comments.

3 Comments

should it be like this 'to' => [2,3,4,5]
No @Sikander, it should rather be like this: $fields = array( 'registration_ids' => [2,3,4,5], ... );
how ? i mean please clarify
0

try this one :

for($i=0;$i<count($TokenArray);$i++){
  send_notification($message, $TokenArray[$i]);
}

1 Comment

would it not add 4 different enteries in firebase admin panel

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.