简体   繁体   English

当您的应用程序在后台时,离子如何通过摇动打开您的应用程序?

[英]Ionic how to open your app with shake when you app is in the background?

I am building an emergency app, and one of the features is to open the app on shake, now I understand that app must be in the background running.我正在构建一个紧急应用程序,其中一个功能是在摇晃时打开应用程序,现在我明白该应用程序必须在后台运行。

and I am using cordova-background-mode and cordova shake and the everything works,我正在使用cordova-background-mode和cordovashake,一切正常,

however when the phone is on the home screen(the app is just minimized).但是当手机在主屏幕上时(应用程序只是最小化)。 the app does not launch when I shake, but when I reopen the app by tapping on its icon, it does show that a shake event occurred and opens another app that I am using to test.该应用程序在我摇晃时不会启动,但是当我通过点击其图标重新打开该应用程序时,它确实显示发生了摇晃事件并打开了另一个我用来测试的应用程序。

How can I get it to open the app on shake when the app is in the background?当应用程序在后台时,如何让它在抖动时打开应用程序?

Here is my code这是我的代码

this.platform.ready().then((e) => {
      this.backgroundMode.enable();
      this.backgroundMode.setEnabled(true);

      this.statusBar.styleDefault();
      this.splashScreen.hide();

        this.shake.startWatch().subscribe(() => {
          console.log(`watching...${e}`);
          alert('shake');
          const options: AppLauncherOptions = {
          }

           if(this.platform.is('ios')) {
             options.uri = 'fb://'
           } else {
             options.packageName = 'com.facebook.katana'
           }

           this.appLauncher.canLaunch(options).then((launch:Boolean)=>{
             if(launch){
               this.appLauncher.launch(options).then(()=>{
                 alert('Launched')
               },
               (err)=>{
                alert(JSON.stringify(err))
               })
             }else{
               alert(JSON.stringify("unable to launch app"));
             }
           },(err)=>{
             alert(JSON.stringify(err));
           })  

          });

Edit编辑

this is my code and it works, however the problem is, if I open another app, ie whatsapp but it can be any app, and close it, and then if I shake the phone, it opens the last app I was using instead of launching my app.这是我的代码,它可以工作,但是问题是,如果我打开另一个应用程序,即 whatsapp 但它可以是任何应用程序,然后关闭它,然后如果我摇晃手机,它会打开我使用的最后一个应用程序而不是启动我的应用程序。


 startBackgroundMode(){          

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

    this.backgroundMode.on('activate').subscribe(value => {
        this.backgroundMode.disableWebViewOptimizations();  
        this.shakePhone();   
    });
}
shakePhone(){
  this.shake.startWatch().subscribe(() => {
    console.log(`watching...`);                      
    this.openApp();

    });
}
  openApp(){  

    const options: AppLauncherOptions = {
    }
    options.packageName = 'this is my app's package name'


  this.appLauncher.launch(options);

  }

Various APIs like playing media or tracking GPS position in background might not work while in background even the background mode is active.即使后台模式处于活动状态,在后台播放媒体或跟踪 GPS 位置等各种 API 也可能无法在后台运行。 To fix such issues the plugin provides a method to disable most optimizations done by Android/CrossWalk.为了解决此类问题,该插件提供了一种禁用大多数由 Android/CrossWalk 完成的优化的方法。

cordova.plugins.backgroundMode.on('activate', function() {
   cordova.plugins.backgroundMode.disableWebViewOptimizations(); 
});

Note: Calling the method led to increased resource and power consumption.注意:调用该方法会导致资源和功耗增加。

Also need to use following还需要使用以下

The plugin fires an event each time its status has been changed.每次更改状态时,插件都会触发一个事件。 These events are enable, disable, activate, deactivate and failure.这些事件是启用、禁用、激活、停用和失败。

also need to check this.shake.startWatch() in background activated mode还需要在后台激活模式下检查 this.shake.startWatch()

cordova.plugins.backgroundMode.on('EVENT', function);

To remove an event listeners:要删除事件侦听器:

cordova.plugins.backgroundMode.un('EVENT', function); 

I believe this is not the right way which you are trying.我相信这不是您正在尝试的正确方法。 If you could employ java in order to develop your application then it was possible to use the SensorManager to detect a shake.如果您可以使用java来开发您的应用程序,那么就可以使用SensorManager来检测抖动。 Now you are working with ionic and the solution is taking advantage of device-motion plugin.现在您正在使用ionic并且解决方案是利用device-motion插件。

Note that solutions like ionic are not as powerful as main development environment (java or kotlin with android studio) although they are more strong at interface design since they take advantage of browser based applications (web kit).请注意,像ionic这样的解决方案不如主要开发环境(java 或 kotlin 与 android studio)强大,尽管它们在界面设计方面更强大,因为它们利用了基于浏览器的应用程序(web kit)。 So you have to consider browser restrictions too, so employing sth like devicemotion EventListener may not work on all operating systems.所以你也必须考虑浏览器的限制,所以使用诸如devicemotion EventListener类的东西可能不适用于所有操作系统。

If you are working on an important application choose the main solution to avoid restrictions.如果您正在处理重要的应用程序,请选择主要解决方案以避免限制。

Knowing that you are using ionic 4, one way to achieve this result is by developing one custom plugin, but with ionic capacitor instead of cordova.知道您正在使用 ionic 4,实现此结果的一种方法是开发一个自定义插件,但使用离子电容器而不是cordova。 Capacitor made easier to build your custom plugins and makes the native code more accessible for those who hasnt built any plugins yet. Capacitor 使构建自定义插件变得更容易,并使尚未构建任何插件的人更容易访问本机代码。

The links below show how you implement a custom plugin for both ios and android.下面的链接显示了如何为 ios 和 android 实现自定义插件。
How to embed third party framework on ionic capacitor custom plugin? 如何在离子电容器自定义插件上嵌入第三方框架?
How to embed an Android library (AAR file) into a capacitor plugin? 如何将 Android 库(AAR 文件)嵌入到电容器插件中?

I point this solution as a alternative.我将此解决方案作为替代方案。 I know that the workload would be much higher and that the project already have plenty of cordova plugins.我知道工作量会高得多,而且该项目已经有大量的cordova 插件。

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

相关问题 离子1信号有一种方法可以知道从背景到前景打开应用程序或启动应用程序时是否有推送通知 - ionic 1 onesignal is there a way to know if there is a push notification when open app from background to foreground or launching app Ionic / Cordova:即使在后台,如何强制应用在启动时刷新? - Ionic / Cordova: How to force app to refresh on start, even when it was in background? 当我将 Ionic 应用程序移到后台时,它会“重新启动” - Ionic app is “restarted” when I move it to background 在Ionic 2中打开应用程序时处理推送通知 - Handling Push Notifications when app is open in Ionic 2 如何在您的应用程序上设置漂亮的背景 - How to Set a Pretty Background on your App 如何使您的移动AIR应用程序始终在后台运行? - How do you make your mobile AIR app run in the background all the time? android 离子推送通知不打开后台应用程序 - Ionic push notification does not open the background app on android 如何在 ionic/android 中后台运行应用程序 - How to App running in background in ionic/android 当应用程序无法在后台运行时,如何在点击推送通知中打开网址 - how to open url on click push notification when app is not working in background 当应用程序在后台运行时,如何打开特定活动? - How to open a specific activity when the app is running in background?
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM