Not getting complete information on push notification on cordova (cordova-plugin-firebasex)

793 views Asked by At

I am developing a Cordova app where I need push notifications. For push notification, i used cordova-plugin-firebasex plugin . its works well when app is foreground but when app is background push notification display on the notification bar but when i click it deosn't shows title,body and additional data .Currently it shows following message:

collapse_key: "com.hadi.storex"  
from: "383656394813"  
google.delivered_priority: "normal"  
google.message_id: "0:1620737447755747%7dc0077a7dc0077a"   
google.original_priority: "normal"   
google.sent_time: 1620737447734   
google.ttl: 2419200   
messageType: "notification"   
tap: "background"   

My js code is following :

-

   function onDeviceReady() {
          FirebasePlugin.getToken(function (fcmToken) {
          console.log(fcmToken);    }, function (error) {
          console.error(error);    });    FirebasePlugin.onTokenRefresh(function (fcmToken) {
          console.log(fcmToken);    }, function (error) {
          console.error(error);    });    FirebasePlugin.onMessageReceived(function (message) {
          console.log("Message type: " + message.messageType);
          if (message.messageType === "notification") {
              console.log("Notification message received");
              if (message.tap) {
                  console.log("Tapped in " + message.tap);
              }
          }
          console.dir(message);
         }, function (error) {
             console.error(error);
         });    }
       document.addEventListener("deviceready", onDeviceReady, false);

My PHP code is following:

-

$path_to_fcm = "https://fcm.googleapis.com/fcm/send";   
       $headers = array(    
          'Authorization:key=' . FCM_SERVER_KEY,    
          'Content-Type:application/json');     
   
   $fields = array(

       "to" => 'registration_ids',

       'notification' => array(
           'title' => "title of notification1122",
           'body' => "your notification goes here22233",
           "surveyID" => "ewtawgreg-gragrag-rgarhthgbad",
           "data" => array(
               'title' => "title of notification1122",
               'body' => "your notification goes here22233",
               "notification_body" => "Notification body",
               "notification_title" => "Notification title",
               "surveyID" => "ewtawgreg-gragrag-rgarhthgbad"
           )
       )
   );

   $payload = json_encode($fields);


   $curl_session = curl_init();

   curl_setopt($curl_session, CURLOPT_URL, $path_to_fcm);
   curl_setopt($curl_session, CURLOPT_POST, true);
   curl_setopt($curl_session, CURLOPT_HTTPHEADER, $headers);
   curl_setopt($curl_session, enter code hereCURLOPT_RETURNTRANSFER, true);
   curl_setopt($curl_session, CURLOPT_SSL_VERIFYPEER, false);
   curl_setopt($curl_session, CURLOPT_IPRESOLVE, CURL_IPRESOLVE_V4);
   curl_setopt($curl_session, CURLOPT_POSTFIELDS, $payload);

   $curl_result = curl_exec($curl_session);
   print_r($curl_result);
1

There are 1 answers

0
Muhammad Hadi Qureshi On

the correct way to send data on the fcm server is

$fields = array(

        'notification' => array(
            'title' => "title of notification1122",
            'body' => "your notification goes here22233",
            "sound" => "default",
            "click_action" => "FCM_PLUGIN_ACTIVITY",
            "icon" => "fcm_push_icon"
        ),
        "data" => array(
            "param1" => "value1",
            "param2" => "value2"
        ),
        "to" => any device device token or topic like "/topics/topicName" , 
        "priority" => "high",
        "restricted_package_name" => ""
    );

    $payload = json_encode($fields);