火力地堡使用PHP不工作
问题描述:
我目前从下面这个网站的教程发送通知。火力地堡使用PHP不工作
config_firebase.php
<?php
// Firebase API Key
define('FIREBASE_API_KEY', 'xxx');
?>
push_firebase.php
<?php
class Push {
// push message title
private $title;
private $message;
private $image;
// push message payload
private $data;
// flag indicating whether to show the push
// notification or not
// this flag will be useful when perform some opertation
// in background when push is recevied
private $is_background;
function __construct() {
}
public function setTitle($title) {
$this->title = $title;
}
public function setMessage($message) {
$this->message = $message;
}
public function setImage($imageUrl) {
$this->image = $imageUrl;
}
public function setPayload($data) {
$this->data = $data;
}
public function setIsBackground($is_background) {
$this->is_background = $is_background;
}
public function getPush() {
$res = array();
$res['data']['title'] = $this->title;
$res['data']['is_background'] = $this->is_background;
$res['data']['message'] = $this->message;
$res['data']['image'] = $this->image;
$res['data']['payload'] = $this->data;
$res['data']['timestamp'] = date('Y-m-d G:i:s');
return $res;
}
}
?>
firebase.php
<?php
class Firebase {
// sending push message to single user by firebase reg id
public function send($to, $message) {
$fields = array(
'to' => $to,
'data' => $message,
);
return $this->sendPushNotification($fields);
}
// Sending message to a topic by topic name
public function sendToTopic($to, $message) {
$fields = array(
'to' => '/topics/' . $to,
'data' => $message,
);
return $this->sendPushNotification($fields);
}
// sending push message to multiple users by firebase registration ids
public function sendMultiple($registration_ids, $message) {
$fields = array(
'to' => $registration_ids,
'data' => $message,
);
return $this->sendPushNotification($fields);
}
// function makes curl request to firebase servers
private function sendPushNotification($fields) {
require_once __DIR__ . '/config_firebase.php';
// Set POST variables
$url = 'https://fcm.googleapis.com/fcm/send';
$headers = array(
'Authorization: key=' . FIREBASE_API_KEY,
'Content-Type: application/json'
);
// Open connection
$ch = curl_init();
// Set the url, number of POST vars, POST data
curl_setopt($ch, CURLOPT_URL, $url);
curl_setopt($ch, CURLOPT_POST, true);
curl_setopt($ch, CURLOPT_HTTPHEADER, $headers);
curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
// Disabling SSL Certificate support temporarly
curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, false);
curl_setopt($ch, CURLOPT_POSTFIELDS, json_encode($fields));
// Execute post
$result = curl_exec($ch);
if ($result === FALSE) {
die('Curl failed: ' . curl_error($ch));
}
// Close connection
curl_close($ch);
return $result;
}
}
?>
push_notification_firebase.php
<html>
<head>
<title>AndroidHive | Firebase Cloud Messaging</title>
<meta charset="UTF-8">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<link rel="shortcut icon" href="//www.gstatic.com/mobilesdk/160503_mobilesdk/logo/favicon.ico">
<link rel="stylesheet" href="http://yui.yahooapis.com/pure/0.6.0/pure-min.css">
<style type="text/css">
body{
}
div.container{
width: 1000px;
margin: 0 auto;
position: relative;
}
legend{
font-size: 30px;
color: #555;
}
.btn_send{
background: #00bcd4;
}
label{
margin:10px 0px !important;
}
textarea{
resize: none !important;
}
.fl_window{
width: 400px;
position: absolute;
right: 0;
top:100px;
}
pre, code {
padding:10px 0px;
box-sizing:border-box;
-moz-box-sizing:border-box;
webkit-box-sizing:border-box;
display:block;
white-space: pre-wrap;
white-space: -moz-pre-wrap;
white-space: -pre-wrap;
white-space: -o-pre-wrap;
word-wrap: break-word;
width:100%; overflow-x:auto;
}
</style>
</head>
<body>
<?php
// Enabling error reporting
error_reporting(-1);
ini_set('display_errors', 'On');
require_once __DIR__ . '/firebase.php';
require_once __DIR__ . '/push_firebase.php';
$firebase = new Firebase();
$push = new Push();
// optional payload
$payload = array();
$payload['team'] = 'India';
$payload['score'] = '5.6';
// notification title
$title = isset($_GET['title']) ? $_GET['title'] : '';
// notification message
$message = isset($_GET['message']) ? $_GET['message'] : '';
// push type - single user/topic
$push_type = isset($_GET['push_type']) ? $_GET['push_type'] : '';
// whether to include to image or not
$include_image = isset($_GET['include_image']) ? TRUE : FALSE;
$push->setTitle($title);
$push->setMessage($message);
if ($include_image) {
$push->setImage('http://api.androidhive.info/images/minion.jpg');
} else {
$push->setImage('');
}
$push->setIsBackground(FALSE);
$push->setPayload($payload);
$json = '';
$response = '';
if ($push_type == 'topic') {
$json = $push->getPush();
$response = $firebase->sendToTopic('global', $json);
} else if ($push_type == 'individual') {
$json = $push->getPush();
$regId = isset($_GET['regId']) ? $_GET['regId'] : '';
$response = $firebase->send($regId, $json);
}
?>
<div class="container">
<div class="fl_window">
<div><img src="http://api.androidhive.info/images/firebase_logo.png" width="200" alt="Firebase"/></div>
<br/>
<?php if ($json != '') { ?>
<label><b>Request:</b></label>
<div class="json_preview">
<pre><?php echo json_encode($json) ?></pre>
</div>
<?php } ?>
<br/>
<?php if ($response != '') { ?>
<label><b>Response:</b></label>
<div class="json_preview">
<pre><?php echo json_encode($response) ?></pre>
</div>
<?php } ?>
</div>
<form class="pure-form pure-form-stacked" method="get">
<fieldset>
<legend>Send to Single Device</legend>
<label for="redId">Firebase Reg Id</label>
<input type="text" id="redId" name="regId" class="pure-input-1-2" placeholder="Enter firebase registration id">
<label for="title">Title</label>
<input type="text" id="title" name="title" class="pure-input-1-2" placeholder="Enter title">
<label for="message">Message</label>
<textarea class="pure-input-1-2" rows="5" name="message" id="message" placeholder="Notification message!"></textarea>
<label for="include_image" class="pure-checkbox">
<input name="include_image" id="include_image" type="checkbox"> Include image
</label>
<input type="hidden" name="push_type" value="individual"/>
<button type="submit" class="pure-button pure-button-primary btn_send">Send</button>
</fieldset>
</form>
<br/><br/><br/><br/>
<form class="pure-form pure-form-stacked" method="get">
<fieldset>
<legend>Send to Topic `global`</legend>
<label for="title1">Title</label>
<input type="text" id="title1" name="title" class="pure-input-1-2" placeholder="Enter title">
<label for="message1">Message</label>
<textarea class="pure-input-1-2" name="message" id="message1" rows="5" placeholder="Notification message!"></textarea>
<label for="include_image1" class="pure-checkbox">
<input id="include_image1" name="include_image" type="checkbox"> Include image
</label>
<input type="hidden" name="push_type" value="topic"/>
<button type="submit" class="pure-button pure-button-primary btn_send">Send to Topic</button>
</fieldset>
</form>
</div>
</body>
</html>
这是我得到的结果:
请求:
{"data":{"title":"This is a second test from php","is_background":false,"message":"Second test from php","image":"","payload":{"team":"India","score":"5.6"},"timestamp":"2017-02-28 3:05:55"}}
响应:
"{\"message_id\":5384787464945132888}"
我找不到哪部分出了问题。有谁知道出了什么问题?
编辑: push_firebase.php
public function getPush() {
$res = array();
$res['notification']['title'] = $this->title;
//$res['data']['is_background'] = $this->is_background;
//$res['data']['message'] = $this->message;
//$res['data']['image'] = $this->image;
//$res['data']['payload'] = $this->data;
//$res['data']['timestamp'] = date('Y-m-d G:i:s');
return $res;
}
firebase.php
// sending push message to single user by firebase reg id
public function send($to, $message) {
$fields = array(
'to' => $to,
'notification' => $message,
);
return $this->sendPushNotification($fields);
}
// Sending message to a topic by topic name
public function sendToTopic($to, $message) {
$fields = array(
'to' => '/topics/' . $to,
'notification' => $message,
);
return $this->sendPushNotification($fields);
}
// sending push message to multiple users by firebase registration ids
public function sendMultiple($registration_ids, $message) {
$fields = array(
'to' => $registration_ids,
'notification' => $message,
);
return $this->sendPushNotification($fields);
}
请求:
{"notification":{"title":"test"}}
响应:
"{\"message_id\":5724949249510866740}"
的Android代码onMessageReceived:
public void onMessageReceived(RemoteMessage remoteMessage){
Log.e(TAG,"FROM: " + remoteMessage.getFrom());
if(remoteMessage == null){
return;
}
// Check if message contains a notification payload.
if (remoteMessage.getNotification() != null) {
Log.e(TAG, "Notification Body: " + remoteMessage.getNotification().getBody());
handleNotification(remoteMessage.getNotification().getBody());
}
//check if message contains a data payload
if(remoteMessage.getData().size() > 0){
Log.e(TAG,"Data Payload: " + remoteMessage.getData().toString());
try{
JSONObject json = new JSONObject(remoteMessage.getData().toString());
handleDataMessage(json);
}catch(Exception e){
Log.e(TAG,"Exception: " + e.getMessage());
}
}
}
答
的响应示出了message_id
这意味着请求是成功。
您是否指出您无法收到通知?如果是这样,那么这可能是因为您使用的是data
消息有效载荷,而您的Android代码仅处理notification
消息有效载荷。有关两者的描述,请参阅Message Types。
来自Firebase控制台的推送通知被分类为notification
消息有效内容。当您在高级选项中添加自定义键值对时,它将成为两者(单个有效内容中的notification
和data
)的组合。
要处理data
- 只有有效载荷,您必须使用RemoteMessage.getData()
。有关更多详细信息,请参阅Handling Messages in Android。
在您编辑的脚本中,您只是添加了标题,但在您的Android侧代码中,您只处理通知正文。尝试['handleNotification(remoteMessage.getNotification().getTitle());'](https://firebase.google.com/docs/reference/android/com/google/firebase/messaging/RemoteMessage.Notification.html#getTitle ())。 –