简体   繁体   English

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

[英]Firebase push notification is not working in android kotlin and PHP

i am trying to send push notification from my android device through my server, so i have wrote kotlin code for getting token from firebase and stored it into my server.我正在尝试通过我的服务器从我的 android 设备发送推送通知,所以我编写了 kotlin 代码以从 firebase 获取令牌并将其存储到我的服务器中。 next step i wrote php script to fetch stored tokens from my server and send message command to firebase.下一步我编写了 php 脚本从我的服务器获取存储的令牌并将消息命令发送到 firebase。 i have tested the same API using postman and u got the success message我已经使用 postman 测试了相同的 API 并且您收到了成功消息

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

but the message is not received in my android application, when i directly send notification from the firebase console the notification is received in my application i think the problem is in my PHP script.但是在我的 android 应用程序中没有收到该消息,当我直接从 firebase 控制台发送通知时,我的应用程序中收到了通知,我认为问题出在我的 PHP 脚本中。 i am new to this firebase configuration and PHP help me to complete this one.我是这个 firebase 配置的新手,PHP 帮助我完成这个配置。 below i'll add my kotlin code and PHP scripts下面我将添加我的 kotlin 代码和 PHP 脚本

my kotlin file我的 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!!)
    }
}
}

fetch token from my db从我的数据库中获取令牌

   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 send PHP 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;

}
}

sett message PHP设置消息 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;
}

}

*sorry for my bad english *对不起,我的英语不好

You are receiving notification payload on your android app so change your php getPush function to use notification payload:您正在 android 应用程序上接收通知有效负载,因此更改您的 php getPush function 以使用通知有效负载:

public function getPush() {
    $res = array();
    $res['title'] = $this->title;
    $res['body'] = $this->message;
    $res['image'] = $this->image;
    return $res;
}

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM