首页 > 解决方案 > ionic 3 - 在 Ionic 上接收推送通知不起作用

问题描述

有人在 ionic 3 上处理过通知吗?我正在尝试创建一些存储 fcm 通知的应用程序,我正在尝试大多数教程,但似乎没有任何效果。当我收到通知并点击它时,我无法收到消息。

这是我尝试过的一些代码

initializeApp() {
    this.platform.ready().then(() => {
      // START
      this.fcm.getToken().then(token => {
        console.log(token);
        alert('token: '+token);
      });
      this.fcm.onTokenRefresh().subscribe(token => {
        console.log(token);
        alert('refresh token: '+token);
      });
      this.fcm.onNotification().subscribe(data => {
        alert('data: '+data);
        if(data.wasTapped){
          console.log("Received in background " + JSON.stringify(data) );
          alert("Received in background " + JSON.stringify(data) );
          this.nav.setRoot(InboxnotifPage, {data: data});
        } else {
          console.log("Received in foreground" + JSON.stringify(data) );
          alert("Received in foreground" + JSON.stringify(data) );
          this.nav.push(InboxnotifPage, {data: data});
        };
      });
      // END
    }
}

标签: firebaseionic-frameworkionic3cordova-plugin-fcm

解决方案


您必须在项目构建根目录中添加 .plist 文件才能与您的设备通信

this.fcm.onNotification().subscribe(data => {
                if (data.wasTapped) {
                  alert('');
                } else {
                  console.log("Received in foreground");
                  // this.presentToast("Received in foreground");
                }
              });
              this.getToken();
              this.fcm.onTokenRefresh().subscribe(token => {
                this.rest.globalToken = token;
              });
            });

遵循原始文档,请首先检查来自https://console.firebase.google.com的通知,然后将令牌传递给 rest Api


推荐阅读