Skip to content

Instantly share code, notes, and snippets.

@pbernasconi
Last active August 29, 2015 14:13
Show Gist options
  • Save pbernasconi/f550baf4f86b01e5dd3b to your computer and use it in GitHub Desktop.
Save pbernasconi/f550baf4f86b01e5dd3b to your computer and use it in GitHub Desktop.
$cordovaPush with $rootScope.$new()
angular.module('ngCordova.plugins.push', [])
.factory('$cordovaPush', ['$q', '$window', '$rootScope', '$timeout', function ($q, $window, $rootScope, $timeout) {
var $cordovaPush = $rootScope.$new(true);
$cordovaPush.onNotification = function (notification) {
$timeout(function () {
$cordovaPush.$broadcast('notificationReceived', notification);
});
};
$cordovaPush.register = function (config) {
var q = $q.defer();
var injector;
if (config !== undefined && config.ecb === undefined) {
if (angular.element(document.querySelector('[ng-app]')) === undefined) {
injector = "document.body";
}
else {
injector = "document.querySelector('[ng-app]')";
}
config.ecb = "angular.element(" + injector + ").injector().get('$cordovaPush').onNotification";
}
$window.plugins.pushNotification.register(function (token) {
q.resolve(token);
}, function (error) {
q.reject(error);
}, config);
return q.promise;
};
$cordovaPush.unregister = function (options) {
var q = $q.defer();
$window.plugins.pushNotification.unregister(function (result) {
q.resolve(result);
}, function (error) {
q.reject(error);
}, options);
return q.promise;
};
$cordovaPush.setBadgeNumber = function (number) {
var q = $q.defer();
$window.plugins.pushNotification.setApplicationIconBadgeNumber(function (result) {
q.resolve(result);
}, function (error) {
q.reject(error);
}, number);
return q.promise;
};
return $cordovaPush;
/*
return {
onNotification: function (notification) {
$timeout(function () {
//$rootScope.$broadcast('$cordovaPush:notificationReceived', notification);
scope.$broadcast('notificationReceived', notification);
});
},
this: scope,
register: function (config) {
var q = $q.defer();
var injector;
if (config !== undefined && config.ecb === undefined) {
if (angular.element(document.querySelector('[ng-app]')) === undefined) {
injector = "document.body";
}
else {
injector = "document.querySelector('[ng-app]')";
}
config.ecb = "angular.element(" + injector + ").injector().get('$cordovaPush').onNotification";
}
$window.plugins.pushNotification.register(function (token) {
q.resolve(token);
}, function (error) {
q.reject(error);
}, config);
return q.promise;
},
unregister: function (options) {
var q = $q.defer();
$window.plugins.pushNotification.unregister(function (result) {
q.resolve(result);
}, function (error) {
q.reject(error);
}, options);
return q.promise;
},
// iOS only
setBadgeNumber: function (number) {
var q = $q.defer();
$window.plugins.pushNotification.setApplicationIconBadgeNumber(function (result) {
q.resolve(result);
}, function (error) {
q.reject(error);
}, number);
return q.promise;
}
};
*/
}])
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment