将firebase推送通知发送给多个不工作的用户-php

时间:2018-01-03 11:17:25

标签: php android firebase push-notification firebase-cloud-messaging

我正在尝试使用以下push脚本将notification PHP发送到多个设备。当我将notification发送到一个设备时,它正在运行。但是不能正常工作多个设备。

protected function pushNotification($reg_id){

      define( 'API_ACCESS_KEY', 'AAAAgUh9ZSQ:APA91bEjQ2nO3Po9_-T_kNey4q3Eizcoc3gLsD04Bjh0KYXQu_33O2gpLBvmMV_DtbE3xP2evZ1Dwki6WRqgqu8ZYhOG2CYi-6Wpxq2CLy3SQPb1nD6QkzwNN0249pt-H2mAgB8L-sjm' );
      $registrationIds = array($reg_id);          
          $msg = array
                  (
                     'body'     => 'Hey! You have a new order in queue',
                     'title'    => 'New Order Receieved',
                     'icon' => 'myicon',/*Default Icon*/
                     'sound' => 'mySound'/*Default sound*/
                  );
        $fields = array
                (
                    'to'        => $registrationIds,
                    '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 );
                curl_close( $ch );
                error_log("Result ".$result) ;

    }

1 个答案:

答案 0 :(得分:0)

问题出在您在to中使用$fieldsto仅在拥有单独的String FCM ID时使用。如果要将通知发送到FCM ID数组(您的情况),则必须使用registration_ids代替to。请注意,此操作一次只能传递一个小于1000的数组。 有关更多信息,请https://firebase.google.com/docs/cloud-messaging/http-server-ref