I am developing Angular 2 web app and want to register Firebase Messaging and obtain token.
I am using angularfire2 for Angular 2. I use Angular ng serve local development environment. Unfortunatelly the browser console shows me below errors:
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
Below is the code of my service.
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) {
}
}