Skip to content

firebase messaging error #1682

@piuskamil

Description

@piuskamil

Version info
Angular:6.0.1

Firebase:5.0.3

AngularFire:5.0.0-rc.10

iOS safari

Problem:
when this error persist app crash and i can see only white screen

"{
  "code": "messaging/unsupported-browser",
  "message": "Messaging: This browser doesn't support the API's required to use the firebase SDK. (messaging/unsupported-browser).",
  "name": "FirebaseError"
}"

It works well on AngularFIre 5.0.0.rc6 and firebase 4.12.1

app.component.ts

this.msgService.getPermission();
this.msgService.receiveMessage();
this.msgService.refreshToken();

this.msgService.currentMessage.subscribe(d => {
      console.log(d);
    });`

service
`
import { Injectable } from '@angular/core';
import * as firebase from 'firebase';
import { BehaviorSubject, of } from 'rxjs';
import { Action, AngularFirestore } from 'angularfire2/firestore';
import { AngularFireAuth } from 'angularfire2/auth';
import { HttpClient } from '@angular/common/http';
import { environment } from '../../../environments/environment';
import { filter, switchMap, take } from 'rxjs/operators';

@Injectable()
export class MessagingService {
  private messaging: firebase.messaging.Messaging = firebase.messaging();
  public currentMessage: BehaviorSubject<any> = new BehaviorSubject(null);

  constructor( private modalService: ModalService, private db: AngularFirestore, private afAuth: AngularFireAuth, private http: HttpClient ) {
  }

  public updateToken( token: string ): void {
    const addToken = ( actions: Action<firebase.firestore.DocumentSnapshot>, currentTokens: string[] ) => {
      actions.payload.ref
        .set({
          token: currentTokens,
        })
        .then(() => {
        });
    };

    this.afAuth.authState
      .pipe(
        switchMap(user => {
          if (!user) {
            return of(null);
          }
          return this.db
            .collection('fcmTokens/')
            .doc(user.uid)
            .snapshotChanges();
        }),
        filter(( action: Action<firebase.firestore.DocumentSnapshot> ) => !!action)
      )

      .subscribe(( actions: Action<firebase.firestore.DocumentSnapshot> ) => {
        if (!actions.payload.data()) {
          addToken(actions, [ token ]);
          return;
        }
        const currentTokens: string[] = actions.payload.data().token;
        const index: number = currentTokens.findIndex(( currentToken: string ) => currentToken === token);
        if (index === -1) {
          currentTokens.push(token);
          addToken(actions, currentTokens);
        }

      });
  }

  public refreshToken(): void {
    this.messaging.onTokenRefresh(() => {
      this.messaging.getToken().then(( refreshedToken: string ) => {
        console.log('refreshedToken: ', refreshedToken);
        this.updateToken(refreshedToken);
      });
    });
  }

  public getPermission(): void {
    if ('Notification' in window && Notification[ 'permission' ] === 'default') {
      this.showPermission();
    } else if ('Notification' in window && Notification[ 'permission' ] === 'granted') {
      this.showPermission();
    }

  }

  private showPermission(): void {
    this.messaging
      .requestPermission()
      .then(() => {
        console.log('Notification permission granted.');
        return this.messaging.getToken();
      })
      .then(( token: string ) => {
        this.updateToken(token);
      })
      .catch(err => {
        console.log('Unable to get permission to notify.', err);
      });
  }

  public receiveMessage(): void {
    this.messaging.onMessage(payload => {
      console.log('Message received. ', payload);
      this.currentMessage.next(payload);
    });
  }
}

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions