我有一个简单的Ionic实现(Android)来接收来自FCM的消息。当从Firebase控制台发送消息时,通知到达并显示警报,但未显示消息数据。
这是代码(app.component.ts):
constructor(platform: Platform, statusBar: StatusBar, splashScreen: SplashScreen, public fcm: FCM, private alertCtrl: AlertController) {
this.fcm.subscribeToTopic('all');
platform.ready().then(() => {
this.fcm.getToken().then(token => {
console.log(token);
let alert = this.alertCtrl.create({
title: '¡New token!',
message: token,
buttons: [
{
text: 'Cancel',
role: 'cancel',
handler: () => {
console.log('Cancel');
}
},
{
text: 'OK',
handler: () => {
console.log('OK');
this.navCtrl.push('DetailPage');
}
}
]
});
alert.present();
});
this.fcm.onNotification().subscribe(data => {
alert('message received');
if(data.wasTapped) {
console.log(data);
console.info("Received in background");
} else {
// console.log(data);
console.info("Received in foreground");
};
});
例如,当消息从Firebase控制台发送时:
将显示应用程序中的警报(“消息接收”),但console.log(数据)到fcm.onNotification().subscribe()的输出是:
> {wasTapped: false}
{"wasTapped": false}
如何获取消息数据?有什么想法吗?谢谢。
发布于 2020-02-03 20:31:37
要使数据具有信息,必须从服务器发送类似于此的信息
{"message":{
"token":"bk3RNwTe3H0:CI2k_HHwgIpoDKCIZvvDMExUdFQ3P1...",
"notification":{ "title":"Portugal vs.Denmark", "body":
"great match!" },
"data" : { "Nick" : "Mario", "Room" : "PortugalVSDenmark" } } }
其中message.data是您想要发送的数据输出
发布于 2018-06-25 13:35:33
在this.fcm.onNotification().subscribe()函数中,可以得到data.title、data.description和data.wasTapped。使用data.title和data.description可以显示消息。
this.fcm.onNotification().subscribe(data => {
if (data.wasTapped) {
console.log("Received in background");
} else {
// alert when push notification in foreground
let confirmAlert = this.alertCtrl.create({
title: data.title,
subTitle: data.description,
buttons: [{
text: 'Ignore',
role: 'cancel'
}, {
text: 'Ok',
handler: () => {
//TODO: Your logic here
}
}]
});
confirmAlert.present();
console.log("Received in foreground");
}
});
希望你能找到合适的答案。
https://stackoverflow.com/questions/50997326
复制相似问题