Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Phonegap - Bring from background to foreground

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 application must come to foreground similar to viber or whatsapp call. I stopped at this point. My application can now call an audio and vibrate, but I have to draw the screen to the foreground.

I'm using version 5.1.1 phonegap.

I am this plugin: https://github.com/katzer/cordova-plugin-background-mode

Could someone give me a hand? Very grateful this already.

like image 252
Caio Ladislau Avatar asked Sep 03 '15 12:09

Caio Ladislau


2 Answers

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 <content src="index.html" /> in config.xml
        loadUrl(launchUrl);
    }
}
like image 110
Caio Ladislau Avatar answered Oct 05 '22 15:10

Caio Ladislau


I have used this plugin instead of developing one for my own, its a forked repo https://github.com/suhail339/cordova-bring-to-front

Keep in mind to install from Cordova CLI, visual studio plugin installer might create problem.

like image 25
Suhail Mumtaz Awan Avatar answered Oct 05 '22 14:10

Suhail Mumtaz Awan