Expo Push Notification with PHP

后端 未结 2 1281
别跟我提以往
别跟我提以往 2021-01-13 21:01

I am trying to send a Push Notification to my react native app using PHP, the below code is sending too all the users that registered their token and it sends plenty Notific

相关标签:
2条回答
  • 2021-01-13 21:12

    Try this

    $key = "ExponentPushToken[0GAEokJazChx21MOxeC1l2]";
    $userId = 'userId from your database';
    $notification = ['title' => $title,'body' => $msg];
      try{
    
          $expo = \ExponentPhpSDK\Expo::normalSetup();
          $expo->notify($userId,$notification);//$userId from database
          $status = 'success';
    }catch(Exception $e){
            $expo->subscribe($userId, $key); //$userId from database
            $expo->notify($userId,$notification);
            $status = 'new subscribtion';
    }
    
      echo $status;
      ?>
    
    0 讨论(0)
  • 2021-01-13 21:20

    Without the expo php sdk it can be done this way.

      <?php
    
        $payload = array(
            'to' => 'ExponentPushToken[xxborxxxxxxxxxx]',
            'sound' => 'default',
            'body' => 'hello',
        );
    
    $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) {
      echo "cURL Error #:" . $err;
    } else {
      echo $response;
    }
    ?>
    
    0 讨论(0)
提交回复
热议问题