我正在開發Angular 2 Web應用程序,並且想要註冊Firebase消息並獲取令牌。Angular 2和FireBase消息 - 無法註冊
我正在爲Angular 2使用angularfire2。我使用Angular ng服務本地開發環境。 可惜的是瀏覽器控制檯顯示我下面的錯誤:
V browserErrorMessage : "Failed to register a ServiceWorker: A bad HTTP response code (404) was received when fetching the script." code : "messaging/failed-serviceworker-registration" message : "Messaging: We are unable to register the default service worker. Failed to register a ServiceWorker: A bad HTTP response code (404) was received when fetching the script. (messaging/failed-serviceworker-registration)." stack : "FirebaseError: Messaging: We are unable to register the default service worker. Failed to register a ServiceWorker: A bad HTTP response code (404) was received when fetching the script. (messaging/failed-serviceworker-registration).↵ at http://localhost:4200/vendor.bundle.js:96468:225 ↵ at ZoneDelegate.invoke (http://localhost:4200/vendor.bundle.js:102953:26)↵ at Object.onInvoke (http://localhost:4200/vendor.bundle.js:33052:37)↵ at ZoneDelegate.invoke (http://localhost:4200/vendor.bundle.js:102952:32)↵ at Zone.run (http://localhost:4200/vendor.bundle.js:102824:43)↵ at http://localhost:4200/vendor.bundle.js:103246:57 ↵ at ZoneDelegate.invokeTask (http://localhost:4200/vendor.bundle.js:102986:35)↵ at Object.onInvokeTask (http://localhost:4200/vendor.bundle.js:33043:37)↵ at ZoneDelegate.invokeTask (http://localhost:4200/vendor.bundle.js:102985:40)↵ at Zone.runTask (http://localhost:4200/vendor.bundle.js:102862:47)↵ at drainMicroTaskQueue (http://localhost:4200/vendor.bundle.js:103144:35)" proto : Error
下面是我服務的代碼。
import { Inject, Injectable } from "@angular/core";
import { FirebaseApp } from "angularfire2";
import * as firebase from 'firebase';
@Injectable()
export class FirebaseMessagingService {
private messaging: firebase.messaging.Messaging;
constructor(@Inject(FirebaseApp) private firebaseApp: firebase.app.App) {
console.log("FirebaseMessagingService init...");
this.messaging = firebase.messaging(this.firebaseApp);
}
init() {
this.messaging.requestPermission()
.then(() => {
this.onFirebaseInitialized();
})
.catch((error) => {
console.log("FirebaseMessagingService requestPermission error:" + error);
});
}
private onFirebaseInitialized() {
console.log("FirebaseMessagingService initialized");
/*this.messaging.setBackgroundMessageHandler(message => {
})*/
this.getToken();
// Callback fired if Instance ID token is updated.
this.messaging.onTokenRefresh(() => {
this.getToken();
});
this.messaging.onMessage((payload) => {
console.log("Message received. ", payload);
// ...
});
}
getToken() {
this.messaging.getToken()
.then((currentToken) => {
console.log('getToken() received');
if (currentToken) {
this.sendTokenToServer(currentToken);
this.updateUIForPushEnabled(currentToken);
} else {
// Show permission request.
console.log('No Instance ID token available. Request permission to generate one.');
// Show permission UI.
this.updateUIForPushPermissionRequired();
this.setTokenSentToServer(false);
}
})
.catch(function (err) {
console.log('An error occurred while retrieving token. ', err);
//this.showToken('Error retrieving Instance ID token. ', err);
//this.setTokenSentToServer(false);
});
}
sendTokenToServer(token) {
}
updateUIForPushEnabled(token) {
}
updateUIForPushPermissionRequired() {
}
setTokenSentToServer(isSent: boolean) {
}
}