1
+ <!DOCTYPE HTML>
2
+ < html >
3
+ < head >
4
+ < title > com.PhoneGap.c2dm</ title >
5
+ < script type ="text/javascript " charset ="utf-8 " src ="cordova-2.1.0.js "> </ script >
6
+ < script type ="text/javascript " charset ="utf-8 " src ="jquery_1.5.2.min.js "> </ script >
7
+ < script type ="text/javascript " src ="PushNotification.js "> </ script >
8
+
9
+ < script type ="text/javascript ">
10
+ var pushNotification ;
11
+
12
+ function onLoad ( ) {
13
+ document . addEventListener ( 'deviceready' , onDeviceReady , true ) ;
14
+ }
15
+
16
+ function onDeviceReady ( ) {
17
+ $ ( "#app-status-ul" ) . append ( '<li>deviceready event received</li>' ) ;
18
+
19
+ pushNotification = window . plugins . pushNotification ;
20
+ if ( device . platform == 'android' || device . platform == 'Android' ) {
21
+ pushNotification . register ( successHandler , errorHandler , { "senderID" :"661780372179" , "ecb" :"onNotificationGCM" } ) ;
22
+ } else {
23
+ pushNotification . register ( tokenHandler , errorHandler , { "badge" :"true" , "sound" :"true" , "alert" :"true" , "ecb" :"onNotificationAPN" } ) ;
24
+ }
25
+ }
26
+
27
+ // handle APNS notifications for iOS
28
+ function onNotificationAPN ( event ) {
29
+ if ( event . alert ) {
30
+ $ ( "#app-status-ul" ) . append ( '<li>push-notification: ' + event . alert + '</li>' ) ;
31
+ navigator . notification . alert ( event . alert ) ;
32
+ }
33
+
34
+ if ( event . sound ) {
35
+ var snd = new Media ( event . sound ) ;
36
+ snd . play ( ) ;
37
+ }
38
+
39
+ if ( event . badge ) {
40
+ pushNotification . setApplicationIconBadgeNumber ( successHandler , event . badge ) ;
41
+ }
42
+ }
43
+
44
+ // handle GCM notifications for Android
45
+ function onNotificationGCM ( e ) {
46
+ $ ( "#app-status-ul" ) . append ( '<li>EVENT -> RECEIVED:' + e . event + '</li>' ) ;
47
+
48
+ switch ( e . event )
49
+ {
50
+ case 'registered' :
51
+ if ( e . regid . length > 0 )
52
+ {
53
+ $ ( "#app-status-ul" ) . append ( '<li>REGISTERED -> REGID:' + e . regid + "</li>" ) ;
54
+ // Your GCM push server needs to know the regID before it can push to this device
55
+ // here is where you might want to send it the regID for later use.
56
+ }
57
+ break ;
58
+
59
+ case 'message' :
60
+ $ ( "#app-status-ul" ) . append ( '<li>MESSAGE -> MSG: ' + e . message + '</li>' ) ;
61
+ $ ( "#app-status-ul" ) . append ( '<li>MESSAGE -> MSGCNT: ' + e . msgcnt + '</li>' ) ;
62
+ break ;
63
+
64
+ case 'error' :
65
+ $ ( "#app-status-ul" ) . append ( '<li>ERROR -> MSG:' + e . msg + '</li>' ) ;
66
+ break ;
67
+
68
+ default :
69
+ $ ( "#app-status-ul" ) . append ( '<li>EVENT -> Unknown, an event was received and we do not know what it is</li>' ) ;
70
+ break ;
71
+ }
72
+ }
73
+
74
+ function tokenHandler ( result ) {
75
+ $ ( "#app-status-ul" ) . append ( '<li>token: ' + result + '</li>' ) ;
76
+ // Your iOS push server needs to know the token before it can push to this device
77
+ // here is where you might want to send it the token for later use.
78
+ }
79
+
80
+ function successHandler ( result ) {
81
+ $ ( "#app-status-ul" ) . append ( '<li>success:' + result + '</li>' ) ;
82
+ }
83
+
84
+ function errorHandler ( error ) {
85
+ $ ( "#app-status-ul" ) . append ( '<li>error:' + error + '</li>' ) ;
86
+ }
87
+ </ script >
88
+ </ head >
89
+ < body onload ="onLoad(); ">
90
+ < div id ="app-status-div ">
91
+ < ul id ="app-status-ul ">
92
+ < li > Cordova Google Cloud Messaging Plugin Demo</ li >
93
+ </ ul >
94
+ </ div >
95
+ </ body >
96
+ </ html >
0 commit comments