php - 推送通知正在发送到服务器但未出现在手机上

标签 php android firebase push-notification firebase-cloud-messaging

所以我有这个测试应用程序,我正在使用它来学习如何将推送通知从托管在我的桌面上的外部服务器发送到虚拟设备或智能手机。

然后我使用一个简单的表单并输入标题和消息,然后单击提交按钮发送通知。

我已经三重检查,通知的编码没问题,我能够将 firebase token 从 android 应用程序传递到服务器,服务器将其存储在 MySQL 数据库中。

此外,应用程序正确发送 token ,server_key 也正确(使用 firebase 网站上的内置复制功能)

为什么我的通知没有显示?这是将通知发送到 firebase 的 php 脚本的源代码:

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



?>

感谢任何帮助,提前致谢!

最佳答案

检查此链接以获取有关推送通知的更多信息

How to handle notification when app in background in Firebase

关于php - 推送通知正在发送到服务器但未出现在手机上,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/44534617/

相关文章:

javascript - 将逗号分隔列表转换为无序列表

php - 如何通过 jQuery+Codeigniter 获取值并显示在输入字段中?

android - 如何根据时区设置日历

ios - 配置应用程序时出现 Firebase 错误 - 无法识别的选择器已发送至类

php - 如何在 php 中使用 "alternative syntax"而不是 "curly-brace syntax"? (使用 Endswitch 语法糖)

javascript - 图像选择器 html 表单

java - 如何从firebase数据库中获取基于值的数据

java - AccessibilityService 对 OnKeyEvent() 没有响应

ios - Firebase loginWithFacebook 未在 FAuthData (Swift) 中返回足够的信息

firebase - Flutter中创建文档后返回新创建的Document ID