【问题标题】:FCMPlugin is not defined in Ionic 3Ionic 3 中未定义 FCMPlugin
【发布时间】:2018-01-25 08:11:09
【问题描述】:

我目前正在使用 Ionic 3、Cordova 和 Firebase 构建推送通知应用程序,我在以下链接中找到了有关如何构建应用程序的在线资源:http://tphangout.com/ionic-2-sending-push-notifications-to-specific-devices-part-1/,我能够构建 app.apk 并安装它在我的安卓移动设备中。该应用程序似乎在我的设备中正常运行。但是我有一个问题,当我在我的 cmd 中键入 Ionic serve 时,我收到以下错误,“FCMPlugin 未定义”,我尝试了不同的方法,但我找不到解决方案,我对 ionic 和我不知道我是否做错了什么,有没有人遇到过类似的问题?,我该如何解决这个问题?

我在我的 app.compoment.ts 中声明它

import { Component } from '@angular/core';
import { Platform } from 'ionic-angular';
import { StatusBar } from '@ionic-native/status-bar';
import { SplashScreen } from '@ionic-native/splash-screen';

import { LoginPage } from '../pages/login/login';




declare var FCMPlugin;

@Component({
  templateUrl: 'app.html'
})
@Component({
  templateUrl: 'app.html'
})
export class MyApp {
  rootPage:any = LoginPage;

  constructor(platform: Platform, statusBar: StatusBar, splashScreen: SplashScreen) {



FCMPlugin.getToken(
  (t) => {
    console.log(t);
  },
  (e) => {
    console.log(e);
  }
);

FCMPlugin.onNotification(
  (data) => {
    console.log(data);
  },
  (e) => {
    console.log(e);
  }
);

    platform.ready().then(() => {
      // Okay, so the platform is ready and our plugins are available.
      // Here you can do any higher level native things you might need.
      statusBar.styleDefault();
      splashScreen.hide();
    });
  }
}

在我家.ts

import { Component } from '@angular/core';
import { NavController } from 'ionic-angular';
import { AngularFireDatabase } from 'angularfire2/database';


import firebase from 'firebase';

declare var FCMPlugin;

@Component({
  selector: 'page-home',
  templateUrl: 'home.html'
})
export class HomePage {
  firestore = firebase.database().ref('/pushtokens');
  firemsg = firebase.database().ref('/messages');
  constructor(public navCtrl: NavController, public afd: AngularFireDatabase) {
    this.tokensetup().then((token) => {
      this.storetoken(token);
    })
  }



  ionViewDidLoad() {
    FCMPlugin.onNotification(function(data){
    if(data.wasTapped){
      //Notification was received on device tray and tapped by the user.
      alert( JSON.stringify(data) );
    }else{
      //Notification was received in foreground. Maybe the user needs to be notified.
      alert( JSON.stringify(data) );
    }
    });

FCMPlugin.onTokenRefresh(function(token){
    alert( token );
});    
  }

  tokensetup() {
    var promise = new Promise((resolve, reject) => {
      FCMPlugin.getToken(function(token){
    resolve(token);
      }, (err) => {
        reject(err);
});
    })
    return promise;
  }

  storetoken(t) {
    this.afd.list(this.firestore).push({
      uid: firebase.auth().currentUser.uid,
      devtoken: t

    }).then(() => {
      alert('Token stored');
      })

    this.afd.list(this.firemsg).push({
      sendername: firebase.auth().currentUser.displayName,
      message: 'hello'
    }).then(() => {
      alert('Message stored');
      })
}

}

感谢您的问候

【问题讨论】:

  • 这样做。FCMplugin 而不是 FCMPlugin。

标签: android ios cordova firebase firebase-cloud-messaging


【解决方案1】:

它不适用于浏览器,因为它需要 Cordova,因此您需要始终检查 platform.is('cordova') 方法,或者您可以简单地使用 typeof

if(this.platform.is('cordova')){
 .....        //then execute FCM methods 
}

或者

If(typeof FCM != typeof 'undefined'){
 .... //only then perform FCM operations 
} 

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2017-09-18
    • 1970-01-01
    • 2017-12-10
    • 2018-07-12
    • 1970-01-01
    • 1970-01-01
    • 2017-11-14
    相关资源
    最近更新 更多