[英]Send FCM Push notifcations to specific devices in android app using MySQL query as identification from a PHP script
I want to send FCM push notifications in specific android users only using their token saved in mysql database as identification. 我只想使用保存在mysql数据库中的令牌作为标识在特定的android用户中发送FCM推送通知。 here's my current progress
这是我目前的进度
PHP Script Snippet Code: Report_Status.php (File 1) 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);
}
PHP code for File 2: Push_User_Notification.php 文件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);
}
?>
Problem: 问题:
The page is always stuck in Report_Status.php
every time I ran the script. 每次我运行脚本时,页面始终停留在
Report_Status.php
。 It is supposed to go in Push_User_Notification
and return to Report_Status
once the process is done. 它应该去
Push_User_Notification
并返回Report_Status
一旦这个过程完成。 Am I wrong in the implementation of calling the Push_User_Notification.php
or the receiving parameters to Push_User_Notification.php
? 我错在调用执行
Push_User_Notification.php
或接收参数Push_User_Notification.php
?
PS PS
Here's my full source code of Report_Status.php
in case anyone wants to check it: Report_Status.php 这是我的
Report_Status.php
完整源代码,以防有人要检查它: Report_Status.php
I think the problem you may be having is that you are sending a lot of notifications to several devices in short amount of time. 我认为您可能遇到的问题是,您正在短时间内将大量通知发送到多个设备。 I think it might be being picked up as spaming.
我认为可能是垃圾邮件。 My suggestion is sending one notification to multiple devices.
我的建议是将一个通知发送到多个设备。
Try changing your code in report_status.php to this. 尝试将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);
the idea is that you will collect the user tokens in $User_Token[]
array. 这个想法是,您将在
$User_Token[]
数组中收集用户令牌。 Then you would comma seperate the tokens and send the message once to all the devices that associate to the tokens. 然后,您将逗号分隔标记并将消息一次发送到与该标记关联的所有设备。 FCM allows you to send to multiple tokens in one go.
FCM允许您一次发送多个令牌。
updated 更新
$User_Token
needs to be an array. $User_Token
必须是一个数组。 so remove the implode. 因此移除爆破。 That was my mistake.
那是我的错
Secondly the $message
needs to be in the following format. 其次,
$message
必须采用以下格式。
$message = array(
'title' => 'This is a title.',
'body' => 'Here is a message.'
);
Also another thing to note is that there are 2 types of messages you can send using FCM. 还要注意的另一件事是,可以使用FCM发送两种消息。 Notification Messages or Data Messages.
通知消息或数据消息。 Read more here: https://firebase.google.com/docs/cloud-messaging/concept-options
在此处了解更多信息: https : //firebase.google.com/docs/cloud-messaging/concept-options
I dont know if your app is handling the receipt of messages (i dont know if you have implemented onMessageRecieve
method) so i would probably suggest making a small change to the $fields
array in send_notification
function. 我不知道您的应用程序是否正在处理消息的接收(我不知道您是否已实现
onMessageRecieve
方法),所以我可能建议对send_notification
函数中的$fields
数组进行一些更改。 Adding the notification field allows android to handle notifications automatically if your app is in the background. 如果您的应用程序位于后台,则添加通知字段可使android自动处理通知。 So make sure you app is in the background when testing.
因此,在测试时,请确保您的应用程序在后台。 https://firebase.google.com/docs/cloud-messaging/android/receive
https://firebase.google.com/docs/cloud-messaging/android/receive
$fields = array(
'registration_ids' => $tokens,
'data' => $message,
'notification' => $message
);
So try the code below. 因此,请尝试下面的代码。 I have tried and tested.
我已经尝试和测试。 It works for me.
这个对我有用。 If it does not work.
如果不起作用。 In
send_notification
function echo $result to get the error message. 在
send_notification
函数中,echo $ result以获取错误消息。 echo $result = curl_exec($ch);
Then we can work from there to see what is wrong. 然后,我们可以从那里开始工作,看看有什么问题。 You can see what the errors mean here: https://firebase.google.com/docs/cloud-messaging/http-server-ref#error-codes
您可以在此处查看错误的含义: 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);
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.