使用FCM {I 44推送通知

时间:2018-02-05 02:57:23

标签: android firebase ionic2 firebase-cloud-messaging cordova-plugin-fcm

我在我的Android Ionic 2 App上使用Ionic Native FCM

实施推送通知

当我在前台收到通知时,它可以正常工作,但是当我在后台收到通知并且点击它时,没有任何反应。

app.component.ts

firebaseInit(){
  //Firebase
  this.fcm.subscribeToTopic('all');

  this.fcm.getToken()
    .then(token => {
      console.log(token);
      this.nativeStorage.setItem('fcm-token', token);
  });

  this.fcm.onNotification().subscribe(
    data => {
      console.log("NOTIF DATA: " + JSON.stringify(data));
      if(data.wasTapped){
        this.nav.push(MemoViewPage, {memo: {_id: data.memo_id}})
        console.info('Received in bg')
      }else{
        let alert = this.alertCtrl.create({
          title: data.subject,
          message: "New memorandum",
          buttons: [
            {
              text: 'Ignore',
              role: 'cancel'
            },
            {
              text: 'View',
              handler: () => {
                this.nav.push(MemoViewPage, {memo: {_id: data.memo_id}})
              }
            }
          ]
        });

        alert.present();
        console.info('Received in fg')
      }
  });

  this.fcm.onTokenRefresh()
    .subscribe(token => {
      console.log(token);
  })
}

单击系统托盘中的通知后,if(data.wasTapped)条件不会消失。

修改

应用程序会打开但仅在主页中打开,而不是我设置的指定页面this.nav.push(MemoViewPage, {memo: {_id: data.memo_id}})

当应用程序被终止或未运行时,我也无法收到通知。

1 个答案:

答案 0 :(得分:0)

您可以使用push插件代替FCM

this.push.createChannel({
 id: "testchannel1",
 description: "My first test channel",
 importance: 3
}).then(() => console.log('Channel created'));

然后您可以使用pushObjects来指定声音,离子等通知的需求。

const options: PushOptions = {
   android: {},
   ios: {
       alert: 'true',
       badge: true,
       sound: 'false'
   },
   windows: {},
   browser: {
       pushServiceURL: 'http://push.api.phonegap.com/v1/push'
   }
};

之后,无论您是否使用该应用程序,都可以轻松收到通知

const pushObject: PushObject = this.push.init(options);

pushObject.on('registration').subscribe((registration: any) => this.nativeStorage.setItem('fcm-token', token));

pushObject.on('notification').subscribe((notification: any) => console.log('Received a notification', notification));

您可以在应用的forceShow:true中使用pushObject init选项来显示您是否使用该应用的通知。

点击通知后,应用会收到通知有效内容,并将应用主页设置为默认值。