|
5 | 5 | </head>
|
6 | 6 | <body>
|
7 | 7 |
|
8 |
| -/* |
| 8 | +<!-- |
9 | 9 | NOTE:
|
10 | 10 | This demo uses these plugins:
|
11 | 11 | Cordova Device Plugin: http://plugins.cordova.io/#/package/org.apache.cordova.device
|
|
14 | 14 | To add them via the CLI:
|
15 | 15 | $ cordova plugin add org.apache.cordova.device
|
16 | 16 | $ cordova plugin add org.apache.cordova.media
|
17 |
| -*/ |
18 |
| - <!--<script type="text/javascript" charset="utf-8" src="cordova-2.2.0.js"></script>--> |
19 |
| - <script type="text/javascript" charset="utf-8" src="phonegap.js"></script> |
20 |
| - <script type="text/javascript" charset="utf-8" src="jquery_1.5.2.min.js"></script> |
21 |
| - <script type="text/javascript" src="PushNotification.js"></script> |
22 |
| - |
| 17 | +--> |
| 18 | + |
| 19 | + <script type="text/javascript" charset="utf-8" src="cordova.js"></script> |
| 20 | + <script type="text/javascript" charset="utf-8" src="https://code.jquery.com/jquery-1.11.1.min.js"></script> |
| 21 | + |
23 | 22 | <script type="text/javascript">
|
24 | 23 | var pushNotification;
|
25 |
| - |
| 24 | + |
26 | 25 | function onDeviceReady() {
|
27 | 26 | $("#app-status-ul").append('<li>deviceready event received</li>');
|
28 |
| - |
| 27 | + |
29 | 28 | document.addEventListener("backbutton", function(e)
|
30 | 29 | {
|
31 | 30 | $("#app-status-ul").append('<li>backbutton event received</li>');
|
32 |
| - |
| 31 | + |
33 | 32 | if( $("#home").length > 0)
|
34 | 33 | {
|
35 | 34 | // call this to get a new token each time. don't call it to reuse existing token.
|
|
42 | 41 | navigator.app.backHistory();
|
43 | 42 | }
|
44 | 43 | }, false);
|
45 |
| - |
46 |
| - try |
47 |
| - { |
| 44 | + |
| 45 | + try |
| 46 | + { |
48 | 47 | pushNotification = window.plugins.pushNotification;
|
49 | 48 | $("#app-status-ul").append('<li>registering ' + device.platform + '</li>');
|
50 | 49 | if (device.platform == 'android' || device.platform == 'Android' ||
|
|
54 | 53 | pushNotification.register(tokenHandler, errorHandler, {"badge":"true","sound":"true","alert":"true","ecb":"onNotificationAPN"}); // required!
|
55 | 54 | }
|
56 | 55 | }
|
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 |
| - } |
| 56 | + catch(err) |
| 57 | + { |
| 58 | + txt="There was an error on this page.\n\n"; |
| 59 | + txt+="Error description: " + err.message + "\n\n"; |
| 60 | + alert(txt); |
| 61 | + } |
63 | 62 | }
|
64 |
| - |
| 63 | + |
65 | 64 | // handle APNS notifications for iOS
|
66 | 65 | function onNotificationAPN(e) {
|
67 | 66 | if (e.alert) {
|
68 | 67 | $("#app-status-ul").append('<li>push-notification: ' + e.alert + '</li>');
|
69 | 68 | // showing an alert also requires the org.apache.cordova.dialogs plugin
|
70 | 69 | navigator.notification.alert(e.alert);
|
71 | 70 | }
|
72 |
| - |
| 71 | + |
73 | 72 | if (e.sound) {
|
74 | 73 | // playing a sound also requires the org.apache.cordova.media plugin
|
75 | 74 | var snd = new Media(e.sound);
|
76 | 75 | snd.play();
|
77 | 76 | }
|
78 |
| - |
| 77 | + |
79 | 78 | if (e.badge) {
|
80 | 79 | pushNotification.setApplicationIconBadgeNumber(successHandler, e.badge);
|
81 | 80 | }
|
82 | 81 | }
|
83 |
| - |
| 82 | + |
84 | 83 | // handle GCM notifications for Android
|
85 | 84 | function onNotification(e) {
|
86 | 85 | $("#app-status-ul").append('<li>EVENT -> RECEIVED:' + e.event + '</li>');
|
87 |
| - |
| 86 | + |
88 | 87 | switch( e.event )
|
89 | 88 | {
|
90 | 89 | case 'registered':
|
|
96 | 95 | console.log("regID = " + e.regid);
|
97 | 96 | }
|
98 | 97 | break;
|
99 |
| - |
| 98 | + |
100 | 99 | case 'message':
|
101 | 100 | // if this flag is set, this notification happened while we were in the foreground.
|
102 | 101 | // you might want to play a sound to get the user's attention, throw up a dialog, etc.
|
103 | 102 | if (e.foreground)
|
104 | 103 | {
|
105 | 104 | $("#app-status-ul").append('<li>--INLINE NOTIFICATION--' + '</li>');
|
106 |
| - |
107 |
| - // on Android soundname is outside the payload. |
| 105 | + |
| 106 | + // on Android soundname is outside the payload. |
108 | 107 | // On Amazon FireOS all custom attributes are contained within payload
|
109 | 108 | var soundfile = e.soundname || e.payload.sound;
|
110 | 109 | // if the notification contains a soundname, play it.
|
|
120 | 119 | else
|
121 | 120 | $("#app-status-ul").append('<li>--BACKGROUND NOTIFICATION--' + '</li>');
|
122 | 121 | }
|
123 |
| - |
| 122 | + |
124 | 123 | $("#app-status-ul").append('<li>MESSAGE -> MSG: ' + e.payload.message + '</li>');
|
125 | 124 | //android only
|
126 | 125 | $("#app-status-ul").append('<li>MESSAGE -> MSGCNT: ' + e.payload.msgcnt + '</li>');
|
127 | 126 | //amazon-fireos only
|
128 | 127 | $("#app-status-ul").append('<li>MESSAGE -> TIMESTAMP: ' + e.payload.timeStamp + '</li>');
|
129 | 128 | break;
|
130 |
| - |
| 129 | + |
131 | 130 | case 'error':
|
132 | 131 | $("#app-status-ul").append('<li>ERROR -> MSG:' + e.msg + '</li>');
|
133 | 132 | break;
|
134 |
| - |
| 133 | + |
135 | 134 | default:
|
136 | 135 | $("#app-status-ul").append('<li>EVENT -> Unknown, an event was received and we do not know what it is</li>');
|
137 | 136 | break;
|
138 | 137 | }
|
139 | 138 | }
|
140 |
| - |
| 139 | + |
141 | 140 | function tokenHandler (result) {
|
142 | 141 | $("#app-status-ul").append('<li>token: '+ result +'</li>');
|
143 | 142 | // Your iOS push server needs to know the token before it can push to this device
|
144 | 143 | // here is where you might want to send it the token for later use.
|
145 | 144 | }
|
146 |
| - |
| 145 | + |
147 | 146 | function successHandler (result) {
|
148 | 147 | $("#app-status-ul").append('<li>success:'+ result +'</li>');
|
149 | 148 | }
|
150 |
| - |
| 149 | + |
151 | 150 | function errorHandler (error) {
|
152 | 151 | $("#app-status-ul").append('<li>error:'+ error +'</li>');
|
153 | 152 | }
|
154 |
| - |
| 153 | + |
155 | 154 | document.addEventListener('deviceready', onDeviceReady, true);
|
156 | 155 |
|
157 | 156 | </script>
|
|
0 commit comments