Skip to content

Commit 71f3f6e

Browse files
author
Matthew Sun
committed
update/fix example PushNotification.js, fix odd code formatting
1 parent 894232c commit 71f3f6e

File tree

3 files changed

+100
-87
lines changed

3 files changed

+100
-87
lines changed

Example/www/PushNotification.js

Lines changed: 58 additions & 45 deletions
Original file line numberDiff line numberDiff line change
@@ -1,65 +1,78 @@
1-
21
var PushNotification = function() {
32
};
43

4+
// Call this to register for push notifications. Content of [options] depends on whether we are working with APNS (iOS) or GCM (Android)
5+
PushNotification.prototype.register = function(successCallback, errorCallback, options) {
6+
if (errorCallback == null) { errorCallback = function() {}}
7+
8+
if (typeof errorCallback != "function") {
9+
console.log("PushNotification.register failure: failure parameter not a function");
10+
return
11+
}
12+
13+
if (typeof successCallback != "function") {
14+
console.log("PushNotification.register failure: success callback parameter must be a function");
15+
return
16+
}
17+
18+
cordova.exec(successCallback, errorCallback, "PushPlugin", "register", [options]);
19+
};
520

6-
// Call this to register for push notifications. Content of [options] depends on whether we are working with APNS (iOS) or GCM (Android)
7-
PushNotification.prototype.register = function(successCallback, errorCallback, options) {
8-
if (errorCallback == null) { errorCallback = function() {}}
21+
// Call this to unregister for push notifications
22+
PushNotification.prototype.unregister = function(successCallback, errorCallback, options) {
23+
if (errorCallback == null) { errorCallback = function() {}}
924

10-
if (typeof errorCallback != "function") {
11-
console.log("PushNotification.register failure: failure parameter not a function");
12-
return;
13-
}
25+
if (typeof errorCallback != "function") {
26+
console.log("PushNotification.unregister failure: failure parameter not a function");
27+
return
28+
}
1429

15-
if (typeof successCallback != "function") {
16-
console.log("PushNotification.register failure: success callback parameter must be a function");
17-
return;
18-
}
30+
if (typeof successCallback != "function") {
31+
console.log("PushNotification.unregister failure: success callback parameter must be a function");
32+
return
33+
}
1934

20-
cordova.exec(successCallback, errorCallback, "PushPlugin", "register", [options]);
21-
};
35+
cordova.exec(successCallback, errorCallback, "PushPlugin", "unregister", [options]);
36+
};
2237

23-
// Call this to unregister for push notifications
24-
PushNotification.prototype.unregister = function(successCallback, errorCallback) {
25-
if (errorCallback == null) { errorCallback = function() {}}
38+
// Call this if you want to show toast notification on WP8
39+
PushNotification.prototype.showToastNotification = function (successCallback, errorCallback, options) {
40+
if (errorCallback == null) { errorCallback = function () { } }
2641

27-
if (typeof errorCallback != "function") {
28-
console.log("PushNotification.unregister failure: failure parameter not a function");
29-
return;
30-
}
42+
if (typeof errorCallback != "function") {
43+
console.log("PushNotification.register failure: failure parameter not a function");
44+
return
45+
}
3146

32-
if (typeof successCallback != "function") {
33-
console.log("PushNotification.unregister failure: success callback parameter must be a function");
34-
return;
35-
}
47+
cordova.exec(successCallback, errorCallback, "PushPlugin", "showToastNotification", [options]);
48+
}
3649

37-
cordova.exec(successCallback, errorCallback, "PushPlugin", "unregister", []);
38-
};
39-
40-
41-
// Call this to set the application icon badge
42-
PushNotification.prototype.setApplicationIconBadgeNumber = function(successCallback, badge) {
43-
if (errorCallback == null) { errorCallback = function() {}}
50+
// Call this to set the application icon badge
51+
PushNotification.prototype.setApplicationIconBadgeNumber = function(successCallback, errorCallback, badge) {
52+
if (errorCallback == null) { errorCallback = function() {}}
4453

45-
if (typeof errorCallback != "function") {
46-
console.log("PushNotification.setApplicationIconBadgeNumber failure: failure parameter not a function");
47-
return;
48-
}
54+
if (typeof errorCallback != "function") {
55+
console.log("PushNotification.setApplicationIconBadgeNumber failure: failure parameter not a function");
56+
return
57+
}
4958

50-
if (typeof successCallback != "function") {
51-
console.log("PushNotification.setApplicationIconBadgeNumber failure: success callback parameter must be a function");
52-
return;
53-
}
59+
if (typeof successCallback != "function") {
60+
console.log("PushNotification.setApplicationIconBadgeNumber failure: success callback parameter must be a function");
61+
return
62+
}
5463

55-
cordova.exec(successCallback, successCallback, "PushPlugin", "setApplicationIconBadgeNumber", [{badge: badge}]);
56-
};
64+
cordova.exec(successCallback, errorCallback, "PushPlugin", "setApplicationIconBadgeNumber", [{badge: badge}]);
65+
};
5766

5867
//-------------------------------------------------------------------
5968

60-
if(!window.plugins) {
61-
window.plugins = {};
69+
if (!window.plugins) {
70+
window.plugins = {};
6271
}
6372
if (!window.plugins.pushNotification) {
64-
window.plugins.pushNotification = new PushNotification();
73+
window.plugins.pushNotification = new PushNotification();
74+
}
75+
76+
if (typeof module != 'undefined' && module.exports) {
77+
module.exports = PushNotification;
6578
}

Example/www/index.html

Lines changed: 30 additions & 30 deletions
Original file line numberDiff line numberDiff line change
@@ -14,22 +14,22 @@
1414
To add them via the CLI:
1515
$ cordova plugin add org.apache.cordova.device
1616
$ cordova plugin add org.apache.cordova.media
17-
*/
17+
*/
1818
<!--<script type="text/javascript" charset="utf-8" src="cordova-2.2.0.js"></script>-->
1919
<script type="text/javascript" charset="utf-8" src="phonegap.js"></script>
2020
<script type="text/javascript" charset="utf-8" src="jquery_1.5.2.min.js"></script>
2121
<script type="text/javascript" src="PushNotification.js"></script>
22-
22+
2323
<script type="text/javascript">
2424
var pushNotification;
25-
25+
2626
function onDeviceReady() {
2727
$("#app-status-ul").append('<li>deviceready event received</li>');
28-
28+
2929
document.addEventListener("backbutton", function(e)
3030
{
3131
$("#app-status-ul").append('<li>backbutton event received</li>');
32-
32+
3333
if( $("#home").length > 0)
3434
{
3535
// call this to get a new token each time. don't call it to reuse existing token.
@@ -42,9 +42,9 @@
4242
navigator.app.backHistory();
4343
}
4444
}, false);
45-
46-
try
47-
{
45+
46+
try
47+
{
4848
pushNotification = window.plugins.pushNotification;
4949
$("#app-status-ul").append('<li>registering ' + device.platform + '</li>');
5050
if (device.platform == 'android' || device.platform == 'Android' ||
@@ -54,37 +54,37 @@
5454
pushNotification.register(tokenHandler, errorHandler, {"badge":"true","sound":"true","alert":"true","ecb":"onNotificationAPN"}); // required!
5555
}
5656
}
57-
catch(err)
58-
{
59-
txt="There was an error on this page.\n\n";
60-
txt+="Error description: " + err.message + "\n\n";
61-
alert(txt);
62-
}
57+
catch(err)
58+
{
59+
txt="There was an error on this page.\n\n";
60+
txt+="Error description: " + err.message + "\n\n";
61+
alert(txt);
62+
}
6363
}
64-
64+
6565
// handle APNS notifications for iOS
6666
function onNotificationAPN(e) {
6767
if (e.alert) {
6868
$("#app-status-ul").append('<li>push-notification: ' + e.alert + '</li>');
6969
// showing an alert also requires the org.apache.cordova.dialogs plugin
7070
navigator.notification.alert(e.alert);
7171
}
72-
72+
7373
if (e.sound) {
7474
// playing a sound also requires the org.apache.cordova.media plugin
7575
var snd = new Media(e.sound);
7676
snd.play();
7777
}
78-
78+
7979
if (e.badge) {
80-
pushNotification.setApplicationIconBadgeNumber(successHandler, e.badge);
80+
pushNotification.setApplicationIconBadgeNumber(successHandler, errorHandler, e.badge);
8181
}
8282
}
83-
83+
8484
// handle GCM notifications for Android
8585
function onNotification(e) {
8686
$("#app-status-ul").append('<li>EVENT -> RECEIVED:' + e.event + '</li>');
87-
87+
8888
switch( e.event )
8989
{
9090
case 'registered':
@@ -96,15 +96,15 @@
9696
console.log("regID = " + e.regid);
9797
}
9898
break;
99-
99+
100100
case 'message':
101101
// if this flag is set, this notification happened while we were in the foreground.
102102
// you might want to play a sound to get the user's attention, throw up a dialog, etc.
103103
if (e.foreground)
104104
{
105105
$("#app-status-ul").append('<li>--INLINE NOTIFICATION--' + '</li>');
106-
107-
// on Android soundname is outside the payload.
106+
107+
// on Android soundname is outside the payload.
108108
// On Amazon FireOS all custom attributes are contained within payload
109109
var soundfile = e.soundname || e.payload.sound;
110110
// if the notification contains a soundname, play it.
@@ -120,38 +120,38 @@
120120
else
121121
$("#app-status-ul").append('<li>--BACKGROUND NOTIFICATION--' + '</li>');
122122
}
123-
123+
124124
$("#app-status-ul").append('<li>MESSAGE -> MSG: ' + e.payload.message + '</li>');
125125
//android only
126126
$("#app-status-ul").append('<li>MESSAGE -> MSGCNT: ' + e.payload.msgcnt + '</li>');
127127
//amazon-fireos only
128128
$("#app-status-ul").append('<li>MESSAGE -> TIMESTAMP: ' + e.payload.timeStamp + '</li>');
129129
break;
130-
130+
131131
case 'error':
132132
$("#app-status-ul").append('<li>ERROR -> MSG:' + e.msg + '</li>');
133133
break;
134-
134+
135135
default:
136136
$("#app-status-ul").append('<li>EVENT -> Unknown, an event was received and we do not know what it is</li>');
137137
break;
138138
}
139139
}
140-
140+
141141
function tokenHandler (result) {
142142
$("#app-status-ul").append('<li>token: '+ result +'</li>');
143143
// Your iOS push server needs to know the token before it can push to this device
144144
// here is where you might want to send it the token for later use.
145145
}
146-
146+
147147
function successHandler (result) {
148148
$("#app-status-ul").append('<li>success:'+ result +'</li>');
149149
}
150-
150+
151151
function errorHandler (error) {
152152
$("#app-status-ul").append('<li>error:'+ error +'</li>');
153153
}
154-
154+
155155
document.addEventListener('deviceready', onDeviceReady, true);
156156

157157
</script>

www/PushNotification.js

Lines changed: 12 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,6 @@
11
var PushNotification = function() {
22
};
33

4-
54
// Call this to register for push notifications. Content of [options] depends on whether we are working with APNS (iOS) or GCM (Android)
65
PushNotification.prototype.register = function(successCallback, errorCallback, options) {
76
if (errorCallback == null) { errorCallback = function() {}}
@@ -36,17 +35,18 @@ PushNotification.prototype.unregister = function(successCallback, errorCallback,
3635
cordova.exec(successCallback, errorCallback, "PushPlugin", "unregister", [options]);
3736
};
3837

39-
// Call this if you want to show toast notification on WP8
40-
PushNotification.prototype.showToastNotification = function (successCallback, errorCallback, options) {
41-
if (errorCallback == null) { errorCallback = function () { } }
42-
43-
if (typeof errorCallback != "function") {
44-
console.log("PushNotification.register failure: failure parameter not a function");
45-
return
46-
}
38+
// Call this if you want to show toast notification on WP8
39+
PushNotification.prototype.showToastNotification = function (successCallback, errorCallback, options) {
40+
if (errorCallback == null) { errorCallback = function () { } }
4741

48-
cordova.exec(successCallback, errorCallback, "PushPlugin", "showToastNotification", [options]);
42+
if (typeof errorCallback != "function") {
43+
console.log("PushNotification.register failure: failure parameter not a function");
44+
return
4945
}
46+
47+
cordova.exec(successCallback, errorCallback, "PushPlugin", "showToastNotification", [options]);
48+
}
49+
5050
// Call this to set the application icon badge
5151
PushNotification.prototype.setApplicationIconBadgeNumber = function(successCallback, errorCallback, badge) {
5252
if (errorCallback == null) { errorCallback = function() {}}
@@ -66,7 +66,7 @@ PushNotification.prototype.setApplicationIconBadgeNumber = function(successCallb
6666

6767
//-------------------------------------------------------------------
6868

69-
if(!window.plugins) {
69+
if (!window.plugins) {
7070
window.plugins = {};
7171
}
7272
if (!window.plugins.pushNotification) {
@@ -75,4 +75,4 @@ if (!window.plugins.pushNotification) {
7575

7676
if (typeof module != 'undefined' && module.exports) {
7777
module.exports = PushNotification;
78-
}
78+
}

0 commit comments

Comments
 (0)