如何使用Firebase推送通知PHP MYSQL向所有令牌发送消息。

huangapple go评论69阅读模式
英文:

How to Send Message use Firebase Push Notification PHP MYSQL to All Tokens

问题

我想以这样的方式来做,当我使用 {select token FROM devices} 时,它会选择所有令牌并向它们全部发送推送通知,但当我这样做时,通知会发送两次,但只发送给数据库中的第一个数据令牌。

<form method="post" action="send_notification.php">
标题<input type="text" name="title">
消息<input type="text" name="message">
<!--图标路径<input type="text" name="icon">-->
令牌<input type="text" name="token">
<input type="submit" name="submit" value="发送通知">
</form>

<?php

function sendNotification(){
include("../config/con.php");
$query = mysqli_query($conn,"SELECT token FROM devices");
$query_res = mysqli_fetch_array($query);

foreach ($query_res as $val) {
    $registrationIds[]=$val;
  }
    $url ="https://fcm.googleapis.com/fcm/send";

    $fields=array(
        // "to"=>$_REQUEST['token'],
        "registration_ids"=>$registrationIds,
        "notification"=>array(
            "body"=>$_REQUEST['message'],
            "title"=>$_REQUEST['title'],
            // "icon"=>$_REQUEST['icon'],
            "click_action"=>"https://google.com/"
        )
    );

    $headers=array(
        'Authorization: key=key',
        'Content-Type:application/json'
    );

    $ch=curl_init();
    curl_setopt($ch,CURLOPT_URL,$url);
    curl_setopt($ch,CURLOPT_POST,true);
    curl_setopt($ch,CURLOPT_HTTPHEADER,$headers);
    curl_setopt($ch,CURLOPT_RETURNTRANSFER,true);
    curl_setopt($ch,CURLOPT_POSTFIELDS,json_encode($fields));
    $result=curl_exec($ch);
    print_r($result);
    curl_close($ch);
}
if (isset($_POST['submit'])){
    sendNotification();
}

英文:

I want to do it in such a way when I use {select token FROM devices} it selects all tokens and sends push notifications to all of them but when I do it notifications are sent 2x but to the first data token on database.

<form method="post" action="send_notification.php">
Title<input type="text" name="title">
Message<input type="text" name="message">
<!--Icon path<input type="text" name="icon">-->
Token<input type="text" name="token">
<input type="submit" name="submit" value="Send notification">
</form>
<?php
function sendNotification(){
include("../config/con.php");
$query = mysqli_query($conn,"SELECT token FROM devices");
$query_res = mysqli_fetch_array($query);
foreach ($query_res as $val) {
$registrationIds[]=$val;
}
$url ="https://fcm.googleapis.com/fcm/send";
$fields=array(
// "to"=>$_REQUEST['token'],
"registration_ids"=>$registrationIds,
"notification"=>array(
"body"=>$_REQUEST['message'],
"title"=>$_REQUEST['title'],
// "icon"=>$_REQUEST['icon'],
"click_action"=>"https://google.com/"
)
);
$headers=array(
'Authorization: key=key',
'Content-Type:application/json'
);
$ch=curl_init();
curl_setopt($ch,CURLOPT_URL,$url);
curl_setopt($ch,CURLOPT_POST,true);
curl_setopt($ch,CURLOPT_HTTPHEADER,$headers);
curl_setopt($ch,CURLOPT_RETURNTRANSFER,true);
curl_setopt($ch,CURLOPT_POSTFIELDS,json_encode($fields));
$result=curl_exec($ch);
print_r($result);
curl_close($ch);
}
if (isset($_POST['submit'])){
sendNotification();
}

答案1

得分: 0

你可以尝试以下方法来解决这个问题:

通过循环遍历结果集并检索所有令牌。以下是代码的更新片段:

// 从数据库检索所有令牌
$query_res = mysqli_query($conn, "SELECT token FROM devices");
$registrationIds = [];

// 遍历每一行并收集令牌
while ($row = mysqli_fetch_assoc($query_res)) {
    $registrationIds[] = $row['token'];
}

在修订后的代码片段中,我们使用了 mysqli_fetch_assoc() 来将每一行作为关联数组提取出来。通过使用 while 循环,我们可以遍历结果集中的所有行,并从每一行中提取令牌值。这些令牌值随后添加到 $registrationIds 数组中。

这种方法确保您将数据库中的所有令牌存储在 $registrationIds 数组中。因此,推送通知将被发送到每个令牌,而不会出现重复。

如果我有错误的地方,请纠正我。

英文:

you can solve it by trying this method:

Looping through the result set and retrieving all the tokens. Here's an updated snippet of the code:

// Retrieve all tokens from the database
$query_res = mysqli_query($conn, "SELECT token FROM devices");
$registrationIds = [];
// Iterate over each row and collect the tokens
while ($row = mysqli_fetch_assoc($query_res)) {
$registrationIds[] = $row['token'];
}

In the revised code snippet, we have used mysqli_fetch_assoc() to fetch each row as an associative array. By employing a while loop, we can iterate over all the rows in the result set and extract the token value from each row. These token values are then added to the $registrationIds array.

This approach ensures that you have all the tokens from the database stored in the $registrationIds array. Consequently, the push notification will be sent to each token without any duplication.

Correct me if im wrong

huangapple
  • 本文由 发表于 2023年6月12日 13:20:52
  • 转载请务必保留本文链接:https://go.coder-hub.com/76453804.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定