我目前正在使用Ionic 3,Cordova和Firebase构建推送通知应用程序,我在以下链接中找到了如何构建应用程序的在线资源:http://tphangout.com/ionic-2-sending-push-notifications-to-specific-devices-part-1/,我能够构建app.apk和将它安装在我的Android移动设备中。该应用程序似乎在我的设备中正常运行。我有一个问题,当我在我的cmd中键入Ionic服务时,我得到以下错误,“FCMPlugin未定义”,我尝试了不同的方法,但我无法找到解决方案,我也是新的离子和我不知道我做错了什么,有没有人遇到类似的问题?,我该如何解决这个问题?
我在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();
});
}
}
和我的家。
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');
})
}
}
表示感谢
答案 0 :(得分:0)
它不适用于浏览器,因为它需要Cordova,因此您需要始终检查platform.is(' cordova')方法,或者您只需使用typeof
if(this.platform.is('cordova')){
..... //then execute FCM methods
}
或者
If(typeof FCM != typeof 'undefined'){
.... //only then perform FCM operations
}