javascript - Firebase success notification doesn't appear in browser -
i'm trying implement browser push notifications using fcm. project based on angular 2.
in app.module.ts i've registered firebase application.
import { angularfiremodule, firebaseappconfig } "angularfire2"; const firebaseconfig = { apikey: "test", authdomain: "test", databaseurl: "test", storagebucket: "", messagingsenderid: "328947432649" }; imports: [ angularfiremodule.initializeapp(firebaseconfig) ], i've created firebase-messaging-sw.js
importscripts('https://www.gstatic.com/firebasejs/3.7.2/firebase-app.js'); importscripts('https://www.gstatic.com/firebasejs/3.7.2/firebase-messaging.js'); firebase.initializeapp({ 'messagingsenderid': 'test' }); firebase.messaging(); self.addeventlistener('notificationclick', function (event) { const target = event.notification.data.click_action || '/'; event.notification.close(); // looks see if current open , focuses if event.waituntil(clients.matchall({ type: 'window', includeuncontrolled: true }).then(function (clientlist) { // clientlist empty?! (var = 0; < clientlist.length; i++) { var client = clientlist[i]; if (client.url == target && 'focus' in client) { return client.focus(); } } return clients.openwindow(target); })); }); i've created messagingservice
import { injectable, inject } '@angular/core'; import { http, response, headers, requestoptions } "@angular/http"; import 'rxjs/add/operator/map'; import 'rxjs/add/operator/catch'; import { firebaseapp } "angularfire2"; import * firebase 'firebase'; import { httpservice } "app/services/http.service"; import { observable } "rxjs/rx"; @injectable() export class messagingservice { private currenttoken: string = ""; private _messaging: firebase.messaging.messaging; constructor(private httpservice: httpservice, @inject(firebaseapp) private _firebaseapp: firebase.app.app) { this.currenttoken = json.parse(localstorage.getitem('firebase_token')); this._messaging = firebase.messaging(this._firebaseapp); } getnewtoken() { this.requestpermission(); } private requestpermission() { this._messaging.requestpermission() .then(() => { this.gettoken(); }) .catch(err => { console.log('unable permission notify.', err); }); } private gettoken() { this._messaging.gettoken().then(token => { if (this.currenttoken === token) { this._messaging.deletetoken(token).then(() => { this.deletetokenfromserver(token); this._messaging.gettoken().then( refreshedtoken => { this.currenttoken = refreshedtoken; this.savetoken(this.currenttoken); } ).catch(err => { console.error('msg token error:', err); }); }); } else { this.currenttoken = token; this.savetoken(this.currenttoken); } }); } private savetoken(token: string) { this.currenttoken = token; localstorage.setitem('firebase_token', json.stringify(this.currenttoken)); this.sendtokentoserver(token); } private sendtokentoserver(token: string) { const body = { browsertoken: token } this.httpservice.post(`/pushbrowsernotification/addbrowsertoken`, body).subscribe() } private deletetokenfromserver(token: string) { const body = { browsertoken: token } this.httpservice.post(`/pushbrowsernotification/removebrowsertoken`, body).subscribe(); } } when i'm logging site, i'm calling getnewtoken() method in component. after allow notification on site. i've gotten fire-base token , working service worker.

after i'm trying send notification using postman. @ first works well. i'm getting notifications. after waiting of order of 10 seconds, click send button , nothing happens. these notifications don't work anymore. when change icon, starts work again time. what's wrong implementation? 
Comments
Post a Comment