Phonegap - Bring from background to foreground

后端 未结 2 1638
爱一瞬间的悲伤
爱一瞬间的悲伤 2020-12-07 03:19

I\'m developing my team along with an application that must run in the background when an event called by sockets should put the application in the foreground .

The

2条回答
  •  夕颜
    夕颜 (楼主)
    2020-12-07 04:01

    I found a way! Using the "toForeground" plugin. https://github.com/caioladislau/cordova-toforeground

          cordova.plugins.backgroundMode.enable();
    
          cordova.plugins.backgroundMode.onactivate = function() {
            setTimeout(function(){ 
              toForeground("MainActivity", "com.me.myapp", function() {
                navigator.notification.vibrate(1000);
              }, function(){
                navigator.notification.vibrate(5000);
              }); 
            }, 4000);
          };
    

    Note where it is called in:

    toForeground(mainClassName, packageName, successFunction, errorFunction);
    

    To find the "mainClassName" and "packageName" I searched: platforms/android/src/com/me/myapp/MainActivity.java, and I found:

    package com.me.myapp;
    
    import android.os.Bundle;
    import org.apache.cordova.*;
    
    public class MainActivity extends CordovaActivity
    {
        @Override
        public void onCreate(Bundle savedInstanceState)
        {
            super.onCreate(savedInstanceState);
            // Set by  in config.xml
            loadUrl(launchUrl);
        }
    }
    

提交回复
热议问题