【发布时间】:2017-06-14 02:51:20
【问题描述】:
所以我有这个测试应用程序,我用它来学习如何将推送通知从托管在我桌面上的外部服务器发送到虚拟设备或智能手机。
然后我使用一个简单的表单并输入标题和消息,然后单击提交按钮发送通知。
我进行了三重检查,通知的编码很好,并且我能够将 firebase 令牌从 android 应用程序传递到服务器,服务器将其存储在 MySQL 数据库中。
此外,应用程序正确发送令牌,server_key 也正确(使用 firebase 网站的内置复制功能)
为什么我的通知没有显示?这是 php 中用于向 firebase 发送通知的脚本的源代码:
<?php
require "init.php";
$message = $_POST['message'];
$title = $_POST['title'];
$path_to_fcm = 'https://fcm.googleapis.com/fcm/send';
$server_key = "AAAA0Tedu0Y:APA91bGF1k9LAVw90LVM9IdWludKeG1ueVo2V7HesN4CVz2KFvcwGvLkDymuHjm0IvfRvZ6wOEu5Q33pBgYDUXopvTOBSDKQJf2zFFp_22gTCrg6zxNxKyw8_0M9ciPLt3YyJOwkmNYR";
$sql = "select fcm_token from fcm_info";
$result = mysqli_query($con,$sql);
//these lines get the key that has been store (i only store one key from one app, so it takes the first value of the result
$row = mysqli_fetch_row($result);
$key = $row[0];
$header = array(
'Authorization:key=' .$server_key,
'Content-Type:application/json'
);
$field = array( 'to'=>$key,
'notification'=>array('title'=>$title,'body'=>$message)
);
$payload = json_encode($field);
//If echo $payload, It print the following:
/*
{
"to":"esM8_IMRWrw:APA91bEVOTjpC5kkqAWqWwEs7gNq5-4iUvL6fC947cfWkg1G0VhKDiuibSOr_xSNcIJ8rb4VewjNJ2Jd_0AXBdQgTbOO0FO-stmP3ymOCLGQlka3s2RQ3854UiPr_puc274hXSlQMLen",
"notification":{
"title":"asdasd",
"body":"asdasd"
}
}
So the json is properly formatted
*/
/*this generates the http url connection from my server to firebase push notification sending url. I think the error is somewhere in these lines, but i dont know where*/
$curl_session = curl_init();
curl_setopt($curl_session,CURLOPT_URL,$path_to_fcm);
curl_setopt($curl_session,CURLOPT_POST,TRUE);
curl_setopt($curl_session,CURLOPT_HTTPHEADER,$header);
curl_setopt($curl_session,CURLOPT_RETURNTRANSFER,TRUE);
curl_setopt($curl_session,CURLOPT_SSL_VERIFYPEER,FALSE);
curl_setopt($curl_session,CURLOPT_IPRESOLVE,CURL_IPRESOLVE_V4);
curl_setopt($curl_session,CURLOPT_POSTFIELDS,$payload);
$result = curl_exec($curl_session);
curl_close($curl_session);
mysqli_close($con);
?>
任何帮助表示赞赏,在此先感谢!
【问题讨论】:
-
响应是否显示任何错误?
-
不,没有错误,通知根本没有显示在虚拟设备上。
-
我实际上只是通过删除整个数据库并重新安装应用程序再次尝试它并且它工作了一次。然后它再次停止工作
-
确保您的 PC 和智能手机连接到同一网络
-
@ArpitPatel 他们是,我也在自己的计算机上使用虚拟设备,但没有一个工作。
标签: php android firebase push-notification firebase-cloud-messaging