我使用phonegap-plugin-push在Cordova应用程序中接收通知。我正在使用android Marsh-mellow进行测试。
当应用在后台时,我希望查看并存储通过 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)
经过2天的研究,我找到了答案。我们必须设置"内容可用"在数据字段中为1。如果app在后台,它不会调用通知块。
目前无法通过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);
});
更新:
我也使用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;
}
?>
答案 1 :(得分:1)
我正在经历同样的问题,但我添加了&#34;内容可用&#34;:&#39; 1&#39;但是我的手机没有接到任何后台推送。
最后我发现这是我手机上的问题:https://github.com/phonegap/phonegap-plugin-push/blob/master/docs/PAYLOAD.md#huawei-and-xiaomi-phones
华为和小米手机
这些手机有一个特别的怪癖,当应用程序强制关闭时,您将无法再重新启动应用程序之前接收通知。为了让您收到背景通知:
在您的华为设备上,转到设置&gt;受保护的应用&gt;检查&#34;我的应用&#34;哪里。 在你的小米上确保你的手机有&#34;自动启动&#34;为您的应用启用了属性。 在您的华硕上确保您的手机具有&#34;自动启动&#34;为您的应用启用了该属性。
希望它会帮助某人。