0

我正在使用phonegap-plugin-push來接收科爾多瓦應用程序中的通知。我正在部署Android Marsh-mellow進行測試。科爾多瓦背景Firebase推送通知

我想查看和存儲通過Firebase Console收到的通知的內容,當應用程序在後臺。

這裏是我的代碼 -

document.addEventListener("deviceready",onDeviceReady,false); 
    function onDeviceReady(){ 

     var push = PushNotification.init({ "android": {"senderID": "91254247XXXX"}}); 

     push.on('registration', function(data) { 
      console.log(data.registrationId); 
      //document.getElementById("gcm_id").innerHTML = data.registrationId; 
     }); 

     push.on('notification', function(data) { 

      alert("On Notification function!!"); 
      // data.message, 
      // data.title, 
      // data.count, 
      // data.sound, 
      // data.image, 
      // data.additionalData 
      console.log("notification event"); 
      console.log(JSON.stringify(data)); 
      alert(JSON.stringify(data)); 
      //Do something 
     }); 

     push.on('error', function(e) { 
      alert(e); 
     }); 
    } 

當應用程序在前景(在屏幕上)我正確地接收到內容(標題,消息,數據等),我能看到他們直接在應用程序中的警報框中。

但是,當應用程序在背景(不在屏幕上,但在後臺運行),我在通知區域中得到通知。當我點擊收到的通知時,它會將我重定向到應用中上次打開的頁面。

函數push.on('notification', function(data) {}未被調用。不顯示警報消息。有人可以幫助我如何訪問通知消息和數據嗎?

回答

0

經過2天的研究,我找到了答案。我們必須在數據字段中將「可用內容」設置爲1。否則,如果應用程序在後臺,它不會調用通知塊。

而現在通過Google Firebase Console這是不可能的。

我們必須要send our custom payload messages(數據或通知或兩者)分別使用firebase servers中的任何一個。

有關背景通知的詳細信息可在plugin's GitHub Page上找到。

我用過NodeJs firebase-admin。我跟着這些setup guidelines和這些steps for sending messages它爲我工作。

這是我工作的代碼(的NodeJS) -

var admin = require("firebase-admin"); 

var serviceAccount = require("pushnotificationapp-xxxxx.json"); //this is the service account details generated from server 

admin.initializeApp({ 
    credential: admin.credential.cert(serviceAccount), 
    databaseURL: "https://pushnotificationappxxxxx.firebaseio.com/" //your database URL here. 
}); 

var registrationToken = "eYjYT0_r8Hg:APA91bG0BqWbT7XXXXX....."; //registration token of the device 
var payload ={ 
    "data": { 
     "title": 'Test Push', 
     "message": 'Push number 1', 
     "info": 'super secret info', 
     "content-available": '1' //FOR CALLING ON NOTIFACATION FUNCTION EVEN IF THE APP IS IN BACKGROUND 
    } 
}; 

//send the notification or message 
admin.messaging().sendToDevice(registrationToken, payload) 
.then(function(response) { 
    console.log("Successfully sent message:", response); 
}) 
.catch(function(error) { 
    console.log("Error sending message:", error); 
}); 

UPDATE

我實現了相同的使用PHP的服務器了。 這是我使用HTTP POST到FCM發送通知工作PHP代碼服務器 -

<?php 

$url = 'https://fcm.googleapis.com/fcm/send'; 

$data = 
    array(
      "to" => "eYjYT0_r8Hg:APA91bG0BqWbT7XXXXX.....", 
      "data" => array(
       "title"=> 'Test Push', 
       "message"=> 'Push number 1', 
       "info"=> 'super secret info', 
       "content-available"=> '1') 

    ); 

// use key 'http' even if you send the request to https://... 
$options = array(
    'http' => array(
     'header' => array("Content-Type:application/json", 
     "Authorization:key=AAAA1HfFM64:APA91XXXX...."), //this is the server authorization key from project settings tab in your Firebase Project 
     'method' => 'POST', 
     'content' => json_encode($data) 
    ) 
); 

$context = stream_context_create($options); 
$result = file_get_contents($url, false, $context); 

if ($result === FALSE) { echo "Caught an error"; } 
else{ 
    echo $result; 
} 
?> 
0

我正在經歷同樣的問題,但我補充說:「內容可用」:「1」,但也是我的手機沒有收到任何後臺推送。

最後我發現,這是問題,我的手機這裏所說:https://github.com/phonegap/phonegap-plugin-push/blob/master/docs/PAYLOAD.md#huawei-and-xiaomi-phones

華爲和小米手機

這些手機有一個特別的怪癖,當應用程序被強制關閉,你將不再是能夠接收通知,直到應用程序重新啓動。爲了讓您收到後臺通知:

在您的華爲設備上,進入設置>受保護的應用程序>選中「我的應用程序」。 在您的小米上,確保您的手機具有爲您的應用啓用的「自動啓動」屬性。 在您的華碩確保您的手機具有爲您的應用程序啓用「自動啓動」屬性。

希望它能幫助別人。

相關問題