我正试图通过我的服务器从我的android设备发送推送通知,所以我已经编写了从firebase获取令牌的kotlin代码,并将其存储到我的服务器中。下一步,我编写了php脚本,从服务器中获取存储的令牌,并向firebase发送消息命令。我已经用邮递员测试了相同的API,你得到了成功的消息
{"multicast_id":7524239394194034238,"success":1,"failure":0,"canonical_ids":0,"results":[{"message_id":"0:1587205979775713%03eb2b8403eb2b84"}]}[]
但我的android应用程序中没有收到消息,当我直接从firebase控制台发送通知时,我的应用程序中收到了通知,我认为问题出在我的PHP脚本中。我是这个firebase配置的新手,PHP帮助我完成了这个配置。下面我将添加我的kotlin代码和PHP脚本
我的kotlin文件
class myfirebasemessaging: FirebaseMessagingService() {
override fun onMessageReceived(remoteMessage: RemoteMessage?) {
super.onMessageReceived(remoteMessage)
if (remoteMessage!!.notification != null) {
val title = remoteMessage.notification!!.title
val body = remoteMessage.notification!!.body
NotificationHelper.displayNotification(applicationContext, title!!, body!!)
}
}
}
从我的数据库中提取令牌
public function getAllTokens($usertype){
$stmt = $this->con->prepare("SELECT token from fcm_token WHERE user_type=?");
$stmt->bind_param("s", $usertype);
$stmt->execute();
//$stmt->bind_result($token);
$result = $stmt->get_result();
$tokens = array();
while($token = $result->fetch_assoc()){
array_push($tokens, $token['token']);
}
return $tokens;
}
Firebase发送PHP
class Firebase {
public function send($registration_ids, $message) {
$fields = array(
'registration_ids' => $registration_ids,
'notification' => $message,
);
return $this->sendPushNotification($fields);
}
/*
* This function will make the actuall curl request to firebase server
* and then the message is sent
*/
private function sendPushNotification($fields) {
//importing the constant files
require_once '../Constants.php';
//firebase server url to send the curl request
$url = 'https://fcm.googleapis.com/fcm/send';
//building headers for the request
$headers = array(
'Authorization: key=' . FIREBASE_API_KEY,
'Content-Type: application/json'
);
//Initializing curl to open a connection
$ch = curl_init();
//Setting the curl url
curl_setopt($ch, CURLOPT_URL, $url);
//setting the method as post
curl_setopt($ch, CURLOPT_POST, true);
//adding headers
curl_setopt($ch, CURLOPT_HTTPHEADER, $headers);
curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
//disabling ssl support
curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, false);
//adding the fields in json format
curl_setopt($ch, CURLOPT_POSTFIELDS, json_encode($fields));
//finally executing the curl request
$result = curl_exec($ch);
if ($result === FALSE) {
die('Curl failed: ' . curl_error($ch));
}
//Now close the connection
curl_close($ch);
//and return the result
return $result;
}
}
设置消息PHP
<?php
class Push {
//notification title
private $title;
//notification message
private $message;
//notification image url
private $image;
//initializing values in this constructor
function __construct($title, $message, $image) {
$this->title = $title;
$this->message = $message;
$this->image = $image;
}
//getting the push notification
public function getPush() {
$res = array();
$res['data']['title'] = $this->title;
$res['data']['message'] = $this->message;
$res['data']['image'] = $this->image;
return $res;
}
}
*抱歉我的英语不好
您在android应用程序上收到通知负载,因此请更改php getPush函数以使用通知负载:
public function getPush() {
$res = array();
$res['title'] = $this->title;
$res['body'] = $this->message;
$res['image'] = $this->image;
return $res;
}