【问题标题】:Send FCM Push notifcations to specific devices in android app using MySQL query as identification from a PHP script使用 MySQL 查询作为 PHP 脚本的标识,将 FCM 推送通知发送到 android 应用程序中的特定设备
【发布时间】:2018-11-12 02:05:01
【问题描述】:

我想仅使用保存在 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.php 或接收参数到 Push_User_Notification.php?

附言

这是我的 Report_Status.php 的完整源代码,如果有人想查看它:Report_Status.php

【问题讨论】:

  • 卡住是什么意思。它应该转到不同的页面吗?
  • @MohammadC 是的,但它不会转到其他页面。
  • 您是否在页面上收到任何错误消息。
  • 你的$message 应该看起来像这样$message = array('title' =&gt; 'This is a title.', 'body' =&gt; 'Here is a message.');
  • @MohammadC 无。我在我的代码之前尝试过,它运行良好。之前唯一的问题是我的 SQL 查询,而不是将其发送到特定设备,而是发送给所有拥有令牌的用户。

标签: php android mysql firebase firebase-cloud-messaging


【解决方案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 方法)所以我可能会建议对send_notification 函数中的$fields 数组进行小改动。如果您的应用在后台,添加通知字段允许 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);

【讨论】:

  • 谢谢。您的代码逻辑改进了程序的流程,但它仍然没有发送任何通知。似乎是什么问题(新问题/旧问题)?但是当我打印 $tokens 数组时,它成功地检索到了令牌。
  • 顺便说一句,这是我的 Report_Status.php 的完整源代码,如果您想查看:Report_Status.php
  • 好的。有时间我会去看看的。
  • @Denzell 嘿,检查我的答案的更新部分。如果代码不起作用,请确保回显错误并在此处发布。
  • Output of echo $result = curl_exec($ch); 是的,它现在可以工作,但还有一个问题。设备现在已成功接收到它,但是当设备收到通知时,它的应用程序会崩溃。它不显示任何通知,但每次我触发 PHP 脚本 Push_User_Notification.php 时应用程序都会崩溃
猜你喜欢
  • 1970-01-01
  • 2017-04-27
  • 2017-09-15
  • 2021-01-07
  • 1970-01-01
  • 2014-06-04
  • 2017-09-15
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多