Send FCM Push notifcations to specific devices in android app using MySQL query as identification from a PHP...











up vote
0
down vote

favorite












I want to send FCM push notifications in specific android users only using their token saved in mysql database as identification. here's my current progress



PHP Script Snippet Code: Report_Status.php (File 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



<?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. It is supposed to go in Push_User_Notification and return to Report_Status once the process is done. Am I wrong in the implementation of calling the
Push_User_Notification.php or the receiving parameters to
Push_User_Notification.php?



P.S.



Here's my full source code of Report_Status.php in case anyone wants to check it: Report_Status.php










share|improve this question
























  • What do you mean by stuck. Is it supposed to go to a different page.
    – Mohammad C
    Nov 12 at 2:11










  • @MohammadC yes, but it does not going to a different page.
    – Denzell
    Nov 12 at 2:13










  • Do you get any error messages on the page.
    – Mohammad C
    Nov 12 at 2:18










  • Also your $message should look something like this $message = array('title' => 'This is a title.', 'body' => 'Here is a message.');
    – Mohammad C
    Nov 12 at 2:20












  • @MohammadC none. I tried before my code and it's working perfectly. the only problem before is my SQL query, instead of sending it to specific device/s it sends to all users who had a token.
    – Denzell
    Nov 12 at 2:41















up vote
0
down vote

favorite












I want to send FCM push notifications in specific android users only using their token saved in mysql database as identification. here's my current progress



PHP Script Snippet Code: Report_Status.php (File 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



<?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. It is supposed to go in Push_User_Notification and return to Report_Status once the process is done. Am I wrong in the implementation of calling the
Push_User_Notification.php or the receiving parameters to
Push_User_Notification.php?



P.S.



Here's my full source code of Report_Status.php in case anyone wants to check it: Report_Status.php










share|improve this question
























  • What do you mean by stuck. Is it supposed to go to a different page.
    – Mohammad C
    Nov 12 at 2:11










  • @MohammadC yes, but it does not going to a different page.
    – Denzell
    Nov 12 at 2:13










  • Do you get any error messages on the page.
    – Mohammad C
    Nov 12 at 2:18










  • Also your $message should look something like this $message = array('title' => 'This is a title.', 'body' => 'Here is a message.');
    – Mohammad C
    Nov 12 at 2:20












  • @MohammadC none. I tried before my code and it's working perfectly. the only problem before is my SQL query, instead of sending it to specific device/s it sends to all users who had a token.
    – Denzell
    Nov 12 at 2:41













up vote
0
down vote

favorite









up vote
0
down vote

favorite











I want to send FCM push notifications in specific android users only using their token saved in mysql database as identification. here's my current progress



PHP Script Snippet Code: Report_Status.php (File 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



<?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. It is supposed to go in Push_User_Notification and return to Report_Status once the process is done. Am I wrong in the implementation of calling the
Push_User_Notification.php or the receiving parameters to
Push_User_Notification.php?



P.S.



Here's my full source code of Report_Status.php in case anyone wants to check it: Report_Status.php










share|improve this question















I want to send FCM push notifications in specific android users only using their token saved in mysql database as identification. here's my current progress



PHP Script Snippet Code: Report_Status.php (File 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



<?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. It is supposed to go in Push_User_Notification and return to Report_Status once the process is done. Am I wrong in the implementation of calling the
Push_User_Notification.php or the receiving parameters to
Push_User_Notification.php?



P.S.



Here's my full source code of Report_Status.php in case anyone wants to check it: Report_Status.php







php android mysql firebase firebase-cloud-messaging






share|improve this question















share|improve this question













share|improve this question




share|improve this question








edited Nov 12 at 16:33

























asked Nov 12 at 2:05









Denzell

437




437












  • What do you mean by stuck. Is it supposed to go to a different page.
    – Mohammad C
    Nov 12 at 2:11










  • @MohammadC yes, but it does not going to a different page.
    – Denzell
    Nov 12 at 2:13










  • Do you get any error messages on the page.
    – Mohammad C
    Nov 12 at 2:18










  • Also your $message should look something like this $message = array('title' => 'This is a title.', 'body' => 'Here is a message.');
    – Mohammad C
    Nov 12 at 2:20












  • @MohammadC none. I tried before my code and it's working perfectly. the only problem before is my SQL query, instead of sending it to specific device/s it sends to all users who had a token.
    – Denzell
    Nov 12 at 2:41


















  • What do you mean by stuck. Is it supposed to go to a different page.
    – Mohammad C
    Nov 12 at 2:11










  • @MohammadC yes, but it does not going to a different page.
    – Denzell
    Nov 12 at 2:13










  • Do you get any error messages on the page.
    – Mohammad C
    Nov 12 at 2:18










  • Also your $message should look something like this $message = array('title' => 'This is a title.', 'body' => 'Here is a message.');
    – Mohammad C
    Nov 12 at 2:20












  • @MohammadC none. I tried before my code and it's working perfectly. the only problem before is my SQL query, instead of sending it to specific device/s it sends to all users who had a token.
    – Denzell
    Nov 12 at 2:41
















What do you mean by stuck. Is it supposed to go to a different page.
– Mohammad C
Nov 12 at 2:11




What do you mean by stuck. Is it supposed to go to a different page.
– Mohammad C
Nov 12 at 2:11












@MohammadC yes, but it does not going to a different page.
– Denzell
Nov 12 at 2:13




@MohammadC yes, but it does not going to a different page.
– Denzell
Nov 12 at 2:13












Do you get any error messages on the page.
– Mohammad C
Nov 12 at 2:18




Do you get any error messages on the page.
– Mohammad C
Nov 12 at 2:18












Also your $message should look something like this $message = array('title' => 'This is a title.', 'body' => 'Here is a message.');
– Mohammad C
Nov 12 at 2:20






Also your $message should look something like this $message = array('title' => 'This is a title.', 'body' => 'Here is a message.');
– Mohammad C
Nov 12 at 2:20














@MohammadC none. I tried before my code and it's working perfectly. the only problem before is my SQL query, instead of sending it to specific device/s it sends to all users who had a token.
– Denzell
Nov 12 at 2:41




@MohammadC none. I tried before my code and it's working perfectly. the only problem before is my SQL query, instead of sending it to specific device/s it sends to all users who had a token.
– Denzell
Nov 12 at 2:41












1 Answer
1






active

oldest

votes

















up vote
2
down vote



accepted










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.



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. 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.



updated



$User_Token needs to be an array. so remove the implode. That was my mistake.



Secondly the $message needs to be in the following format.



$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. Notification Messages or Data Messages. Read more here: 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. Adding the notification field allows android to handle notifications automatically if your app is in the background. So make sure you app is in the background when testing. 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. 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



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





share|improve this answer























  • Thanks. the logic of your code improves the flow of the program, but it still not sending any notifications. what seems to be the problem (new problem /old problem)? But it sucessfully retrieved the tokens when I print the $tokens array.
    – Denzell
    Nov 12 at 16:28












  • BTW here's my full source code of Report_Status.php in case you want to checl: Report_Status.php
    – Denzell
    Nov 12 at 16:31












  • Okay. I will try and take a look when i get time.
    – Mohammad C
    Nov 12 at 19:34










  • @Denzell Hey, Check my updated section of my answer. If the code does not work then make sure to echo the error and post it here.
    – Mohammad C
    Nov 13 at 1:48








  • 1




    Yes, I already fixed it. :D
    – Denzell
    Nov 21 at 17:05











Your Answer






StackExchange.ifUsing("editor", function () {
StackExchange.using("externalEditor", function () {
StackExchange.using("snippets", function () {
StackExchange.snippets.init();
});
});
}, "code-snippets");

StackExchange.ready(function() {
var channelOptions = {
tags: "".split(" "),
id: "1"
};
initTagRenderer("".split(" "), "".split(" "), channelOptions);

StackExchange.using("externalEditor", function() {
// Have to fire editor after snippets, if snippets enabled
if (StackExchange.settings.snippets.snippetsEnabled) {
StackExchange.using("snippets", function() {
createEditor();
});
}
else {
createEditor();
}
});

function createEditor() {
StackExchange.prepareEditor({
heartbeatType: 'answer',
convertImagesToLinks: true,
noModals: true,
showLowRepImageUploadWarning: true,
reputationToPostImages: 10,
bindNavPrevention: true,
postfix: "",
imageUploader: {
brandingHtml: "Powered by u003ca class="icon-imgur-white" href="https://imgur.com/"u003eu003c/au003e",
contentPolicyHtml: "User contributions licensed under u003ca href="https://creativecommons.org/licenses/by-sa/3.0/"u003ecc by-sa 3.0 with attribution requiredu003c/au003e u003ca href="https://stackoverflow.com/legal/content-policy"u003e(content policy)u003c/au003e",
allowUrls: true
},
onDemand: true,
discardSelector: ".discard-answer"
,immediatelyShowMarkdownHelp:true
});


}
});














draft saved

draft discarded


















StackExchange.ready(
function () {
StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f53255158%2fsend-fcm-push-notifcations-to-specific-devices-in-android-app-using-mysql-query%23new-answer', 'question_page');
}
);

Post as a guest















Required, but never shown

























1 Answer
1






active

oldest

votes








1 Answer
1






active

oldest

votes









active

oldest

votes






active

oldest

votes








up vote
2
down vote



accepted










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.



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. 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.



updated



$User_Token needs to be an array. so remove the implode. That was my mistake.



Secondly the $message needs to be in the following format.



$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. Notification Messages or Data Messages. Read more here: 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. Adding the notification field allows android to handle notifications automatically if your app is in the background. So make sure you app is in the background when testing. 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. 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



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





share|improve this answer























  • Thanks. the logic of your code improves the flow of the program, but it still not sending any notifications. what seems to be the problem (new problem /old problem)? But it sucessfully retrieved the tokens when I print the $tokens array.
    – Denzell
    Nov 12 at 16:28












  • BTW here's my full source code of Report_Status.php in case you want to checl: Report_Status.php
    – Denzell
    Nov 12 at 16:31












  • Okay. I will try and take a look when i get time.
    – Mohammad C
    Nov 12 at 19:34










  • @Denzell Hey, Check my updated section of my answer. If the code does not work then make sure to echo the error and post it here.
    – Mohammad C
    Nov 13 at 1:48








  • 1




    Yes, I already fixed it. :D
    – Denzell
    Nov 21 at 17:05















up vote
2
down vote



accepted










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.



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. 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.



updated



$User_Token needs to be an array. so remove the implode. That was my mistake.



Secondly the $message needs to be in the following format.



$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. Notification Messages or Data Messages. Read more here: 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. Adding the notification field allows android to handle notifications automatically if your app is in the background. So make sure you app is in the background when testing. 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. 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



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





share|improve this answer























  • Thanks. the logic of your code improves the flow of the program, but it still not sending any notifications. what seems to be the problem (new problem /old problem)? But it sucessfully retrieved the tokens when I print the $tokens array.
    – Denzell
    Nov 12 at 16:28












  • BTW here's my full source code of Report_Status.php in case you want to checl: Report_Status.php
    – Denzell
    Nov 12 at 16:31












  • Okay. I will try and take a look when i get time.
    – Mohammad C
    Nov 12 at 19:34










  • @Denzell Hey, Check my updated section of my answer. If the code does not work then make sure to echo the error and post it here.
    – Mohammad C
    Nov 13 at 1:48








  • 1




    Yes, I already fixed it. :D
    – Denzell
    Nov 21 at 17:05













up vote
2
down vote



accepted







up vote
2
down vote



accepted






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.



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. 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.



updated



$User_Token needs to be an array. so remove the implode. That was my mistake.



Secondly the $message needs to be in the following format.



$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. Notification Messages or Data Messages. Read more here: 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. Adding the notification field allows android to handle notifications automatically if your app is in the background. So make sure you app is in the background when testing. 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. 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



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





share|improve this answer














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.



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. 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.



updated



$User_Token needs to be an array. so remove the implode. That was my mistake.



Secondly the $message needs to be in the following format.



$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. Notification Messages or Data Messages. Read more here: 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. Adding the notification field allows android to handle notifications automatically if your app is in the background. So make sure you app is in the background when testing. 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. 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



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






share|improve this answer














share|improve this answer



share|improve this answer








edited Nov 13 at 2:34

























answered Nov 12 at 3:10









Mohammad C

1,1701212




1,1701212












  • Thanks. the logic of your code improves the flow of the program, but it still not sending any notifications. what seems to be the problem (new problem /old problem)? But it sucessfully retrieved the tokens when I print the $tokens array.
    – Denzell
    Nov 12 at 16:28












  • BTW here's my full source code of Report_Status.php in case you want to checl: Report_Status.php
    – Denzell
    Nov 12 at 16:31












  • Okay. I will try and take a look when i get time.
    – Mohammad C
    Nov 12 at 19:34










  • @Denzell Hey, Check my updated section of my answer. If the code does not work then make sure to echo the error and post it here.
    – Mohammad C
    Nov 13 at 1:48








  • 1




    Yes, I already fixed it. :D
    – Denzell
    Nov 21 at 17:05


















  • Thanks. the logic of your code improves the flow of the program, but it still not sending any notifications. what seems to be the problem (new problem /old problem)? But it sucessfully retrieved the tokens when I print the $tokens array.
    – Denzell
    Nov 12 at 16:28












  • BTW here's my full source code of Report_Status.php in case you want to checl: Report_Status.php
    – Denzell
    Nov 12 at 16:31












  • Okay. I will try and take a look when i get time.
    – Mohammad C
    Nov 12 at 19:34










  • @Denzell Hey, Check my updated section of my answer. If the code does not work then make sure to echo the error and post it here.
    – Mohammad C
    Nov 13 at 1:48








  • 1




    Yes, I already fixed it. :D
    – Denzell
    Nov 21 at 17:05
















Thanks. the logic of your code improves the flow of the program, but it still not sending any notifications. what seems to be the problem (new problem /old problem)? But it sucessfully retrieved the tokens when I print the $tokens array.
– Denzell
Nov 12 at 16:28






Thanks. the logic of your code improves the flow of the program, but it still not sending any notifications. what seems to be the problem (new problem /old problem)? But it sucessfully retrieved the tokens when I print the $tokens array.
– Denzell
Nov 12 at 16:28














BTW here's my full source code of Report_Status.php in case you want to checl: Report_Status.php
– Denzell
Nov 12 at 16:31






BTW here's my full source code of Report_Status.php in case you want to checl: Report_Status.php
– Denzell
Nov 12 at 16:31














Okay. I will try and take a look when i get time.
– Mohammad C
Nov 12 at 19:34




Okay. I will try and take a look when i get time.
– Mohammad C
Nov 12 at 19:34












@Denzell Hey, Check my updated section of my answer. If the code does not work then make sure to echo the error and post it here.
– Mohammad C
Nov 13 at 1:48






@Denzell Hey, Check my updated section of my answer. If the code does not work then make sure to echo the error and post it here.
– Mohammad C
Nov 13 at 1:48






1




1




Yes, I already fixed it. :D
– Denzell
Nov 21 at 17:05




Yes, I already fixed it. :D
– Denzell
Nov 21 at 17:05


















draft saved

draft discarded




















































Thanks for contributing an answer to Stack Overflow!


  • Please be sure to answer the question. Provide details and share your research!

But avoid



  • Asking for help, clarification, or responding to other answers.

  • Making statements based on opinion; back them up with references or personal experience.


To learn more, see our tips on writing great answers.





Some of your past answers have not been well-received, and you're in danger of being blocked from answering.


Please pay close attention to the following guidance:


  • Please be sure to answer the question. Provide details and share your research!

But avoid



  • Asking for help, clarification, or responding to other answers.

  • Making statements based on opinion; back them up with references or personal experience.


To learn more, see our tips on writing great answers.




draft saved


draft discarded














StackExchange.ready(
function () {
StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f53255158%2fsend-fcm-push-notifcations-to-specific-devices-in-android-app-using-mysql-query%23new-answer', 'question_page');
}
);

Post as a guest















Required, but never shown





















































Required, but never shown














Required, but never shown












Required, but never shown







Required, but never shown

































Required, but never shown














Required, but never shown












Required, but never shown







Required, but never shown







Popular posts from this blog

List item for chat from Array inside array React Native

Thiostrepton

Caerphilly