Skip to content
This repository was archived by the owner on Sep 15, 2021. It is now read-only.

Commit 1cb428a

Browse files
committed
fix(plugin:pushNotif): add onNotification check + replace $rootscope.$apply() with $timeout to avoid '$digest already in progress'error
1 parent a5df9db commit 1cb428a

File tree

7 files changed

+111
-89
lines changed

7 files changed

+111
-89
lines changed

demo/init.sh

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -22,8 +22,6 @@ ap media
2222
ap inappbrowser
2323
ap battery-status
2424

25-
#read appID
26-
#read appName
2725
cordova plugin add https://github.com/Wizcorp/phonegap-facebook-plugin.git --variable APP_ID="12345678" --variable APP_NAME="Name"
2826
cordova plugin add https://github.com/driftyco/ionic-plugins-keyboard # keyboard
2927
cordova plugin add https://github.com/VitaliiBlagodir/cordova-plugin-datepicker.git # date-picker
@@ -35,3 +33,6 @@ cordova plugin add uk.co.ilee.touchid # touchID
3533
cordova plugin add https://github.com/pushandplay/cordova-plugin-apprate.git # AppRate
3634
cordova plugin add https://github.com/katzer/cordova-plugin-local-notifications.git
3735
cordova plugin add https://github.com/katzer/cordova-plugin-email-composer.git
36+
cordova plugin add https://github.com/phonegap-build/PushPlugin.git
37+
cordova plugin add https://github.com/EddyVerbruggen/cordova-plugin-actionsheet.git
38+
cordova plugin add de.appplant.cordova.plugin.badge

demo/www/app/app.js

Lines changed: 31 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -39,7 +39,7 @@ angular.module('demo', [
3939
'demo.vibration.ctrl'
4040
])
4141

42-
.run(function ($rootScope, $ionicPlatform, $cordovaNetwork, $cordovaBatteryStatus, $cordovaLocalNotification) {
42+
.run(function ($rootScope, $ionicPlatform, $cordovaNetwork, $cordovaBatteryStatus, $cordovaLocalNotification, $cordovaPush) {
4343

4444
$ionicPlatform.ready(function () {
4545
if (window.cordova && window.cordova.plugins.Keyboard) {
@@ -55,6 +55,36 @@ angular.module('demo', [
5555
//alert("denied registration");
5656
});
5757

58+
var iosConfig = {
59+
"badge": true,
60+
"sound": true,
61+
"alert": true
62+
};
63+
$cordovaPush.register(iosConfig).then(function (result) {
64+
alert("device token: " + result.deviceToken);
65+
}, function (error) {
66+
alert("error " + error);
67+
});
68+
69+
$rootScope.$on('$cordovaPush:notificationReceived', function (event, notification) {
70+
if (notification.alert)
71+
navigator.notification.alert(notification.alert);
72+
73+
if (notification.sound)
74+
var snd = new Media(event.sound);
75+
snd.play();
76+
77+
if (notification.badge) {
78+
$cordovaPush.setBadgeNumber(notification.badge)
79+
.then(function (result) {
80+
// Success!
81+
}, function (err) {
82+
// An error occurred. Show a message to the user
83+
});
84+
}
85+
});
86+
87+
5888
$rootScope.$on("$cordovaNetwork:offline", function () {
5989
alert("Device is now Offline!");
6090
});

demo/www/lib/ngCordova/dist/ng-cordova.js

Lines changed: 25 additions & 28 deletions
Original file line numberDiff line numberDiff line change
@@ -4875,58 +4875,55 @@ angular.module('ngCordova.plugins.progressIndicator', [])
48754875

48764876
angular.module('ngCordova.plugins.push', [])
48774877

4878-
.factory('$cordovaPush', ['$q', '$window', '$rootScope', function ($q, $window, $rootScope) {
4878+
.factory('$cordovaPush', ['$q', '$window', '$rootScope', '$timeout', function ($q, $window, $rootScope, $timeout) {
48794879
return {
48804880
onNotification: function (notification) {
4881-
$rootScope.$apply(function () {
4881+
$timeout(function () {
48824882
$rootScope.$broadcast('$cordovaPush:notificationReceived', notification);
48834883
});
48844884
},
48854885

48864886
register: function (config) {
48874887
var q = $q.defer();
4888-
4888+
var injector;
48894889
if (config !== undefined && config.ecb === undefined) {
4890-
config.ecb = "angular.element(document.querySelector('[ng-app]')).injector().get('$cordovaPush').onNotification";
4890+
if (angular.element(document.querySelector('[ng-app]')) === undefined) {
4891+
injector = "document.body";
4892+
}
4893+
else {
4894+
injector = "document.querySelector('[ng-app]')";
4895+
}
4896+
config.ecb = "angular.element(" + injector + ").injector().get('$cordovaPush').onNotification";
48914897
}
48924898

4893-
$window.plugins.pushNotification.register(
4894-
function (token) {
4895-
q.resolve(token);
4896-
},
4897-
function (error) {
4898-
q.reject(error);
4899-
},
4900-
config);
4899+
$window.plugins.pushNotification.register(function (token) {
4900+
q.resolve(token);
4901+
}, function (error) {
4902+
q.reject(error);
4903+
}, config);
49014904

49024905
return q.promise;
49034906
},
49044907

49054908
unregister: function (options) {
49064909
var q = $q.defer();
4907-
$window.plugins.pushNotification.unregister(
4908-
function (result) {
4909-
q.resolve(result);
4910-
},
4911-
function (error) {
4912-
q.reject(error);
4913-
},
4914-
options);
4910+
$window.plugins.pushNotification.unregister(function (result) {
4911+
q.resolve(result);
4912+
}, function (error) {
4913+
q.reject(error);
4914+
}, options);
49154915

49164916
return q.promise;
49174917
},
49184918

49194919
// iOS only
49204920
setBadgeNumber: function (number) {
49214921
var q = $q.defer();
4922-
$window.plugins.pushNotification.setApplicationIconBadgeNumber(
4923-
function (result) {
4924-
q.resolve(result);
4925-
},
4926-
function (error) {
4927-
q.reject(error);
4928-
},
4929-
number);
4922+
$window.plugins.pushNotification.setApplicationIconBadgeNumber(function (result) {
4923+
q.resolve(result);
4924+
}, function (error) {
4925+
q.reject(error);
4926+
}, number);
49304927
return q.promise;
49314928
}
49324929
};

demo/www/lib/ngCordova/dist/ng-cordova.min.js

Lines changed: 1 addition & 1 deletion
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

dist/ng-cordova.js

Lines changed: 25 additions & 28 deletions
Original file line numberDiff line numberDiff line change
@@ -4875,58 +4875,55 @@ angular.module('ngCordova.plugins.progressIndicator', [])
48754875

48764876
angular.module('ngCordova.plugins.push', [])
48774877

4878-
.factory('$cordovaPush', ['$q', '$window', '$rootScope', function ($q, $window, $rootScope) {
4878+
.factory('$cordovaPush', ['$q', '$window', '$rootScope', '$timeout', function ($q, $window, $rootScope, $timeout) {
48794879
return {
48804880
onNotification: function (notification) {
4881-
$rootScope.$apply(function () {
4881+
$timeout(function () {
48824882
$rootScope.$broadcast('$cordovaPush:notificationReceived', notification);
48834883
});
48844884
},
48854885

48864886
register: function (config) {
48874887
var q = $q.defer();
4888-
4888+
var injector;
48894889
if (config !== undefined && config.ecb === undefined) {
4890-
config.ecb = "angular.element(document.querySelector('[ng-app]')).injector().get('$cordovaPush').onNotification";
4890+
if (angular.element(document.querySelector('[ng-app]')) === undefined) {
4891+
injector = "document.body";
4892+
}
4893+
else {
4894+
injector = "document.querySelector('[ng-app]')";
4895+
}
4896+
config.ecb = "angular.element(" + injector + ").injector().get('$cordovaPush').onNotification";
48914897
}
48924898

4893-
$window.plugins.pushNotification.register(
4894-
function (token) {
4895-
q.resolve(token);
4896-
},
4897-
function (error) {
4898-
q.reject(error);
4899-
},
4900-
config);
4899+
$window.plugins.pushNotification.register(function (token) {
4900+
q.resolve(token);
4901+
}, function (error) {
4902+
q.reject(error);
4903+
}, config);
49014904

49024905
return q.promise;
49034906
},
49044907

49054908
unregister: function (options) {
49064909
var q = $q.defer();
4907-
$window.plugins.pushNotification.unregister(
4908-
function (result) {
4909-
q.resolve(result);
4910-
},
4911-
function (error) {
4912-
q.reject(error);
4913-
},
4914-
options);
4910+
$window.plugins.pushNotification.unregister(function (result) {
4911+
q.resolve(result);
4912+
}, function (error) {
4913+
q.reject(error);
4914+
}, options);
49154915

49164916
return q.promise;
49174917
},
49184918

49194919
// iOS only
49204920
setBadgeNumber: function (number) {
49214921
var q = $q.defer();
4922-
$window.plugins.pushNotification.setApplicationIconBadgeNumber(
4923-
function (result) {
4924-
q.resolve(result);
4925-
},
4926-
function (error) {
4927-
q.reject(error);
4928-
},
4929-
number);
4922+
$window.plugins.pushNotification.setApplicationIconBadgeNumber(function (result) {
4923+
q.resolve(result);
4924+
}, function (error) {
4925+
q.reject(error);
4926+
}, number);
49304927
return q.promise;
49314928
}
49324929
};

dist/ng-cordova.min.js

Lines changed: 1 addition & 1 deletion
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

src/plugins/push.js

Lines changed: 25 additions & 28 deletions
Original file line numberDiff line numberDiff line change
@@ -3,58 +3,55 @@
33

44
angular.module('ngCordova.plugins.push', [])
55

6-
.factory('$cordovaPush', ['$q', '$window', '$rootScope', function ($q, $window, $rootScope) {
6+
.factory('$cordovaPush', ['$q', '$window', '$rootScope', '$timeout', function ($q, $window, $rootScope, $timeout) {
77
return {
88
onNotification: function (notification) {
9-
$rootScope.$apply(function () {
9+
$timeout(function () {
1010
$rootScope.$broadcast('$cordovaPush:notificationReceived', notification);
1111
});
1212
},
1313

1414
register: function (config) {
1515
var q = $q.defer();
16-
16+
var injector;
1717
if (config !== undefined && config.ecb === undefined) {
18-
config.ecb = "angular.element(document.querySelector('[ng-app]')).injector().get('$cordovaPush').onNotification";
18+
if (angular.element(document.querySelector('[ng-app]')) === undefined) {
19+
injector = "document.body";
20+
}
21+
else {
22+
injector = "document.querySelector('[ng-app]')";
23+
}
24+
config.ecb = "angular.element(" + injector + ").injector().get('$cordovaPush').onNotification";
1925
}
2026

21-
$window.plugins.pushNotification.register(
22-
function (token) {
23-
q.resolve(token);
24-
},
25-
function (error) {
26-
q.reject(error);
27-
},
28-
config);
27+
$window.plugins.pushNotification.register(function (token) {
28+
q.resolve(token);
29+
}, function (error) {
30+
q.reject(error);
31+
}, config);
2932

3033
return q.promise;
3134
},
3235

3336
unregister: function (options) {
3437
var q = $q.defer();
35-
$window.plugins.pushNotification.unregister(
36-
function (result) {
37-
q.resolve(result);
38-
},
39-
function (error) {
40-
q.reject(error);
41-
},
42-
options);
38+
$window.plugins.pushNotification.unregister(function (result) {
39+
q.resolve(result);
40+
}, function (error) {
41+
q.reject(error);
42+
}, options);
4343

4444
return q.promise;
4545
},
4646

4747
// iOS only
4848
setBadgeNumber: function (number) {
4949
var q = $q.defer();
50-
$window.plugins.pushNotification.setApplicationIconBadgeNumber(
51-
function (result) {
52-
q.resolve(result);
53-
},
54-
function (error) {
55-
q.reject(error);
56-
},
57-
number);
50+
$window.plugins.pushNotification.setApplicationIconBadgeNumber(function (result) {
51+
q.resolve(result);
52+
}, function (error) {
53+
q.reject(error);
54+
}, number);
5855
return q.promise;
5956
}
6057
};

0 commit comments

Comments
 (0)