pro*_*ats 2 android push-notification cordova firebase phonegap-pushplugin
我正在使用phonegap-plugin-push来接收Cordova 应用程序中的通知。我正在 android Marsh-mellow 上部署进行测试。
我想在应用程序处于后台时查看并存储通过Firebase 控制台收到的通知内容。
这是我的代码 -
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);
});
}
Run Code Online (Sandbox Code Playgroud)
当应用程序位于前台(在屏幕上)时,我可以正确接收内容(标题、消息、数据等),并且可以直接在应用程序中的警报框中看到它们。
但是当应用程序在后台(不是在屏幕上而是在后台运行)时,我会在通知区域收到通知。当我点击收到的通知时,它会将我重定向到应用程序中最后打开的页面。
该函数push.on('notification', function(data) {}未被调用。不显示任何警报消息。有人可以帮助我如何访问通知消息和数据吗?
经过两天的研究,我找到了答案。我们必须在数据字段中将“content-available”设置为 1。否则,如果应用程序在后台,它不会调用通知块。
目前还无法通过Google Firebase Console实现这一点。
我们必须使用任一Firebase 服务器单独发送自定义有效负载消息(数据或通知或两者) 。
详细信息可以在插件的 GitHub 页面上的后台通知中找到。
我使用 NodeJs firebase-admin。我遵循了这些设置指南和发送消息的步骤,它对我有用。
这是我的工作代码(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);
});
Run Code Online (Sandbox Code Playgroud)
更新:
我也使用 php 服务器实现了相同的功能。这是我的工作 php 代码,用于使用HTTP POST向 FCM 服务器发送通知 -
<?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;
}
?>
Run Code Online (Sandbox Code Playgroud)
| 归档时间: |
|
| 查看次数: |
5722 次 |
| 最近记录: |