3

我正在使用PhoneGap。我想獲得我從FCM獲得的推送通知值。現在,我收到通知,但我無法在我的應用程序內接收通知值。如何使用FCM在phonegap中獲取推送通知值?

PHP側FCM代碼:

<?php 
// API access key from Google API's Console 
define('API_ACCESS_KEY', 'AIzaxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx'); 
$registrationIds = array($_GET['id']); 
// prep the bundle 
$msg = array 
(
    'body' => $_GET['body'], 
    'title'  => $_GET['title'], 
    'vibrate' => 1, 
    'sound'  => 1, 
); 
var_dump($msg); 
$fields = array 
(
    'registration_ids' => $registrationIds, 
    'notification'   => $msg 
); 

$headers = array 
(
    'Authorization: key=' . API_ACCESS_KEY, 
    'Content-Type: application/json' 
); 

$ch = curl_init(); 
curl_setopt($ch,CURLOPT_URL, 'https://fcm.googleapis.com/fcm/send'); 
curl_setopt($ch,CURLOPT_POST, true); 
curl_setopt($ch,CURLOPT_HTTPHEADER, $headers); 
curl_setopt($ch,CURLOPT_RETURNTRANSFER, true); 
curl_setopt($ch,CURLOPT_SSL_VERIFYPEER, false); 
curl_setopt($ch,CURLOPT_POSTFIELDS, json_encode($fields)); 
$result = curl_exec($ch); 
curl_close($ch); 
echo $result; 
?> 

這是我的腳本接收通知

<script type="text/javascript"> 
    function onLoad() { 
    document.addEventListener("deviceready", Fire, false); 
         } 

    function Fire() { 



     FCMPlugin.getToken(
      function (token) { 

       var name = document.getElementById("fireup"); 
       name.value = token; 
       localStorage.setItem("key",token); 
      }, 
      function (err) { 
        alert("Error: " + 'error retrieving token: ' + err); 
      } 
     ); 

     FCMPlugin.onNotification(function(data){ 
       if(data.wasTapped){ 
        alert(JSON.stringify(data.title)); 
       }else{ 
        alert(JSON.stringify(data.title)); 
       } 
       }, 
       function(msg){ 
        alert('onNotification callback successfully registered: ' + msg); 
       }, 
       function(err){ 
        alert('Error registering onNotification callback: ' + err); 
       } 
      ); 

    }; 


</script> 
+0

當我通知選項卡,我想獲得應用程序中的標題和正文,但我正在變得不確定。 –

+0

則返回undefined –

+0

我還想說的是,當我使用這個測試儀https://cordova-plugin-fcm.appspot.com/,我得到的通知以及通知值攻 –

回答

1

這是幫助我

<?php 
// API access key from Google API's Console 
define('API_ACCESS_KEY', 'AIzaSyCpN9xxxxxxxxxxxxxxxxxxxxxxx'); 
$registrationIds = array($_GET['id']); 
// prep the bundle 
$msg = array 
(
    'body' => $_GET['body'], 
    'title'  => $_GET['title'], 
    'vibrate' => 1, 
    'sound'  => 1, 
); 

$fields = array 
(
    'registration_ids' => $registrationIds, 
    'data'     => $msg, 
     'notification'  => $msg 
); 

$headers = array 
(
    'Authorization: key=' . API_ACCESS_KEY, 
    'Content-Type: application/json' 
); 

$ch = curl_init(); 
curl_setopt($ch,CURLOPT_URL, 'https://fcm.googleapis.com/fcm/send'); 
curl_setopt($ch,CURLOPT_POST, true); 
curl_setopt($ch,CURLOPT_HTTPHEADER, $headers); 
curl_setopt($ch,CURLOPT_RETURNTRANSFER, true); 
curl_setopt($ch,CURLOPT_SSL_VERIFYPEER, false); 
curl_setopt($ch,CURLOPT_POSTFIELDS, json_encode($fields)); 
$result = curl_exec($ch); 
curl_close($ch); 
//echo $result; 
echo json_encode(array("data"=>$msg)); 
?>