How to send push notifications to multiple devices using php script using FCM?

前端 未结 5 1401
-上瘾入骨i
-上瘾入骨i 2021-01-12 05:55

I\'m new to push notifications using FCM from php to Android devices. From android side I have generated FCM reg_id & send it over php script & store into mysql data

5条回答
  •  别那么骄傲
    2021-01-12 06:22

    To send firebase notifications to multiple users at once

    add multiple firebase tokens to an array.

    $token_ids = array('token1', 'token2');
    

    Pass the tokens to the below shown function

    Using below shown function you can also send images with your notification

    if you don't want to send any image then just pass empty string

    sendFirebaseNotification($token_ids ,"notification title", "message", "image_url");
    

    I use this function

    function sendFirebaseNotification($fb_key_array, $title, $message, $imageURL){
        $authorization_key = "your_auth_key";
    
        $finalPostArray = array('registration_ids' => $fb_key_array,
                                'notification' => array('body' => $message,
                                                        'title' => $title,
                                                        "image"=> $imageURL),
                                "data"=> array("click_action"=> "FLUTTER_NOTIFICATION_CLICK",
                                                "sound"=> "default", 
                                                "status"=> "done")); 
        
        $ch = curl_init();
        curl_setopt($ch, CURLOPT_URL,"https://fcm.googleapis.com/fcm/send");
        curl_setopt($ch, CURLOPT_POST, 1);
        curl_setopt($ch, CURLOPT_POSTFIELDS,json_encode($finalPostArray));  //Post Fields
        curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
        curl_setopt($ch, CURLOPT_HTTPHEADER, array('Content-Type: application/json', 'Authorization: key='.$authorization_key));
        $server_output = curl_exec ($ch);
        curl_close ($ch);
        //echo $server_output; 
    }
    

提交回复
热议问题