0

I made an app in Phone Gap but now I am trying to make it in NativeScript,

App connect and is almost 50% complete; however when the user navigate away from the app, screen go off or change to another app, the app disconnect and reconnect when on focus again..

My question, has anybody tackle the issue and keep SocketIO from disconnecting and reconnecting when in background mode?

I have run this example, but have no clue how to implement it.. for SocketIO.. https://github.com/NativeScript/sample-android-background-services

cordova with this simple code, it keep the socket connected.

document.addEventListener("pause", onPause, false);
document.addEventListener("resume", onResume, false);

               function onPause(){


                    setTimeout(function(){

                        //console.log('pausing');

                        cordova.plugins.backgroundMode.enable();

                    }, 500);
                };

                function onResume(){

                    setTimeout(function() {

                        console.log('resuming');

                      cordova.plugins.backgroundMode.disable();

                    }, 500);

                };

how to implement same behavior in {{N}}.

// App went to background...
application.on(application.suspendEvent, function (args) {
    console.log('chat is in background mode');
});

// App was reopened...
application.on(application.resumeEvent, function (args) {
    console.log('chat is not longer in background mode');
});
UTAN
  • 11
  • 4
  • I'm not sure what you have inside `onPause` or `onResume` functions. But the equivalent of Cordova's pause & resume events are of course suspendEvent & resumeEvent. `cordova.plugins.backgroundMode.overrideBackButton()` does nothing but prevents app being closed by back button and imitates Home button so app is minimised. You could do the [same](https://github.com/katzer/cordova-plugin-background-mode/blob/master/src/android/BackgroundModeExt.java#L136-L140) in your {N} app by accessing the native APIs. – Manoj Aug 12 '19 at 15:14
  • I am sorry, first time posting here.. so I forgot to do place the actual code.. that's not what i am looking, what i want is to keep getting messages when my app goes into background mode.. ```js – UTAN Aug 13 '19 at 06:18
  • `function onPause(){ setTimeout(function(){ //console.log('pausing'); cordova.plugins.backgroundMode.enable(); }, 500); }; function onResume(){ setTimeout(function() { console.log('resuming'); cordova.plugins.backgroundMode.disable(); }, 500); };` I don't get how to post more code here, but basically I want my app not disconnect from socketIO – UTAN Aug 13 '19 at 06:22
  • If you are familiar with Cordova's plugin architecture you must have noticed, those methods does nothing but calls the `startService` and `stopService` methods in [BackgroundMode.java](https://github.com/katzer/cordova-plugin-background-mode/blob/master/src/android/BackgroundMode.java). You may directly use those native code in your {N} project, you must be familiar with [Marshalling Java to JS](https://docs.nativescript.org/core-concepts/android-runtime/marshalling/java-to-js) to do the same. – Manoj Aug 13 '19 at 07:01
  • I am sorry, am not . Am barely learning {{N}} and for phonegap just made the hibrid app reusing my code from the web, then i did not studied further.. Am not really skilled and don't have much time to study. That's why i couldn't understand what's happening with https://github.com/NativeScript/sample-android-background-services appreciate ur replies. – UTAN Aug 13 '19 at 08:02

0 Answers0