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

前端 未结 5 1435
-上瘾入骨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:36

    $notification_data = $this->common->get_all_record('table name',array()); //get all id from table
    
                if($notification_data != NULL){
                    foreach ($notification_data as $notification_data_row) {
                        $registrationIds = $notification_data_row['token'];
                    #prep the bundle
                        $msg = array
                            (
                            'body'  => 'body msg',
                            'title' => 'title',
                            'icon'  => 'myicon',/*Default Icon*/
                            'sound' => 'mySound'/*Default sound*/
                            );
                        $fields = array
                            (
                            'to'            => $registrationIds,
                            'notification'  => $msg
                            );
                        $headers = array
                            (
                            'Authorization: key=' . "your key",
                            'Content-Type: application/json'
                            );
                    #Send Reponse To FireBase Server
                        $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 "
    ";print_r($result);exit;
                        curl_close ( $ch );
                    }
                }
    

提交回复
热议问题