簡體   English   中英

使用FCM的Ionic 2推送通知

[英]Ionic 2 Push Notifications with FCM

我正在使用Ionic Native FCM在Android Ionic 2 App上實現推送通知

當我在前台收到通知時,它起作用,但是當我在后台收到通知時,如果單擊它,則什么也沒有發生。

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}})

當應用程序被終止或未運行時,我也無法收到通知。

您可以使用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));

您可以在應用的pushObject init使用forceShow:true選項,以顯示是否使用該應用的通知。

並且,一旦您單擊通知,該應用就會收到通知有效載荷,並且該應用的主頁設置為默認主頁。

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM