php - 使用 MySQL 查询作为 PHP 脚本的标识,将 FCM 推送通知发送到 Android 应用程序中的特定设备

标签 php android mysql firebase firebase-cloud-messaging

我想仅使用保存在 mysql 数据库中的 token 作为标识来向特定 Android 用户发送 FCM 推送通知。这是我目前的进度

PHP 脚本 fragment 代码:Report_Status.php(文件 1)

//Gets the token of every user and sends it to Push_User_Notification.php 
while ($User_Row = mysqli_fetch_array($Retrieve_User, MYSQLI_ASSOC)){
            $User_Token = $User_Row['User_Token'];
            include "../Android_Scripts/Notifications/Push_User_Notification.php";
            $message = "Your Report has been approved! Please wait for the fire fighters to respond!";
            send_notification($User_Token, $message);
        }

文件 2 的 PHP 代码:Push_User_Notification.php

<?php  //Send FCM push notifications process
include_once("../../System_Connector.php");

function send_notification ($tokens, $message)
{
    $url = 'https://fcm.googleapis.com/fcm/send';
    $fields = array(
         'registration_ids' => $tokens,
         'data' => $message
        );
    $headers = array(
        'Authorization:key = API_ACCESS_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_SSL_VERIFYHOST, 0);  
   curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, false);
   curl_setopt($ch, CURLOPT_POSTFIELDS, json_encode($fields));
   $result = curl_exec($ch);           
   if ($result === FALSE) {
       die('Curl failed: ' . curl_error($ch));
   }
   curl_close($ch);
}

?>

问题:

每次我运行时,页面总是卡在Report_Status.php中 脚本。一旦该过程完成,它应该进入 Push_User_Notification 并返回到 Report_Status。我在执行调用时错了吗 Push_User_Notification.php 或接收参数 Push_User_Notification.php

附注

这是我的 Report_Status.php 的完整源代码,以防有人想检查它:Report_Status.php

最佳答案

我认为您可能遇到的问题是您在短时间内向多个设备发送了大量通知。我认为它可能会被认为是垃圾邮件。我的建议是将一个通知发送到多个设备。

尝试将report_status.php中的代码更改为此。

include "../Android_Scripts/Notifications/Push_User_Notification.php";
$message = "Your Report has been approved! Please wait for the fire fighters to respond!";
while ($User_Row = mysqli_fetch_array($Retrieve_User, MYSQLI_ASSOC)){
    $User_Token[] = $User_Row['User_Token'];
}
$tokens = implode(",", $User_Token);
send_notification($tokens, $message);

这个想法是您将在 $User_Token[] 中收集用户 token 大批。然后,您可以用逗号分隔 token ,并将消息发送到与 token 关联的所有设备一次。 FCM 允许您一次性发送到多个 token 。

已更新

$User_Token需要是一个数组。所以消除内爆。那是我的错误。

其次是$message需要采用以下格式。

$message = array(
    'title' => 'This is a title.',
    'body' => 'Here is a message.'
);

另外需要注意的是,您可以使用 FCM 发送两种类型的消息。通知消息或数据消息。在这里阅读更多信息:https://firebase.google.com/docs/cloud-messaging/concept-options

我不知道您的应用程序是否正在处理消息的接收(我不知道您是否已实现 onMessageRecieve 方法),因此我可能建议对 $fields 进行一些小更改数组位于 send_notification功能。如果您的应用程序在后台运行,添加通知字段可以让 Android 自动处理通知。因此,测试时请确保您的应用程序处于后台。 https://firebase.google.com/docs/cloud-messaging/android/receive

$fields = array(
    'registration_ids' => $tokens,
    'data' => $message,
    'notification' => $message
);

所以尝试下面的代码。我已经尝试过并测试过。这个对我有用。如果不起作用。在 send_notification函数 echo $result 来获取错误消息。 echo $result = curl_exec($ch);然后我们可以从那里开始工作,看看哪里出了问题。您可以在此处查看错误的含义: https://firebase.google.com/docs/cloud-messaging/http-server-ref#error-codes

include "../Android_Scripts/Notifications/Push_User_Notification.php";
$message = array(
    'title' => 'Report Approved',
    'body' => 'Your Report has been approved! Please wait for the fire fighters to respond!'
);
while ($User_Row = mysqli_fetch_array($Retrieve_User, MYSQLI_ASSOC)){
    $User_Token[] = $User_Row['User_Token'];
}
send_notification($User_Token, $message);

关于php - 使用 MySQL 查询作为 PHP 脚本的标识,将 FCM 推送通知发送到 Android 应用程序中的特定设备,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/53255158/

相关文章:

mysql - 对于不存在的数据返回空行

php - session VS 温度。 cookies

php - 如何使用 php 和 mysql 循环 data[] 系列

android - CrashlyticsInitProvider 跳过初始化

java - Android apk差异化

mysql - SQL : update table from another grouped table

php - MySQL 查询来选择特定的 DATETIME 值?

php - 无法在 PHP 中为 Cognito 生成正确的 PASSWORD_CLAIM_SIGNATURE

android - 响应式网站在智能手机上显示较大的右侧边距

mysql - 如何检查事件调度程序状态mysql