未获得有关 cordova 推送通知的完整信息 (cordova-plugin-firebasex)

Posted

技术标签:

【中文标题】未获得有关 cordova 推送通知的完整信息 (cordova-plugin-firebasex)【英文标题】:Not getting complete information on push notification on cordova (cordova-plugin-firebasex) 【发布时间】:2021-08-01 20:44:43 【问题描述】:

我正在开发一个需要推送通知的 Cordova 应用程序。对于推送通知,我使用了 cordova-plugin-firebasex 插件。它在应用程序处于前台时运行良好,但当应用程序处于后台时,通知栏上会显示推送通知,但当我点击它时,它不会显示标题、正文和其他数据。目前它显示以下消息:

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"   

我的 js 代码如下:

-

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

我的 php 代码如下:

-

$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】:

在fcm服务器上发送数据的正确方式是

$fields = 数组(

        '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);

【讨论】:

以上是关于未获得有关 cordova 推送通知的完整信息 (cordova-plugin-firebasex)的主要内容,如果未能解决你的问题,请参考以下文章

无法获得缺少的 gcm 推送通知

解析推送通知:iOS 设备令牌未定义

Cordova windows 8 手机 WNS 推送通知和应用发布流程及详细信息

应用程序被杀死时未收到 IOS 13 VOIP 推送通知

需要在 Cordova 应用程序中使用本机 APNs/GCM 为 iOS 推送通知的指导

未发送有关可用应用更新的 Google Play 推送通知。我错过了啥吗?