首页 > 解决方案 > 如何使用 Ionic 捕获移动通知?

问题描述

有没有办法使用 Ionic 或 Cordova 插件捕获在移动设备上收到的通知?

标签: cordovaionic-frameworkcordova-pluginsionic4

解决方案


import { FCM } from '@ionic-native/fcm/ngx';

constructor(private fcm: FCM) {}

...

this.fcm.subscribeToTopic('marketing');

this.fcm.getToken().then(token => {
  backend.registerToken(token);
});

this.fcm.onNotification().subscribe(data => {
  if(data.wasTapped){
    console.log("Received in background");
  } else {
    console.log("Received in foreground");
  };
});

this.fcm.onTokenRefresh().subscribe(token => {
  backend.registerToken(token);
});

this.fcm.unsubscribeFromTopic('marketing');

PS此处的详细程序:https ://ionicframework.com/docs/native/fcm


推荐阅读