简体   繁体   中英

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. next step i wrote php script to fetch stored tokens from my server and send message command to firebase. i have tested the same API using postman and u got the success message

{"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. i am new to this firebase configuration and PHP help me to complete this one. below i'll add my kotlin code and PHP scripts

my kotlin file

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

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 

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:

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

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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