Red-Folder / bgs-core

Core code for the Cordova Background Service
Other
236 stars 105 forks source link

On Android when i hit back button then the app do not work to ring #81

Closed shamun closed 6 years ago

shamun commented 6 years ago

Using the sample https://github.com/Red-Folder/bgs-core/wiki/Using-the-MyService-Sample i can see it works when i come back to my app by having the real focus only on my app.

But my app need to run absolutely in the backend (like you start the app once and you put it by hitting back or close).

Is this normal? (how would you really let the app run forever with back or close) When i hit back or close my app focus and using some other app, then it never playing my ring crawl. it is only crawling or ringing when my app is absolutely on main focus.

        var timer_ringing = null;
        var timer_delay = 4000;
        function startRing(){
            var username = 'test';
            clearTimeout(timer_ringing);

            showErrorLess("StartRing: " + username);

            try {
                var post_url1 = 'https://www.example.com/test/notify';
                $.post(post_url1, {
                        username: username,
                }, function(msg) {
                    if(msg.result=='ok') {
                        $('#iring').trigger("play");
                        timer_delay = 6000;
                    }
                    else {
                        $('#iring').trigger("paused");//pause
                        $('#iring').attr("currentTime",0);
                        timer_delay = 4000;
                    }
                }, "json");
            }catch(e) {
                showErrorLess(e);
            }

            timer_ringing = setTimeout(function() {
                startRing();
            }, timer_delay);
        }
shamun commented 6 years ago

Is there any way, to go in the foreground when in my backend i have some status received from my own networks? (so that i can implement ringing or UI on screen lock or other state )

Red-Folder commented 6 years ago

I think you are asking if there is a way for you app to jump to the foreground.

Normally if there is an event that the user should interact with, I would have the background service create a notification (https://developer.android.com/guide/topics/ui/notifiers/notifications.html). The user could click on the notification opening the app.

shamun commented 6 years ago

Not exactly i am looking for notification.

But when my background service is running, i am doing a crawling to my server where it says "mom calling". On that moment even the mobile is in lock mode or my app is not running on focus, i wanted as following GUI (ringing). Note: currently, it works when my app is not closed or active on the screen (at-least a ringing works, without GUI auto showing on front screen)

Red-Folder commented 6 years ago

If you program that background logic into the background service, it should continue running even if another app takes over the frontend. Note that that logic needs to be in Java (not JavaScript) and running in the MyService.java doWork (if you are amending the bgs-sample code).

Occasionally the background code maybe killed - but this seems to be a restriction enforced by Android when the device simply doesn't have enough resources.