Search code examples
ionic-frameworkionic2backgroundworkerbackground-process

Http request not working on background mode


i write a http request for Reddit but not working on app component i copied code on other pages and works fine but on app component not working Http request(and my private services)

import { Http, Response,Request } from '@angular/http';
import 'rxjs';
import { Observable } from 'rxjs/Observable';
import 'rxjs/add/operator/map';





constructor(public http: Http,public agornaApi:AgornaApi ,private backgroundMode: BackgroundMode ,public localNotifications: LocalNotifications,platform: Platform, statusBar: StatusBar, splashScreen: SplashScreen, public storage: Storage,public authService: Auth) {


localNotifications.on("click", (notification, state) => {
this.nav.push(ListmessagesPage);
        });
document.addEventListener('deviceready', function () {  
    // Android customization
    // To indicate that the app is executing tasks in background and being paused would disrupt the user.
    // The plug-in has to create a notification while in background - like a download progress bar.
    cordova.plugins.backgroundMode.setDefaults({ 
        title:  'بارگزاری',
        text:   ''
    });
    // Enable background mode
    cordova.plugins.backgroundMode.enable();

    // Called when background mode has been activated
    cordova.plugins.backgroundMode.onactivate = function () {


          setInterval(function () {






        this.http.get('https://www.reddit.com/r/gifs/new/.json?limit=10').map(res => res.json()).subscribe(data => {


   cordova.plugins.notification.local.schedule({

        text: data.kind,
        data: { secret:'key' }
    });

    });





        }, 10000);
    }
}, false);



  });



    platform.ready().then(() => {
      // Okay, so the platform is ready and our plugins are available.
      // Here you can do any higher level native things you might need.
      statusBar.styleDefault();
          setTimeout(() => {
        splashScreen.hide();        

      }, 100);
    });
  }
```

system info

global packages:

@ionic/cli-utils : 1.0.0-rc.2
Cordova CLI      : 6.5.0
Ionic CLI        : 3.0.0-rc.2

local packages:

@ionic/app-scripts              : 1.3.6
@ionic/cli-plugin-cordova       : 1.0.0-rc.2
@ionic/cli-plugin-ionic-angular : 1.0.0-rc.2
Ionic Framework                 : ionic-angular 3.1.1

System:

Node       : v7.4.0
OS         : macOS Sierra
Xcode      : Xcode 8.3.2 Build version 8E2002
ios-deploy : 1.9.1
ios-sim    : not installed

Solution

  • I would try making a separate page, where we have a different controller (for that view).

    First of all, try the following troubleshooting in order to know if it is actually trying to fetch the request:

    Go to chrome->dev-tools->network tab: In the network tab, verify the GET requests being done by angular. If you see it, then try to see the preview part of the request. Sometimes, depending on your system, a cross-origin block happens. Sometimes is just your url, not being found (a 404) or any other type of error.

    Secondly: If you see the request but no data, probably is that you might be doing something right after getting the data, like moving from a page to another. Try saving such data or try making all of the logic inside of the http request. But why inside the http request? Because http are asynchronous in nature and you might be executing and the observable has not yet got response in order to react. So probably you are making the call, but the code logic is just jumping forward.

    I once tried to do http requests in this exact same area of my project, they always gave me problems.