Skip to content

Not receiving FCM messages #1904

@BradyShober

Description

@BradyShober

I have tried following the documentation in order to set up browser notifications for the application my team is currently developing. While I am able to obtain an FCM registration token, when I try to send a notification it does not appear to be received by the application. We are using the default firebase-messaging-sw.js

Version info

Ionic 3.9.2
Angular 5.2.10
Firebase ^5.5.1
AngularFire ^5.0.2

Browsers tested: Chrome 69.0.3497.100, Firefox 63.0b10

How to reproduce these conditions

Create a new Ionic project and install required packages.

app.module.ts

import { BrowserModule } from '@angular/platform-browser';
import { ErrorHandler, NgModule } from '@angular/core';
import { IonicApp, IonicErrorHandler, IonicModule } from 'ionic-angular';
import { SplashScreen } from '@ionic-native/splash-screen';
import { StatusBar } from '@ionic-native/status-bar';

import { MyApp } from './app.component';
import { HomePage } from '../pages/home/home';
import { FcmProvider } from '../providers/fcm/fcm';
import { NotificationProvider } from '../providers/fcm/notifications';

import * as firebase from 'firebase';

import { AngularFireModule } from '@angular/fire';

import { AngularFireMessagingModule } from '@angular/fire/messaging';


const config = {
  //Firebase configuration information
}; 

firebase.initializeApp(config);

@NgModule({
  declarations: [
    MyApp,
    HomePage
  ],
  imports: [
    BrowserModule,
    IonicModule.forRoot(MyApp),
    AngularFireModule.initializeApp(config),
    AngularFireMessagingModule
  ],
  bootstrap: [IonicApp],
  entryComponents: [
    MyApp,
    HomePage
  ],
  providers: [
    StatusBar,
    SplashScreen,
    {provide: ErrorHandler, useClass: IonicErrorHandler},
    FcmProvider,
    NotificationProvider
  ]
})
export class AppModule {}

fcm.ts provider

import { Injectable } from '@angular/core';
import { AngularFireMessaging } from '@angular/fire/messaging';
import { Platform } from 'ionic-angular';
import { mergeMapTo } from 'rxjs/operators';
import { Observable } from 'rxjs-compat';


@Injectable()
export class FcmProvider {


  constructor(private platform: Platform, private afMessaging: AngularFireMessaging) {

  }

  requestPermission(){
    return this.afMessaging.requestPermission.pipe(mergeMapTo(this.afMessaging.tokenChanges));
  }

  listen(){
    console.log("listening for messages");
    return this.afMessaging.messages;
  }

}

notification.ts provider

import { Injectable } from '@angular/core';
import { FcmProvider } from './fcm';
import { Platform } from 'ionic-angular';
import { AlertController } from 'ionic-angular';

@Injectable()
export class NotificationProvider {

    constructor(private platform: Platform, private fcm: FcmProvider, private alertCtrl: AlertController){}

    initializeNotifications(){

        this.fcm.requestPermission()
            .subscribe(token =>{
              console.log(token);
              this.fcm.listen()
                .subscribe(message => {
                console.log(message);
                this.notificationOpen(message);
                });
            });

    }

    notificationOpen(data){
        console.log(JSON.stringify(data));
        var alert;

        alert = this.alertCtrl.create({
            title: 'Request Received',
            message: 'A request has been received, would you like to accept it?',
            buttons: [
                {
                    text: 'Decline',
                    role: 'cancel'
                },
                {
                    text: 'Accept',
                    handler: () => {
                        this.acceptRequest(); 
                    }
                }
            ]
        });
        alert.present();
    }

    acceptRequest(){
        //call provider to accept request
        console.log("Request Accepted");
    }
}

home.ts

import { Component } from '@angular/core';
import { NavController } from 'ionic-angular';
import { NotificationProvider } from '../../providers/fcm/notifications';

@Component({
  selector: 'page-home',
  templateUrl: 'home.html',
  providers: [
    NotificationProvider
  ]
})
export class HomePage {

  constructor(public navCtrl: NavController, private notifications: NotificationProvider) {
    notifications.initializeNotifications();

  }

}

Expected behavior

When a notification is sent we should be seeing a log entry in the console and the notification should open.

Actual behavior

There is no log message and no notification opens.

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