4

I'm using cordova-plugin-fcm for push notifications in case of background and app killed. In case of app in foreground, I'm scheduling local notification using the plugin. This is my code:

            FCMPlugin.onNotification((msg) => {
            this.notificationMsg = msg;
            if (msg.wasTapped) {
             // code for app background and app killed
            }
            else {
                LocalNotifications.schedule({
                        id: 1,
                        title: 'title',
                        text: 'text',
                    })
           }
        })

But no local notifications comes up in foreground. But as soon as I minimize the app, it comes. What's the issue here? Anything to do with plugin version? Please help.

Saksham Gupta
  • 620
  • 6
  • 14
  • Am also facing same issue.. – Kanak Sony Jan 05 '18 at 19:21
  • any updates? I am using the "Local Notifcation" 0.9.0.beta.3 version and could not receive the "local Notification" in foreground and minimize the app. But v0.8.5, receive the local notification while minimize the application not in "Foreground". Any assitance? – Finder Dec 07 '18 at 12:01
  • Were you able to find out any solution to this? I am having same issue. – rout0802 Apr 25 '19 at 00:16

3 Answers3

0

NotificationEventAdditionalData.foreground returns a boolean which is true if the app is in foreground and false otherwise .

That example works with me perfectly

import { Component } from '@angular/core';
import { Platform } from 'ionic-angular';
import { StatusBar } from '@ionic-native/status-bar';
import { SplashScreen } from '@ionic-native/splash-screen';

import { TabsPage } from '../pages/tabs/tabs';

import { FCM } from '@ionic-native/fcm';

@Component({
  templateUrl: 'app.html'
})
export class MyApp {
  rootPage: any = TabsPage;
  isIOS: boolean = false;

  constructor(platform: Platform, statusBar: StatusBar, splashScreen: SplashScreen, private fcm: FCM) {
    platform.ready().then(() => {
      // Okay, so the platform is ready and our plugins are available.
      // Here you can do any higher level native things you might need.
      this.isIOS = platform.is('ios');

      if (this.isIOS) {
        // disable status bar overlay webview
        statusBar.overlaysWebView(false);
      } else {
        // let status bar overlay webview
        statusBar.overlaysWebView(true);
      }

      // set status bar to white
      statusBar.backgroundColorByHexString('#8c0f1a');

      // use light content
      statusBar.styleLightContent();

      splashScreen.hide();

    fcm.subscribeToTopic('marketing').catch(e => console.log('Error subscribing to topic', e));



        fcm.getToken().then(token => {
          // backend.registerToken(token);
          console.log(token);
        });

        fcm.onNotification().subscribe(data => {
          if (data.wasTapped) {
            console.log("Received in background");
          } else {
            console.log("Received in foreground");
          };
        });

        fcm.onTokenRefresh().subscribe(token => {
          // backend.registerToken(token);
          console.log(token);
        });
      }

  }
}
Amr.Ayoub
  • 718
  • 9
  • 20
0

Use below cordova local notification plugin, that works perfectly for me : https://github.com/katzer/cordova-plugin-local-notifications

The plugin creates the object cordova.plugins.notification.local and is accessible after deviceready has been fired.

Example:

cordova.plugins.notification.local.schedule({
title: 'My first notification',
text: 'Thats pretty easy...',
foreground: true
});
Nimesh khatri
  • 763
  • 12
  • 29
0

If you want to receive notification in the foreground with FCMPlugin you can just edit the following file

https://github.com/fechanique/cordova-plugin-fcm/blob/master/src/android/MyFirebaseMessagingService.java#L53

change the following line:

//sendNotification(remoteMessage.getNotification().getTitle(),remoteMessage.getNotification().getBody(), remoteMessage.getData());

to:

sendNotification(remoteMessage.getNotification().getTitle(), remoteMessage.getNotification().getBody(), data);
remove and add the platform you using and you are all set