简体   繁体   English

离子将应用程序带到前台在 Android 中不起作用

[英]Ionic Bring Application to Foreground not working in Android

I am building an app using Ionic which is supposed to run in the background and as soon as an event of interest happens, the app should appear in the foreground.我正在使用 Ionic 构建一个应用程序,它应该在后台运行,一旦发生感兴趣的事件,该应用程序应该出现在前台。

To do that I use BackgroundMode.为此,我使用 BackgroundMode。

In the constructor I enable background mode.在构造函数中,我启用了后台模式。

this.backgroundMode.enable();

In order to test this functionality, I crated a function which after pressing the button, waits for 5 seconds and sends a local notification and supposed to bring the application to foreground为了测试这个功能,我创建了一个函数,在按下按钮后,等待 5 秒并发送本地通知并应该将应用程序置于前台

  bringToForeground() {
    this.sleep(5000).then(() => {
      this.backgroundMode.moveToForeground();
      this.localNotifications.schedule({
        id: 2,
        text: '2 -> Single ILocalNotification',
        sound: 'file://sound.mp3',
        data: { secret: "" }
      });

    })

  }


  sleep(time) {
    return new Promise((resolve) => setTimeout(resolve, time));
  }

To test it, I为了测试它,我

  1. Run the application on Samsung Galaxy 7 (Android)在三星 Galaxy 7 (Android) 上运行应用程序
  2. Press a button in the application to trigger the function按下应用程序中的按钮以触发该功能
  3. Press home button on the phone so that the app goes to the background按下手机上的主页按钮,使应用程序进入后台
  4. Wait.等待。

What happens is that I see the notification, the app is still running in the background.发生的情况是我看到通知,该应用程序仍在后台运行。

Any idea about what is going on?知道发生了什么吗?

You have used the enabled() on constructor?您在构造函数上使用了 enabled() 吗? Something like this:像这样的东西:

startBackgroundMode(){

        console.log('Iniciando background mode')        

        this.backgroundMode.enable();
        this.backgroundMode.overrideBackButton();               
        //this.backgroundMode.excludeFromTaskList();    

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

For me, the solution was use an Observable, then I subscribe and call moveToForeGround()对我来说,解决方案是使用 Observable,然后我订阅并调用 moveToForeGround()

Check the complete answer here此处查看完整答案

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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