首页 > 解决方案 > 使用 MySQL 查询作为 PHP 脚本的标识,将 FCM 推送通知发送到 android 应用程序中的特定设备

问题描述

我想仅使用保存在 mysql 数据库中的令牌作为标识在特定的 android 用户中发送 FCM 推送通知。这是我目前的进展

PHP 脚本片段代码: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.phpPush_User_Notification.php

附言

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

标签: phpandroidmysqlfirebasefirebase-cloud-messaging

解决方案


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

尝试将 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[]数组中收集用户令牌。然后,您将用逗号分隔令牌并将消息一次发送到与令牌关联的所有设备。FCM 允许您一次性发送到多个令牌。

更新

$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);

推荐阅读