我有一个简单的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(data)在fcm.onNotification()。subscribe()中的输出为:
> {wasTapped: false}
{"wasTapped": false}
如何获取消息数据?任何想法? 谢谢。
答案 0 :(得分:0)
在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");
}
});
希望您找到合适的答案。
答案 1 :(得分:0)
要使数据具有信息,您必须从服务器发送这样的信息
{"message":{
"token":"bk3RNwTe3H0:CI2k_HHwgIpoDKCIZvvDMExUdFQ3P1...",
"notification":{ "title":"Portugal vs.Denmark", "body":
"great match!" },
"data" : { "Nick" : "Mario", "Room" : "PortugalVSDenmark" } } }
其中message.data是您要发送的数据输出