Tatiana Perere
Tatiana Perere

Reputation: 81

AngularJS access $http on onNotification event PushNotification cordova plugin

I'm using the plugin https://github.com/phonegap-build/PushPlugin/ with Angular 1.3 and I need to send the regid to server when receive "registered" event. The problem is that I don't have $http object to call my server on this context. How can I achieve that, please?

function onNotification(e){
    if(e.event == "registered"){
        var req = {
            method: "POST",
            url: "http://myurl.com/?var="+e.regid
        };
        $http(req).success(function(data){
            alert(data);
        });
    }
}

Upvotes: 1

Views: 57

Answers (2)

Tatiana Perere
Tatiana Perere

Reputation: 81

I just learned how to inject $http into the event method:

$http = angular.injector(["ng"]).get("$http");

Upvotes: 1

Hardik Vaghani
Hardik Vaghani

Reputation: 2173

Change $http call as follows, .success is deprecated.

$http({
    method: "POST",
    url: "http://myurl.com/?var="+e.regid
}).then(function successCallback(response) {
     // this callback will be called asynchronously
     // when the response is available
     alert(response);
}, function errorCallback(response) {
     // called asynchronously if an error occurs
     // or server returns response with an error status.
});

Ref. : https://docs.angularjs.org/api/ng/service/$http

Regards.

Upvotes: 0

Related Questions