我目前正在使用FCM进行角度项目通知,但我的应用程序将不再构建。我现在已经阅读了有关该主题的所有内容,并在此后形成了我的代码,但我仍然遇到同样的错误。
构建模块3/3模块0 activeFailed on type {“filePath”:“C:/Users/SlyOtis/Documents/Time2Work/src/frontend/src/app/_services/firebase.service.ts”,“name” :“FCMService”,“members”:[]}有错误错误:在静态解析符号值时遇到错误。 EXP 不支持ression表单(原始.ts文件中的位置15:57),在C中解析符号FCMService:/Users/SlyOtis/Documents/Time2Work/src/frontend/src/app/_services/firebase.service.ts
ERROR in Error遇到静态解析符号值。不支持表达式表单(原始.ts文件中的位置15:57),在C中解析符号FCMService:/Users/SlyOtis/Documents/Time2Work/src/frontend/src/app/_services/firebase.service.ts
import {Inject, Injectable} from '@angular/core';
import {FirebaseApp} from 'angularfire2';
import * as firebase from 'firebase';
import {StorageService} from './storage.service';
import {Subject} from 'rxjs/Subject';
@Injectable()
export class FCMService {
private currentToken = '';
private readonly messaging: firebase.messaging.Messaging;
private readonly subject: Subject<any>;
constructor(@Inject(FirebaseApp) private firebaseApp: firebase.app.App,
private storage: StorageService) {
this.messaging = firebase.messaging(this.firebaseApp);
this.subject = new Subject();
this.messaging.onTokenRefresh(this.onTokenRefresh);
this.messaging.onMessage(this.onMessage);
const tmpToken = storage.get('fcm_token');
if (tmpToken) {
this.currentToken = <string>tmpToken;
} else {
// this.init();
}
}
public init() {
this.requestPermission();
this.getToken();
}
public requestToken() {
this.getToken();
}
public getObservableSubject(): Subject<any> {
return this.subject;
}
private onMessage(payload: any) {
console.log('Message received. ', payload);
// TODO:: Add message structur and observable.
this.subject.next(payload);
this.storage.putObject('msg', payload);
}
private onTokenRefresh() {
const token = this.messaging.getToken();
if (token !== null) {
token.then(refreshedToken => {
console.log('Token refreshed.');
this.sendTokenToServer(refreshedToken);
}).catch(function (err) {
console.log('Unable to retrieve refreshed token ', err);
});
} else {
console.log('Unable to retrieve refreshed token ');
}
}
private getToken() {
const token = this.messaging.getToken();
if (token !== null) {
token.then(currentToken => {
if (currentToken) {
this.sendTokenToServer(currentToken);
} else {
// Show permission request.
console.log('No Instance ID token available. Request permission to generate one.');
}
}).catch(function (err) {
console.log('An error occurred while retrieving token. ', err);
});
} else {
console.log('An error occurred while retrieving token. ');
}
}
// Send the Instance ID token your application server, so that it can:
// - send messages back to this app
// - subscribe/unsubscribe the token from topics
private sendTokenToServer(currentToken: any) {
console.log('Current token: ' + currentToken);
}
private requestPermission() {
console.log('Requesting permission...');
const permission = this.messaging.requestPermission();
if (permission !== null) {
permission.then(() => {
console.log('Notification permission granted.');
this.getToken();
}).catch(function (err) {
console.log('Unable to getObject permission to notify.', err);
});
} else {
console.log('Unable to getObject permission to notify.');
}
}
private deleteToken() {
const token = this.messaging.getToken();
if (token !== null) {
token.then((currentToken: string) => {
const deleted = this.messaging.deleteToken(currentToken);
if (deleted !== null) {
deleted.then(() => {
console.log('Token deleted.');
this.getToken();
})
.catch(function(err: any) {
console.log('Unable to delete token. ', err);
});
} else {
console.log('Unable to delete token. ');
}}).catch(function (err) {
console.log('Error retrieving Instance ID token. ', err);
});
} else {
console.log('Error retrieving Instance ID token. ');
}
}
}
// Give the service worker access to Firebase Messaging.
// Note that you can only use Firebase Messaging here, other Firebase libraries
// are not available in the service worker.
importScripts('https://www.gstatic.com/firebasejs/3.9.0/firebase-app.js');
importScripts('https://www.gstatic.com/firebasejs/3.9.0/firebase-messaging.js');
const firebaseConfig = {
apiKey: 'ÆÆÆÆÆÆÆÆÆÆÆÆÆÆÆÆÆÆÆÆÆÆÆÆÆÆÆÆÆÆÆÆ',
authDomain: 'ØØØØØØØØØØØØØØØØØØØØØØØØØ',
databaseURL: 'ÅÅÅÅÅÅÅÅÅÅÅÅÅÅÅÅÅÅÅÅÅÅ',
projectId: 'asdasdasdasdasd',
storageBucket: 'asdasdasdasdasdasdasd',
messagingSenderId: '420'
};
// Initialize the Firebase app in the service worker by passing in the
// messagingSenderId.
firebase.initializeApp(firebaseConfig);
// Retrieve an instance of Firebase Messaging so that it can handle background
// messages.
const messaging = firebase.messaging();
该项目适用于ng serve
但不适用于build
,对于我做错了什么的任何想法?