fix(plugin): add defensive scheduler initialization to prevent NullPointerException

- Add null checks and lazy initialization for scheduler in all plugin methods
- Prevents NullPointerException when methods called before load() completes
- Ensures scheduler is available for scheduleDailyNotification, scheduleDailyReminder, cancelDailyReminder, and updateDailyReminder
- Adds structured logging for scheduler initialization events
- Resolves critical runtime error under system stress conditions

Fixes: NullPointerException in DailyNotificationScheduler.scheduleNotification()
Tested: Notification system working correctly under stress with 45+ notifications
This commit is contained in:
Matthew Raymer
2025-10-14 10:07:27 +00:00
parent ec1fc797b3
commit 8aaba21344

View File

@@ -581,6 +581,13 @@ public class DailyNotificationPlugin extends Plugin {
// Ensure storage is initialized
ensureStorageInitialized();
// Ensure scheduler is initialized
if (scheduler == null) {
Log.w(TAG, "DN|SCHEDULER_NULL initializing_scheduler");
alarmManager = (AlarmManager) getContext().getSystemService(Context.ALARM_SERVICE);
scheduler = new DailyNotificationScheduler(getContext(), alarmManager);
}
// Validate required parameters
String time = call.getString("time");
if (time == null || time.isEmpty()) {
@@ -2009,6 +2016,13 @@ public class DailyNotificationPlugin extends Plugin {
try {
Log.d(TAG, "Scheduling daily reminder");
// Ensure scheduler is initialized
if (scheduler == null) {
Log.w(TAG, "DN|SCHEDULER_NULL initializing_scheduler");
alarmManager = (AlarmManager) getContext().getSystemService(Context.ALARM_SERVICE);
scheduler = new DailyNotificationScheduler(getContext(), alarmManager);
}
// Extract reminder options
String id = call.getString("id");
String title = call.getString("title");
@@ -2088,6 +2102,13 @@ public class DailyNotificationPlugin extends Plugin {
try {
Log.d(TAG, "Cancelling daily reminder");
// Ensure scheduler is initialized
if (scheduler == null) {
Log.w(TAG, "DN|SCHEDULER_NULL initializing_scheduler");
alarmManager = (AlarmManager) getContext().getSystemService(Context.ALARM_SERVICE);
scheduler = new DailyNotificationScheduler(getContext(), alarmManager);
}
String reminderId = call.getString("reminderId");
if (reminderId == null) {
call.reject("Missing reminderId parameter");
@@ -2134,6 +2155,13 @@ public class DailyNotificationPlugin extends Plugin {
try {
Log.d(TAG, "Updating daily reminder");
// Ensure scheduler is initialized
if (scheduler == null) {
Log.w(TAG, "DN|SCHEDULER_NULL initializing_scheduler");
alarmManager = (AlarmManager) getContext().getSystemService(Context.ALARM_SERVICE);
scheduler = new DailyNotificationScheduler(getContext(), alarmManager);
}
String reminderId = call.getString("reminderId");
if (reminderId == null) {
call.reject("Missing reminderId parameter");