Attempting to send push notification from my laravel server to expo not working

Please provide the following:

  1. SDK Version:37
  2. Platforms(Android/iOS/web/all):Expo mobile app android /ios

This is the api error that i recieve when i make a post request to expo api:

{"errors":[{"code":"API_ERROR","message":"child \"to\" fails because [\"to\" is required], \"value\" must be an
array."}]}

This is the request i am trying to make

$payload=json_encode([
       'to' => "ExponentPushToken[xxxxxxxxxxxxxxxx]",
       'title'=>'Hello',
       'body'=>'hello World',


   ]);

     $response = Http::withHeaders([
         'Host'=>'exp.host',
         'accept'=>'application/json',
         'Content-Type' => 'application/json',
         'accept-encoding'=>'grizp,deflate',
     ])->post('https://exp.host/--/api/v2/push/send', [
         'debug' => TRUE,
         'body' => $payload,
     ]);

but when i make the api call from my expo app using javascript its works fine

  sendPushNotification = async () => {
    const message = {
      to: this.state.expoPushToken,
      sound: 'default',
      title: 'Hello',
      body: 'Hello World',
      data: { data: 'goes here' },
      _displayInForeground: true,
    };

    const response = await fetch('https://exp.host/--/api/v2/push/send', {
      method: 'POST',
      headers: {
        Accept: 'application/json',
        'Accept-encoding': 'gzip, deflate',
        'Content-Type': 'application/json',
      },
      body: JSON.stringify(message),
    });
  };

FINALLY FIXED IT ! https://stackoverflow.com/a/55661278/13231648 this dude s answer solved it for me ,any way i changed my laravel code to

      $payload=array(

              'to' => "ExponentPushToken[xxxxxxxxxxxxxxxxx]",
              'title'=>'hello',
              'body'=>'hello world',


      );
        $curl = curl_init();

        curl_setopt_array($curl, array(
            CURLOPT_URL => "https://exp.host/--/api/v2/push/send",
            CURLOPT_RETURNTRANSFER => true,
            CURLOPT_ENCODING => "",
            CURLOPT_MAXREDIRS => 10,
            CURLOPT_TIMEOUT => 30,
            CURLOPT_HTTP_VERSION => CURL_HTTP_VERSION_1_1,
            CURLOPT_CUSTOMREQUEST => "POST",
            CURLOPT_POSTFIELDS => json_encode($payload),
            CURLOPT_HTTPHEADER => array(
                "Accept: application/json",
                "Accept-Encoding: gzip, deflate",
                "Content-Type: application/json",
                "cache-control: no-cache",
                "host: exp.host"
            ),
        ));

        $response = curl_exec($curl);

        $err = curl_error($curl);

        curl_close($curl);

        if ($err) {
            return $err;
        } else {
            return $response;
        }

This topic was automatically closed 20 days after the last reply. New replies are no longer allowed.