Firebase 推送通知在 android kotlin 和 PHP 中不起作用

Firebase push notification is not working in android kotlin and PHP

我正在尝试通过我的服务器从我的 android 设备发送推送通知,因此我编写了 kotlin 代码以从 firebase 获取令牌并将其存储到我的服务器中。下一步我写了 php 脚本来从我的服务器获取存储的令牌并将消息命令发送到 firebase。我已经使用邮递员测试了相同的 API,你收到了成功消息

{"multicast_id":7524239394194034238,"success":1,"failure":0,"canonical_ids":0,"results":[{"message_id":"0:1587205979775713%03eb2b8403eb2b84"}]}[]

但是我的 android 应用程序没有收到消息,当我直接从 firebase 控制台发送通知时,我的应用程序收到了通知,我认为问题出在我的 PHP 脚本中。我是这个 firebase 配置的新手,PHP 帮我完成了这个配置。 下面我将添加我的科特林代码和 PHP 脚本

我的科特林文件

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;
}