我只想使用保存在mysql数据库中的令牌作为标识在特定的android用户中发送FCM推送通知。这是我目前的进度
PHP脚本代码段代码:Report_Status.php(文件1)
is.map(i -> i + 1).sum();
文件2的PHP代码:Push_User_Notification.php
//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);
}
问题:
每次我运行页面时,页面始终停留在<?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
?
PS
这是我Push_User_Notification.php
的完整源代码,以防万一有人想检查它:Report_Status.php
答案 0 :(得分:1)
我认为您可能遇到的问题是,您正在短时间内将大量通知发送到多个设备。我认为可能是垃圾邮件。我的建议是将一个通知发送到多个设备。
尝试将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
方法),所以我可能建议对{{ 1}}函数。如果您的应用程序位于后台,则添加通知字段可使android自动处理通知。因此,在测试时,请确保您的应用程序在后台。 https://firebase.google.com/docs/cloud-messaging/android/receive
$fields
因此,请尝试以下代码。我已经尝试和测试。这个对我有用。如果不起作用。在send_notification
函数中,echo $ result以获取错误消息。 $fields = array(
'registration_ids' => $tokens,
'data' => $message,
'notification' => $message
);
然后我们可以从那里开始工作,看看有什么问题。您可以在此处查看错误的含义:https://firebase.google.com/docs/cloud-messaging/http-server-ref#error-codes
send_notification