首页
学习
活动
专区
圈层
工具
发布
首页
学习
活动
专区
圈层
工具
社区首页 >问答首页 >在离子应用程序中未接收FCM消息数据。

在离子应用程序中未接收FCM消息数据。
EN

Stack Overflow用户
提问于 2018-06-23 01:58:27
回答 2查看 2.3K关注 0票数 0

我有一个简单的Ionic实现(Android)来接收来自FCM的消息。当从Firebase控制台发送消息时,通知到达并显示警报,但未显示消息数据。

这是代码(app.component.ts):

代码语言:javascript
代码运行次数:0
运行
复制
  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()的输出是:

代码语言:javascript
代码运行次数:0
运行
复制
> {wasTapped: false}
{"wasTapped": false}

如何获取消息数据?有什么想法吗?谢谢。

EN

回答 2

Stack Overflow用户

发布于 2020-02-03 20:31:37

要使数据具有信息,必须从服务器发送类似于此的信息

代码语言:javascript
代码运行次数:0
运行
复制
{"message":{ 
           "token":"bk3RNwTe3H0:CI2k_HHwgIpoDKCIZvvDMExUdFQ3P1...", 
           "notification":{ "title":"Portugal vs.Denmark", "body": 
                             "great match!" },
           "data" : { "Nick" : "Mario", "Room" : "PortugalVSDenmark" } } }

其中message.data是您想要发送的数据输出

票数 0
EN

Stack Overflow用户

发布于 2018-06-25 13:35:33

在this.fcm.onNotification().subscribe()函数中,可以得到data.title、data.description和data.wasTapped。使用data.title和data.description可以显示消息。

代码语言:javascript
代码运行次数:0
运行
复制
 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
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/50997326

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档