Skip to content
This repository was archived by the owner on Jan 14, 2025. It is now read-only.

Commit ad3888b

Browse files
committed
Prevent crash for NaN.
1 parent a799161 commit ad3888b

File tree

2 files changed

+54
-11
lines changed

2 files changed

+54
-11
lines changed

android/src/main/java/com/dieam/reactnativepushnotification/modules/RNPushNotificationHelper.java

Lines changed: 25 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -80,13 +80,19 @@ private AlarmManager getAlarmManager() {
8080
}
8181

8282
private PendingIntent toScheduleNotificationIntent(Bundle bundle) {
83-
int notificationID = Integer.parseInt(bundle.getString("id"));
83+
try {
84+
int notificationID = Integer.parseInt(bundle.getString("id"));
85+
86+
Intent notificationIntent = new Intent(context, RNPushNotificationPublisher.class);
87+
notificationIntent.putExtra(RNPushNotificationPublisher.NOTIFICATION_ID, notificationID);
88+
notificationIntent.putExtras(bundle);
8489

85-
Intent notificationIntent = new Intent(context, RNPushNotificationPublisher.class);
86-
notificationIntent.putExtra(RNPushNotificationPublisher.NOTIFICATION_ID, notificationID);
87-
notificationIntent.putExtras(bundle);
90+
return PendingIntent.getBroadcast(context, notificationID, notificationIntent, PendingIntent.FLAG_UPDATE_CURRENT);
91+
} catch (Exception e) {
92+
Log.e(LOG_TAG, "Unable to parse Notification ID", e);
93+
}
8894

89-
return PendingIntent.getBroadcast(context, notificationID, notificationIntent, PendingIntent.FLAG_UPDATE_CURRENT);
95+
return null;
9096
}
9197

9298
public void sendNotificationScheduled(Bundle bundle) {
@@ -137,6 +143,10 @@ public void sendNotificationScheduledCore(Bundle bundle) {
137143
// notification to the user
138144
PendingIntent pendingIntent = toScheduleNotificationIntent(bundle);
139145

146+
if(pendingIntent == null) {
147+
return;
148+
}
149+
140150
Log.d(LOG_TAG, String.format("Setting a notification with id %s at time %s",
141151
bundle.getString("id"), Long.toString(fireDate)));
142152
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.KITKAT) {
@@ -636,7 +646,11 @@ private void cancelScheduledNotification(String notificationIDString) {
636646
// remove it from the alarm manger schedule
637647
Bundle b = new Bundle();
638648
b.putString("id", notificationIDString);
639-
getAlarmManager().cancel(toScheduleNotificationIntent(b));
649+
PendingIntent pendingIntent = toScheduleNotificationIntent(b);
650+
651+
if(pendingIntent != null) {
652+
getAlarmManager().cancel(pendingIntent);
653+
}
640654

641655
if (scheduledNotificationsPersistence.contains(notificationIDString)) {
642656
// remove it from local storage
@@ -650,7 +664,11 @@ private void cancelScheduledNotification(String notificationIDString) {
650664
// removed it from the notification center
651665
NotificationManager notificationManager = notificationManager();
652666

653-
notificationManager.cancel(Integer.parseInt(notificationIDString));
667+
try {
668+
notificationManager.cancel(Integer.parseInt(notificationIDString));
669+
} catch (Exception e) {
670+
Log.e(LOG_TAG, "Unable to parse Notification ID " + notificationIDString, e);
671+
}
654672
}
655673

656674
private NotificationManager notificationManager() {

index.js

Lines changed: 29 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -141,13 +141,26 @@ Notifications.localNotification = function(details) {
141141
});
142142
} else {
143143
if(details && typeof details.id === 'number') {
144-
details.id = '' + details.id;
144+
if(isNaN(details.id)) {
145+
console.warn('NaN value has been passed as id');
146+
delete details.id;
147+
}
148+
else {
149+
details.id = '' + details.id;
150+
}
145151
}
146152

147153
if(details && typeof details.number === 'number') {
148-
details.number = '' + details.number;
154+
if(isNaN(details.number)) {
155+
console.warn('NaN value has been passed as number');
156+
delete details.number;
157+
}
158+
else {
159+
details.number = '' + details.number;
160+
}
149161
}
150162

163+
151164
this.handler.presentLocalNotification(details);
152165
}
153166
};
@@ -187,11 +200,23 @@ Notifications.localNotificationSchedule = function(details) {
187200
this.handler.scheduleLocalNotification(iosDetails);
188201
} else {
189202
if(details && typeof details.id === 'number') {
190-
details.id = '' + details.id;
203+
if(isNaN(details.id)) {
204+
console.warn('NaN value has been passed as id');
205+
delete details.id;
206+
}
207+
else {
208+
details.id = '' + details.id;
209+
}
191210
}
192211

193212
if(details && typeof details.number === 'number') {
194-
details.number = '' + details.number;
213+
if(isNaN(details.number)) {
214+
console.warn('NaN value has been passed as number');
215+
delete details.number;
216+
}
217+
else {
218+
details.number = '' + details.number;
219+
}
195220
}
196221

197222
details.fireDate = details.date.getTime();

0 commit comments

Comments
 (0)