我正在尝试使用PHP向我的react native应用发送推送通知, 下面的代码会发送太多的所有注册其令牌的用户,并且尽管令牌是针对特定设备的,但它会立即发送大量通知,但它会继续将通知推送给所有人
$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){
}
?>
我尝试如下更改代码
<?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;
?>
它确实已发送给特定用户,但仍然不断发送大量通知吗?
答案 0 :(得分:2)
尝试一下
$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;
?>
答案 1 :(得分:0)
没有expo php sdk,可以通过这种方式完成。
<?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;
}
?>