简体   繁体   中英

IONIC/Capacitor: Bring Android app to foreground on trigger

We have a pretty common usecase with our (via Capacitor) Android generated app: Once it receives a message via eg a Websocket (or third party apps like OneSignal, Firebase etc) we want to bring the app to the foreground in case the user is currently interacting with other apps (like Skype, Whatsapp etc). The reason is that we have implemented an "Alarm" scenario and if an alarm comes in, the app should come to front and show what's going on. Simple push notifications won't do the job here.

So we researched on the topic but as we're not native Android devs, we don't understand the full picture clearly.

eg Android bring app to foreground on Firebase notification suggests that via FLAG_ACTIVITY_REORDER_TO_FRONT it is possible to bring an app from the background to foreground.

The question is how is this going to be implemented in a hybrid app scenario (like with Cordova/IONIC/Capacitor).

In our app we are pretty far to listen to API signals via Websocket. Once an alarm is received we are able to send a signal to the App via Websocket so we can do pretty much anything. We could for example redirect the signal from the webapp back to the app container.

The question now is how can this scenario be solved either via Websocket or Firebase (FCM) and is it possible to solve it straight through the IONIC architecture?

In a cordova/ionic hybrid app I would use the cordova background mode plugin and the window_system_alert permission plugin. Those two are what I'm using and its working like a charm.

The steps I'd follow:

  1. The first one was to include the force-start to the notification body
  2. The second one was to give permissions to the app to be drawn over other apps. I managed to to this using this plugin: https://github.com/wryel/cordova-plugin-system-alert-window-permission . It requests the SYSTEM_ALERT_WINDOW permission.
  3. The last one is to install the background-mode plugin ( https://github.com/katzer/cordova-plugin-background-mode ) and whenever you receive a notification you wake up the terminal and show your app in foreground (enable -> unlock -> moveToForeground).

Please, let me know if I misunderstood something and I can change my answer.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM