使用fcm插件在ionic3中推送通知

时间:2018-10-26 11:20:25

标签: firebase ionic-framework push-notification ionic3 cordova-plugin-fcm

我是ionic 3的新手,我已经在正在构建的应用程序上实现了fcm推送通知,并且当该应用程序运行时(在前台和后端),它都可以正常工作,但是当我尝试将推送通知发送给用户,当应用未运行时,我没有收到任何消息,因此有人可以为我提供一些有关此问题的帮助。

预先感谢。

 import { Component } from '@angular/core';
import { IonicPage, NavController, NavParams, ActionSheetController,Platform,AlertController } from 'ionic-angular';
import { AddeventPage } from '../addevent/addevent';
import { EventItem } from '../../models/event-item/event-item.interface';
import {AngularFireDatabase, AngularFireList} from 'angularfire2/database';
import {AuthService} from '../../services/auth'
import { Observable } from 'rxjs';
import { SigninPage } from '../signin/signin';
import {EventdetailsPage} from '../eventdetails/eventdetails';
import { FCM } from '@ionic-native/fcm';
import  firebase  from "firebase";
import { Push, PushObject, PushOptions } from '@ionic-native/push';



/**
 * Generated class for the EventlistPage page.
 *
 * See https://ionicframework.com/docs/components/#navigation for more info on
 * Ionic pages and navigation.
 */

@IonicPage()
@Component({
  selector: 'page-eventlist',
  templateUrl: 'eventlist.html',
})
export class EventlistPage {
  username: String;

  eventListRef$ : Observable<any[]>

  constructor(
    private authService:AuthService,
    private database: AngularFireDatabase,
    public navCtrl: NavController,
    private actionSheetCtrl: ActionSheetController,
     public navParams: NavParams,
     public platform: Platform,
     private fcm: FCM,
     private push: Push,
     public alertCtrl: AlertController


     ) {

    this.eventListRef$ = this.database.list('eventlist').valueChanges();
    this.initFCM();







  }

  onSelectEvent(){
    this.actionSheetCtrl.create()
  }
  goToAddEvent() {
    this.navCtrl.push(AddeventPage);
  }
  cardtapped(event){
    this.navCtrl.push(EventdetailsPage, {
      date:event.date,
      time:event.time,


  });
  }

  initFCM() {
    this.fcm.subscribeToTopic('marketing');

    // if(typeof(this.fcm) !== "undefined"){

    this.fcm.getToken().then(token=>{

    alert(token);
    console.log(token);
    firebase.database().ref('/volunteers/'+this.authService.getActiveUser().uid).update({
      fcmtoken:token
    })

    });

    //this.fcm.subscribeToTopic('/topics/all');

    this.fcm.onNotification().subscribe(data=>{
    if(data.wasTapped){
    alert("Received in background")
    var Str1 = JSON.stringify(data.value)
    alert(Str1)
    } else {

     var Str2 = JSON.stringify(data.value)
     alert("Received in foreground"+Str2);
    };
    });

    this.fcm.onTokenRefresh().subscribe(token=>{

    alert(token);

    });
    this.fcm.unsubscribeFromTopic('marketing');
  }
}

0 个答案:

没有答案