首页 > 解决方案 > Cordova android fcm插件jquery php点击背景通知

问题描述

我使用 cordova 和 Google FMC 通知插件制作应用程序,并且在应用程序处于前台之前一切正常。

//MY index.php
<div id="load"></div>  
<script>  
FCMPlugin.onNotification(function(data){
          alert( JSON.stringify(data) );
    // I DONT UNDERSTAND THIS SECTION ???
    // WHAT I SCHOULT DO HERE???

    if(data.wasTapped){
      //Notification was received on device tray and tapped by the user.
      alert( JSON.stringify(data) );

      // I RECIVED THE DATA ON "CLOSED" APP!  GET VARS AND LOAD FILE INTO div id="load" WITH AJAX BASED ON KEY SENDTH FROM FIREBASE CONSOLE
      // EXAMPLE KEY = MESSAGE =  10890 
      // $AJAX LOAD FILE MESSAGE ?ID=10890
      // CLEAR NOTIFICATIONS

       //example ajax
       var message= 10890; // GET FROM CORDOVA NOTIFICATION
       var dataString = '& message=' + message;             
       $.ajax({
       type: "POST",
       url: "https://mywebsite.com/LOAD.php",
       data: dataString,
       success: function(response) {
                        $('#info').html(response);
                                   }
              });

    }else{
      //Notification was received in foreground. Maybe the user needs to be notified.
      alert( JSON.stringify(data) );

      // I RECIVED THE DATA ON OPEN APP!  GET VARS AND LOAD FILE INTO div id="load" WITH AJAX BASED ON KEY SENDTH FROM FIREBASE CONSOLE
      // EXAMPLE KEY = MESSAGE =  10890 
      // $AJAX LOAD FILE MESSAGE ?ID=10890
      // CLEAR NOTIFICATIONS
      // HOW TO GET VAR FROM alert( JSON.stringify(data) ); ??

       //example ajax
       var message= 10890; // GET FROM CORDOVA NOTIFICATION
       var dataString = '& message=' + message;             
       $.ajax({
       type: "POST",
       url: "https://mywebsite.com/LOAD.php",
       data: dataString,
       success: function(response) {
                        $('#info').html(response);
                                   }
              });

    }

    });
    </script>

如果有人可以帮助解决这个谜团,那真是太好了。我希望它可以帮助其他像我这样不专业的人。我仅将应用程序用作包装器并加载外部 .php 应用程序可以很好地处理我需要的所有功能,这是我需要解决的唯一问题。

谢谢,对不起,我是新手

标签: androidjqueryfirebasecordova

解决方案


发送通知的问题

<?php
    $url = "https://fcm.googleapis.com/fcm/send";
    $token = "xxxx";
    $serverKey = 'xxx';

    $topic = 'mytopic';
    $title = "title";
    $body = "Message.";
    $img = 'https://url.com/images/img.jpg';


    $data = array('USR_id' => '1000',
                  'USR_name' => 'Name',
                  'MSG_id' => '1004',
                  'SEN_id' => '1001',                 
                  'USR_level' => 'user');



        $android = array('senderID' => 'xxxxxxxxxxxx',
                  'sound' => true,
                  'vibrate' => true,
                  'clearNotifications' => true,               
                  'forceShow' => true);



    $notification = array('title' =>$title,
                           'body' => $body,
                          'image' => $img,
                          'sound' => 'audio.mp3',
                          'color' => '#F6C02B',
                   'click_action' => 'FCM_PLUGIN_ACTIVITY',
                          'badge' => '1');

    $arrayToSend = array('to' => '/topics/' . $topic, 
               'notification' => $notification,
                       'data' => $data,
                    'android' => $android,
                   'priority' => 'high');

    $json = json_encode($arrayToSend);
    $headers = array();
    $headers[] = 'Content-Type: application/json';
    $headers[] = 'Authorization: key='. $serverKey;
    $ch = curl_init();
    curl_setopt($ch, CURLOPT_URL, $url);
    curl_setopt($ch, CURLOPT_CUSTOMREQUEST,"POST");
    curl_setopt($ch, CURLOPT_POSTFIELDS, $json);
    curl_setopt($ch, CURLOPT_HTTPHEADER,$headers);
    //Send the request
    $response = curl_exec($ch);
    //Close request
    if ($response === FALSE) {
    //die('FCM Send Error: ' . curl_error($ch));
    }
    curl_close($ch);
?>

在我用 php 发送它之后完美


推荐阅读