IONIC 3 - Open app on notification received

无人久伴 提交于 2020-01-16 17:08:12

问题


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

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.

This not compile and give me this error:

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

Thank you for your time!


回答1:


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



来源:https://stackoverflow.com/questions/59281976/ionic-3-open-app-on-notification-received

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!