Skip to content

Commit ce1e48f

Browse files
committed
Write listener for initial system notifications
Modify Notification.addListener('press', ...) to only register an on click listener. To determine if the application was initially opened (not in background) via a notification click create a new listener Notification.addListener('initial',...) to handle this event. Add a new callback to addListener which is called if the app is loaded by the user and not by clicking a notification
1 parent 3345856 commit ce1e48f

File tree

2 files changed

+67
-38
lines changed

2 files changed

+67
-38
lines changed

index.android.js

Lines changed: 66 additions & 37 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
'use strict';
22

33
var React = require('react-native');
4-
var { DeviceEventEmitter } = React;
4+
var {DeviceEventEmitter} = React;
55

66
var NotificationModule = require('react-native').NativeModules.NotificationModule;
77

@@ -12,7 +12,8 @@ var Notification = {
1212
NotificationModule.rGetApplicationName(function(e) {}, function(applicationName) {
1313

1414
// Set defaults
15-
if (!attributes.subject) attributes.subject = applicationName;
15+
if (!attributes.subject)
16+
attributes.subject = applicationName;
1617
attributes = encodeNativeNotification(attributes);
1718

1819
NotificationModule.rCreate(attributes.id, attributes, reject, function(notification) {
@@ -64,32 +65,33 @@ var Notification = {
6465
});
6566
},
6667

67-
addListener: function(type, listener) {
68+
addListener: function(type, listener, failListener) {
6869
switch (type) {
6970
case 'press':
7071
case 'click':
7172
DeviceEventEmitter.addListener('sysNotificationClick', listener);
72-
73+
break;
74+
case 'initial':
7375
NotificationModule.getInitialSysNotification(function(initialSysNotificationId,
74-
initialSysNotificationAction,
75-
initialSysNotificationPayload) {
76+
initialSysNotificationAction,
77+
initialSysNotificationPayload) {
7678
if (initialSysNotificationId) {
7779
var event = {
7880
action: initialSysNotificationAction,
7981
payload: JSON.parse(initialSysNotificationPayload)
8082
}
81-
8283
listener(event);
83-
84+
8485
NotificationModule.removeInitialSysNotification();
86+
} else {
87+
failListener();
8588
}
8689
});
87-
8890
break;
8991
}
9092
},
9193

92-
removeAllListeners: function (type) {
94+
removeAllListeners: function(type) {
9395
switch (type) {
9496
case 'press':
9597
case 'click':
@@ -105,13 +107,19 @@ module.exports = Notification;
105107

106108
// Encode the JS notification to pass into the native model
107109
function encodeNativeNotification(attributes) {
108-
if (typeof attributes === 'string') attributes = JSON.parse(attributes);
110+
if (typeof attributes === 'string')
111+
attributes = JSON.parse(attributes);
109112
// 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;
115123
if (attributes.tickerText === undefined) {
116124
if (attributes.subject) {
117125
attributes.tickerText = attributes.subject + ': ' + attributes.message;
@@ -120,18 +128,25 @@ function encodeNativeNotification(attributes) {
120128
}
121129
}
122130

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';
127139

128140
attributes.delayed = (attributes.delay !== undefined);
129141
attributes.scheduled = (attributes.sendAt !== undefined);
130142

131143
// 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);
135150

136151
// Unfold sendAt
137152
if (attributes.sendAt !== undefined) {
@@ -144,9 +159,12 @@ function encodeNativeNotification(attributes) {
144159
}
145160

146161
// 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();
150168

151169
// Prepare scheduled notifications
152170
if (attributes.sendAt !== undefined) {
@@ -201,13 +219,18 @@ function encodeNativeNotification(attributes) {
201219

202220
// Convert long numbers into string before passing them into native modle,
203221
// 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();
208230

209231
// Convert float into integer
210-
if (attributes.progress) attributes.progress = attributes.progress * 1000;
232+
if (attributes.progress)
233+
attributes.progress = attributes.progress * 1000;
211234

212235
// Stringify the payload
213236
attributes.payload = JSON.stringify(attributes.payload);
@@ -218,18 +241,24 @@ function encodeNativeNotification(attributes) {
218241
// Decode the notification data from the native module to pass into JS
219242
function decodeNativeNotification(attributes) {
220243
// 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));
224250

225251
// 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);
227254

228255
// Convert integer into float
229-
if (attributes.progress) attributes.progress = attributes.progress / 1000;
256+
if (attributes.progress)
257+
attributes.progress = attributes.progress / 1000;
230258

231259
// Parse the payload
232-
if (attributes.payload) attributes.payload = JSON.parse(attributes.payload);
260+
if (attributes.payload)
261+
attributes.payload = JSON.parse(attributes.payload);
233262

234263
return attributes;
235264
}

package.json

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
{
22
"name": "react-native-system-notification",
3-
"version": "0.2.0",
3+
"version": "0.3.0",
44
"repository": {
55
"type": "git",
66
"url": "git+https://github.com/Neson/react-native-system-notification"

0 commit comments

Comments
 (0)