2
0
mirror of synced 2025-02-02 17:43:27 +00:00

Merge pull request #1312 from damienix/1308-fix-scheduled-notifications-after-reboot

[Android] Fix rescheduling notification on phone reboot
This commit is contained in:
Michael Diarmid 2018-07-20 11:16:19 +01:00 committed by GitHub
commit 7acc1f78f2
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23

View File

@ -320,15 +320,19 @@ public class RNFirebaseNotificationManager {
String notificationId = notification.getString("notificationId");
Bundle schedule = notification.getBundle("schedule");
// fireDate is stored in the Bundle as Long after notifications are rescheduled.
// This would lead to a fireDate of 0.0 when trying to extract a Double from the bundle.
// Instead always try extract a Long
// fireDate may be stored in the Bundle as 2 different types that we need to handle:
// 1. Double - when a call comes directly from React
// 2. Long - when notifications are rescheduled from boot service (Bundle is loaded from prefences).
// At the end we need Long value (timestamp) for the scheduler
Long fireDate = -1L;
try {
fireDate = (long) schedule.getDouble("fireDate", -1);
} catch (ClassCastException e) {
fireDate = schedule.getLong("fireDate", -1);
Object fireDateObject = schedule.get("fireDate");
if (fireDateObject instanceof Long) {
fireDate = (Long) fireDateObject;
} else if (fireDateObject instanceof Double) {
Double fireDateDouble = (Double) fireDateObject;
fireDate = fireDateDouble.longValue();
}
if (fireDate == -1) {
if (promise == null) {
Log.e(TAG, "Missing schedule information");
@ -420,14 +424,14 @@ public class RNFirebaseNotificationManager {
return;
}
alarmManager.setRepeating(AlarmManager.RTC_WAKEUP, fireDate.longValue(), interval, pendingIntent);
alarmManager.setRepeating(AlarmManager.RTC_WAKEUP, fireDate, interval, pendingIntent);
} else {
if (schedule.containsKey("exact")
&& schedule.getBoolean("exact")
&& Build.VERSION.SDK_INT >= Build.VERSION_CODES.KITKAT) {
alarmManager.setExact(AlarmManager.RTC_WAKEUP, fireDate.longValue(), pendingIntent);
alarmManager.setExact(AlarmManager.RTC_WAKEUP, fireDate, pendingIntent);
} else {
alarmManager.set(AlarmManager.RTC_WAKEUP, fireDate.longValue(), pendingIntent);
alarmManager.set(AlarmManager.RTC_WAKEUP, fireDate, pendingIntent);
}
}