1

My app's notification is working fine, receiving notifications on background and foreground, using firebase-native plugin .Now, my client needs the app open when the notification is received without any user iteration.

I've found was this, but has no correct answer for me.

On my debug, I've realize that the notification is received via broadcast by FirebaseInstanceIdReceiver. So, I've tried:

  1. Modify the

plugins/cordova-plugin-firebase-lib/plugin.xml

Editing config.xml of cordova-plugin-firebase

This compile but nothing happens.

  1. Modify the config.xml and use to replace config.xml of firebase-lib and merge with mine, calling the right intent.

enter image description here

This not compile and give me this error:

enter image description here

MY QUESTION IS: What is the best approach to archive this? Can someone guide me with a real example?

Thank you for your time!

  • The link to a potential solution that you provided contains all the relevant information to achieve this. As mentioned, aside from being highly intrusive and frowned upon, there is no guarantee that it will, at all, work with new versions of android. Also, this is highly specific functionality and no cordova plugin is going to achieve that. You need to write your own native code. – Nikitah Dec 11 '19 at 14:30
  • Thank you for your comment. It's not possible just use a intent to open the app on the received broadcast from Firebase? I just need open the app when the notification arrives, just like Skype does. I've see this functionalitiy on others Android apps already.. – Diego Desenvolvedor Dec 11 '19 at 14:41
  • "It's not possible just use a intent to open the app on the received broadcast from Firebase?" – While technically true, we're mixing apples and oranges here. The intent-filter you define in your config.xml is barely related to the actual intent you need to create to launch an activity from a service. Your answer lies here: https://stackoverflow.com/questions/3456034/how-to-start-an-activity-from-a-service. It's not supported by that plugin, you need to write your own implementation in Java. – Nikitah Dec 11 '19 at 15:07
  • Nice... thanks for the give me a direction – Diego Desenvolvedor Dec 11 '19 at 20:26
  • Maybe if you make a service by using capacitor and listen to override OnmessagSerecive() from firebase. The you can open your app. You can also make a broascast listiner to keep your servicre alive when your app is going to close. – Mohammad Reza Mrg May 28 '20 at 07:25

1 Answers1

-2

If someone need this, the solution that work for me was use the "@ionic-native/background-mode" plugin.

The resolution for this problem it's pretty easy actually. No need for intent, just install the '@ionic-native/background-mode'.

For Ionic 3, install the plugin using this command:

ionic cordova plugin add cordova-plugin-background-mode
npm install --save @ionic-native/background-mode@4

File: app.module.ts:

import {BackgroundMode} from "@ionic-native/background-mode";

Add on providers:

providers: [
    BackgroundMode,
    ...
  ],

File: app.component.ts:

Import the background-mode plugin:

import {BackgroundMode} from "@ionic-native/background-mode";

Add on constructor

constructor(
        platform: Platform, 
        statusBar: StatusBar, 
        splashScreen: SplashScreen,         
        ...
        private backgroundMode: BackgroundMode) {

        this.backgroundMode.enable();
        this.backgroundMode.excludeFromTaskList();
        this.backgroundMode.overrideBackButton();
        // this.backgroundMode.setDefaults({silent: true});

        this.backgroundMode.on('activate').subscribe(value => {
            this.backgroundMode.disableWebViewOptimizations();
        });
}

File: fcm.ts

Here is where all Firebase notification are. First, import the background-mode:

import {BackgroundMode} from "@ionic-native/background-mode";

Now, we just call these two functions on onNotificationOpen, which is the function that firebase call when notification arrives:

listenToNotifications() {
    return this.firebaseNative.onNotificationOpen()
  }

this.listenToNotifications().subscribe(
      ((msg: any) => {

          // 'These two functions make the magic'

          this.backgroundMode.unlock();
          this.backgroundMode.moveToForeground();

          if (!msg.tap)
            this.events.publish(this.dataInfo.eventFcmNew, msg)        
          else 
              this.events.publish('push', 'NotificationsPage')                  
      })
  )

That's it!

Git Example