【问题标题】:The FCM message data is not received in the ionic application离子应用程序中未收到 FCM 消息数据
【发布时间】:2018-12-02 11:39:30
【问题描述】:

我有一个简单的 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 控制台发送时:

  • 消息文本:这是一个测试!
  • 可选标签:新消息。

显示应用程序中的alert('message received'),但是console.log(data)到fcm.onNotification().subscribe()的输出是:

> {wasTapped: false}
{"wasTapped": false}

如何获取消息数据?任何想法? 谢谢。

【问题讨论】:

  • 但是当你在前台收到console.log()时有注释

标签: firebase ionic-framework ionic2 firebase-cloud-messaging cordova-plugin-fcm


【解决方案1】:

为了让数据有信息,你必须从你的服务器发送这样的信息

{"message":{ 
           "token":"bk3RNwTe3H0:CI2k_HHwgIpoDKCIZvvDMExUdFQ3P1...", 
           "notification":{ "title":"Portugal vs.Denmark", "body": 
                             "great match!" },
           "data" : { "Nick" : "Mario", "Room" : "PortugalVSDenmark" } } }

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

【讨论】:

    【解决方案2】:

    在 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");
      }
    });
    

    希望你能找到合适的答案。

    【讨论】:

      猜你喜欢
      • 2020-01-14
      • 1970-01-01
      • 2017-05-31
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2019-07-24
      • 2017-12-17
      相关资源
      最近更新 更多