Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Expo Push Notification with PHP

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 Notification at once though the token is for specific device but it keep pushing the notification to all

$key = "ExponentPushToken[0GAEokJazChx21MOxeC1l2]";
$title = "title";
$interestDetails = ['https://exp.host/--/api/v2/push/send',$key];

  try{

      $expo = \ExponentPhpSDK\Expo::normalSetup();

  // Subscribe the recipient to the server
      $expo->subscribe($interestDetails[0], $interestDetails[1]);

  // Build the notification data

    $notification = ['title' => $title,'body' => $msg];

  // Notify an interest with a notification
   $expo->notify($notification);

  $status = 'success';
}catch(Exception $e){



}


   ?>

I tried changing my code as follow

<?php

$key = "ExponentPushToken[0GAEokJazChx21MOxeC1l2]";
$title = "title";


  try{

      $expo = \ExponentPhpSDK\Expo::normalSetup();



  // Build the notification data

  $notification = ['to' => $key,'title' => $title,'body' => $msg];

  // Notify an interest with a notification
 $expo->notify('https://exp.host/--/api/v2/push/send',$notification);

  $status = 'success';
}catch(Exception $e){
    echo $e;
}




  echo $status;


  ?>

It did sent to a specific user but still it keeps sending plenty notification at once?

like image 958
Basil Satti Avatar asked Dec 10 '22 04:12

Basil Satti


2 Answers

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;
}
?>
like image 124
Alfred Alfizo Mosima Avatar answered Dec 27 '22 01:12

Alfred Alfizo Mosima


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;
  ?>
like image 42
Ahmed Imam Avatar answered Dec 27 '22 01:12

Ahmed Imam