1
1
'use strict' ;
2
2
3
3
var React = require ( 'react-native' ) ;
4
- var { DeviceEventEmitter } = React ;
4
+ var { DeviceEventEmitter} = React ;
5
5
6
6
var NotificationModule = require ( 'react-native' ) . NativeModules . NotificationModule ;
7
7
@@ -12,7 +12,8 @@ var Notification = {
12
12
NotificationModule . rGetApplicationName ( function ( e ) { } , function ( applicationName ) {
13
13
14
14
// Set defaults
15
- if ( ! attributes . subject ) attributes . subject = applicationName ;
15
+ if ( ! attributes . subject )
16
+ attributes . subject = applicationName ;
16
17
attributes = encodeNativeNotification ( attributes ) ;
17
18
18
19
NotificationModule . rCreate ( attributes . id , attributes , reject , function ( notification ) {
@@ -64,32 +65,33 @@ var Notification = {
64
65
} ) ;
65
66
} ,
66
67
67
- addListener : function ( type , listener ) {
68
+ addListener : function ( type , listener , failListener ) {
68
69
switch ( type ) {
69
70
case 'press' :
70
71
case 'click' :
71
72
DeviceEventEmitter . addListener ( 'sysNotificationClick' , listener ) ;
72
-
73
+ break ;
74
+ case 'initial' :
73
75
NotificationModule . getInitialSysNotification ( function ( initialSysNotificationId ,
74
- initialSysNotificationAction ,
75
- initialSysNotificationPayload ) {
76
+ initialSysNotificationAction ,
77
+ initialSysNotificationPayload ) {
76
78
if ( initialSysNotificationId ) {
77
79
var event = {
78
80
action : initialSysNotificationAction ,
79
81
payload : JSON . parse ( initialSysNotificationPayload )
80
82
}
81
-
82
83
listener ( event ) ;
83
-
84
+
84
85
NotificationModule . removeInitialSysNotification ( ) ;
86
+ } else {
87
+ failListener ( ) ;
85
88
}
86
89
} ) ;
87
-
88
90
break ;
89
91
}
90
92
} ,
91
93
92
- removeAllListeners : function ( type ) {
94
+ removeAllListeners : function ( type ) {
93
95
switch ( type ) {
94
96
case 'press' :
95
97
case 'click' :
@@ -105,13 +107,19 @@ module.exports = Notification;
105
107
106
108
// Encode the JS notification to pass into the native model
107
109
function encodeNativeNotification ( attributes ) {
108
- if ( typeof attributes === 'string' ) attributes = JSON . parse ( attributes ) ;
110
+ if ( typeof attributes === 'string' )
111
+ attributes = JSON . parse ( attributes ) ;
109
112
// Set defaults
110
- if ( ! attributes . smallIcon ) attributes . smallIcon = 'ic_launcher' ;
111
- if ( ! attributes . id ) attributes . id = parseInt ( Math . random ( ) * 100000 ) ;
112
- if ( ! attributes . action ) attributes . action = 'DEFAULT' ;
113
- if ( ! attributes . payload ) attributes . payload = { } ;
114
- if ( attributes . autoClear === undefined ) attributes . autoClear = true ;
113
+ if ( ! attributes . smallIcon )
114
+ attributes . smallIcon = 'ic_launcher' ;
115
+ if ( ! attributes . id )
116
+ attributes . id = parseInt ( Math . random ( ) * 100000 ) ;
117
+ if ( ! attributes . action )
118
+ attributes . action = 'DEFAULT' ;
119
+ if ( ! attributes . payload )
120
+ attributes . payload = { } ;
121
+ if ( attributes . autoClear === undefined )
122
+ attributes . autoClear = true ;
115
123
if ( attributes . tickerText === undefined ) {
116
124
if ( attributes . subject ) {
117
125
attributes . tickerText = attributes . subject + ': ' + attributes . message ;
@@ -120,18 +128,25 @@ function encodeNativeNotification(attributes) {
120
128
}
121
129
}
122
130
123
- if ( attributes . priority === undefined ) attributes . priority = 1 ;
124
- if ( attributes . sound === undefined ) attributes . sound = 'default' ;
125
- if ( attributes . vibrate === undefined ) attributes . vibrate = 'default' ;
126
- if ( attributes . lights === undefined ) attributes . lights = 'default' ;
131
+ if ( attributes . priority === undefined )
132
+ attributes . priority = 1 ;
133
+ if ( attributes . sound === undefined )
134
+ attributes . sound = 'default' ;
135
+ if ( attributes . vibrate === undefined )
136
+ attributes . vibrate = 'default' ;
137
+ if ( attributes . lights === undefined )
138
+ attributes . lights = 'default' ;
127
139
128
140
attributes . delayed = ( attributes . delay !== undefined ) ;
129
141
attributes . scheduled = ( attributes . sendAt !== undefined ) ;
130
142
131
143
// Ensure date are Dates
132
- if ( attributes . sendAt && typeof attributes . sendAt !== 'object' ) attributes . sendAt = new Date ( attributes . sendAt ) ;
133
- if ( attributes . endAt && typeof attributes . endAt !== 'object' ) attributes . endAt = new Date ( attributes . endAt ) ;
134
- if ( attributes . when && typeof attributes . when !== 'object' ) attributes . when = new Date ( attributes . when ) ;
144
+ if ( attributes . sendAt && typeof attributes . sendAt !== 'object' )
145
+ attributes . sendAt = new Date ( attributes . sendAt ) ;
146
+ if ( attributes . endAt && typeof attributes . endAt !== 'object' )
147
+ attributes . endAt = new Date ( attributes . endAt ) ;
148
+ if ( attributes . when && typeof attributes . when !== 'object' )
149
+ attributes . when = new Date ( attributes . when ) ;
135
150
136
151
// Unfold sendAt
137
152
if ( attributes . sendAt !== undefined ) {
@@ -144,9 +159,12 @@ function encodeNativeNotification(attributes) {
144
159
}
145
160
146
161
// Convert date objects into number
147
- if ( attributes . sendAt ) attributes . sendAt = attributes . sendAt . getTime ( ) ;
148
- if ( attributes . endAt ) attributes . endAt = attributes . endAt . getTime ( ) ;
149
- if ( attributes . when ) attributes . when = attributes . when . getTime ( ) ;
162
+ if ( attributes . sendAt )
163
+ attributes . sendAt = attributes . sendAt . getTime ( ) ;
164
+ if ( attributes . endAt )
165
+ attributes . endAt = attributes . endAt . getTime ( ) ;
166
+ if ( attributes . when )
167
+ attributes . when = attributes . when . getTime ( ) ;
150
168
151
169
// Prepare scheduled notifications
152
170
if ( attributes . sendAt !== undefined ) {
@@ -201,13 +219,18 @@ function encodeNativeNotification(attributes) {
201
219
202
220
// Convert long numbers into string before passing them into native modle,
203
221
// incase of integer overflow
204
- if ( attributes . sendAt ) attributes . sendAt = attributes . sendAt . toString ( ) ;
205
- if ( attributes . endAt ) attributes . endAt = attributes . endAt . toString ( ) ;
206
- if ( attributes . when ) attributes . when = attributes . when . toString ( ) ;
207
- if ( attributes . repeatEvery ) attributes . repeatEvery = attributes . repeatEvery . toString ( ) ;
222
+ if ( attributes . sendAt )
223
+ attributes . sendAt = attributes . sendAt . toString ( ) ;
224
+ if ( attributes . endAt )
225
+ attributes . endAt = attributes . endAt . toString ( ) ;
226
+ if ( attributes . when )
227
+ attributes . when = attributes . when . toString ( ) ;
228
+ if ( attributes . repeatEvery )
229
+ attributes . repeatEvery = attributes . repeatEvery . toString ( ) ;
208
230
209
231
// Convert float into integer
210
- if ( attributes . progress ) attributes . progress = attributes . progress * 1000 ;
232
+ if ( attributes . progress )
233
+ attributes . progress = attributes . progress * 1000 ;
211
234
212
235
// Stringify the payload
213
236
attributes . payload = JSON . stringify ( attributes . payload ) ;
@@ -218,18 +241,24 @@ function encodeNativeNotification(attributes) {
218
241
// Decode the notification data from the native module to pass into JS
219
242
function decodeNativeNotification ( attributes ) {
220
243
// Convert dates back to date object
221
- if ( attributes . sendAt ) attributes . sendAt = new Date ( parseInt ( attributes . sendAt ) ) ;
222
- if ( attributes . endAt ) attributes . endAt = new Date ( parseInt ( attributes . endAt ) ) ;
223
- if ( attributes . when ) attributes . when = new Date ( parseInt ( attributes . when ) ) ;
244
+ if ( attributes . sendAt )
245
+ attributes . sendAt = new Date ( parseInt ( attributes . sendAt ) ) ;
246
+ if ( attributes . endAt )
247
+ attributes . endAt = new Date ( parseInt ( attributes . endAt ) ) ;
248
+ if ( attributes . when )
249
+ attributes . when = new Date ( parseInt ( attributes . when ) ) ;
224
250
225
251
// Parse possible integer
226
- if ( parseInt ( attributes . repeatEvery ) . toString ( ) === attributes . repeatEvery ) attributes . repeatEvery = parseInt ( attributes . repeatEvery ) ;
252
+ if ( parseInt ( attributes . repeatEvery ) . toString ( ) === attributes . repeatEvery )
253
+ attributes . repeatEvery = parseInt ( attributes . repeatEvery ) ;
227
254
228
255
// Convert integer into float
229
- if ( attributes . progress ) attributes . progress = attributes . progress / 1000 ;
256
+ if ( attributes . progress )
257
+ attributes . progress = attributes . progress / 1000 ;
230
258
231
259
// Parse the payload
232
- if ( attributes . payload ) attributes . payload = JSON . parse ( attributes . payload ) ;
260
+ if ( attributes . payload )
261
+ attributes . payload = JSON . parse ( attributes . payload ) ;
233
262
234
263
return attributes ;
235
264
}
0 commit comments