Android 使用cordova的FireBase推送通知
我正在尝试使用cordova实现Firbase推送通知。我在这里使用最新fcm插件规范的代码: 我可以拿到注册令牌。然后,我尝试使用该令牌从Firebase测试通知模块发送通知。每次在我的设备上运行应用程序时,我都会收到警报- “Msg:onNotification回调已成功注册:确定” 它位于FCMPlugin.onNotification事件的第二个函数内 但是没有调用第一个函数[我想在其中获取通知]。 我不知道我在哪里犯了错误。以下是我在onDeviceReady中的代码:Android 使用cordova的FireBase推送通知,android,cordova,push-notification,Android,Cordova,Push Notification,我正在尝试使用cordova实现Firbase推送通知。我在这里使用最新fcm插件规范的代码: 我可以拿到注册令牌。然后,我尝试使用该令牌从Firebase测试通知模块发送通知。每次在我的设备上运行应用程序时,我都会收到警报- “Msg:onNotification回调已成功注册:确定” 它位于FCMPlugin.onNotification事件的第二个函数内 但是没有调用第一个函数[我想在其中获取通知]。 我不知道我在哪里犯了错误。以下是我在onDeviceReady中的代码: functio
function onDeviceReady() {
// Handle the Cordova pause and resume events
document.addEventListener( 'pause', onPause.bind( this ), false );
document.addEventListener( 'resume', onResume.bind( this ), false );
// TODO: Cordova has been loaded. Perform any initialization that requires Cordova here.
var parentElement = document.getElementById('deviceready');
var listeningElement = parentElement.querySelector('.listening');
var receivedElement = parentElement.querySelector('.received');
listeningElement.setAttribute('style', 'display:none;');
receivedElement.setAttribute('style', 'display:block;');
//=========================
FCMPlugin.getToken(
function (token) {
alert("Token: " + token);
cordova.plugins.email.open({
to: 'sharif@nascenia.com',
subject: 'Greetings',
body: token
});
},
function (err) {
alert("Error: " + 'error retrieving token: ' + err);
}
);
FCMPlugin.onNotification(
function (data) {
alert("Notify: " + JSON.stringify(data));
if (data.wasTapped) {
//Notification was received on device tray and tapped by the user.
alert("Wrapped Notify: " + JSON.stringify(data));
} else {
//Notification was received in foreground. Maybe the user needs to be notified.
alert("Notify: " + JSON.stringify(data));
}
},
function (msg) {
alert("Msg: " + 'onNotification callback successfully registered: ' + msg.Notification);
},
function (err) {
alert("Error: " + 'Error registering onNotification callback: ' + err);
}
);
};
您缺少在onNotification功能之前订阅主题的功能,如下所示:
FCMPlugin.subscribeToTopic('topic');
确保向REST API的有效负载中添加“单击\u操作”:“FCM\u插件\u活动”
。这对于Android来说必须存在。如果此功能不可用,您将无法从点击的通知接收数据(或听到声音)
请参阅cordova插件fcm文档中的REST API有效负载示例:
//POST: https://fcm.googleapis.com/fcm/send
//HEADER: Content-Type: application/json
//HEADER: Authorization: key=AIzaSy*******************
{
"notification":{
"title":"Notification title",
"body":"Notification body",
"sound":"default",
"click_action":"FCM_PLUGIN_ACTIVITY", // <<<<<<< Must be present for Android
"icon":"fcm_push_icon"
},
"data":{
"param1":"value1",
"param2":"value2"
},
"to":"/topics/topicExample",
"priority":"high",
"restricted_package_name":""
}
//帖子:https://fcm.googleapis.com/fcm/send
//标题:内容类型:application/json
//标题:授权:key=AIzaSy*******************
{
“通知”:{
“标题”:“通知标题”,
“正文”:“通知正文”,
“声音”:“默认值”,
“单击操作”:“FCM插件活动”,//嘿,你有什么解决方案吗?我的FCM通知成功发送,但我的问题是,当我点击通知时,我想得到提醒,而且我使用的是与你使用的插件相同的插件。如果你解决了,请告诉我。@KAUSHAL:我已更改了插件。以下插件对我很有效:我对firebase通知da有疑问ta总是显示false我不知道我通过firebase控制台发送的y--ionic3