php - 如何在 php 中向数组中的所有用户发送推送通知?
问题描述
当我使用 {select token FROM riders} 时,我想这样做,它会选择所有令牌并将推送通知发送给所有令牌,但是当我这样做时,它不会发送任何通知。
<?php
define('DB_HOST', 'localhost');
define('DB_USER', 'root');
define('DB_PASS', '');
define('DB_NAME', 'android_api');
//connecting to database and getting the connection object
$conn = new mysqli(DB_HOST, DB_USER, DB_PASS, DB_NAME);
$stmt = mysqli_query($conn,"SELECT token FROM riders");
$query = mysqli_fetch_array($stmt);
$token = $query['token'];
$apiKey = "AAAAKCR7bo4:APA91bHuIwpxHkR5VxODbqje6b518xbBLWUnVfucuUJNJzqzTYDyww- dwkPtgUj.........aDWTNyfARF0ru16"; //Server Key Legacy
$fcmUrl = 'https://fcm.googleapis.com/fcm/send';
$notification = array('title' =>'iDELIVERY',
'body' => 'A Request Has Been Made');
$notifdata = array('title' =>'test title data',
'body' => 'hello',
'image' => 'path'
);
$fcmNotification = array (
'to' => $token,
'notification' => $notification,
'data' => $notifdata
);
$headers = array( 'Authorization: key='.$apiKey, 'Content-Type: application/json');
$ch = curl_init();
curl_setopt($ch, CURLOPT_URL,$fcmUrl);
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($fcmNotification));
$result = curl_exec($ch);
// curl_close($ch);
?>
解决方案
我正在发送我在我的一个移动应用程序中使用过的工作代码,您应该根据您的数据集获取多个令牌,示例代码如下所示
$stmt = mysqli_query($conn,"SELECT token FROM riders where phone='026'");
//Itrate token and store it in an array based on your dataset
$allTokens = array();
foreach($tokens as $token){
$allTokens[] = $token['token'];
}
// This is a sample KEY
define( 'API_ACCESS_KEY', 'AIdsdyALOdsds7pR01rydsds0dsdqQndsdsOVpPpy4adsds');
// Build your message as an array
$msg = array(
'title' => "Payment Alert",
'message' => "You have received a test payment",
'bigText' => "You have received a test payment",
"subText" => "You have received a test payment",
'summaryText' => 'Alert for payment',
'click_action' => 'FCM_PLUGIN_ACTIVITY',
'vibrate' => 300,
'sound' => 1,
);
$fields = array(
'registration_ids' => $allTokens, // multiple tokens will be available in array
'data' => $msg
);
$headers = array(
'Authorization: key=' . API_ACCESS_KEY,
'Content-Type: application/json'
);
if(!empty($allTokens)){
$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 );
}
推荐阅读
- mysql - 为缺少信息的客户添加银行身份验证
- angular - EsLint 无法识别我的 Web 组件扩展 HTMLElement 接口
- python - 如何生成一个小于或等于另一个矩阵的随机0,1矩阵,限制python中的列之和
- video - 如何计算视频的分辨率?
- arrays - 结构中的 C 数组 - 分段错误
- c# - 在 Unity 中使用按钮重置计时器
- javascript - 无法获得标题的高度
- ios - NumberFormatter 仅在 Swift 中用于带符号的货币
- swift - 如果/否则单击 UIButton
- typescript - 比较 `!==` 时,此条件将始终返回 true