Skip to content

Commit ed344d1

Browse files
authored
[flutter_local_notifications] Handle scheduled notification receiver null notification (#1999)
* Add null check in receiver * Add log when calling removeNotificationFromCache * Google Java Format --------- Co-authored-by: github-actions <>
1 parent ac69878 commit ed344d1

File tree

1 file changed

+12
-1
lines changed

1 file changed

+12
-1
lines changed

flutter_local_notifications/android/src/main/java/com/dexterous/flutterlocalnotifications/ScheduledNotificationReceiver.java

Lines changed: 12 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,7 @@
44
import android.content.BroadcastReceiver;
55
import android.content.Context;
66
import android.content.Intent;
7+
import android.util.Log;
78

89
import androidx.annotation.Keep;
910
import androidx.core.app.NotificationManagerCompat;
@@ -26,14 +27,24 @@ public void onReceive(final Context context, Intent intent) {
2627
intent.getStringExtra(FlutterLocalNotificationsPlugin.NOTIFICATION_DETAILS);
2728
if (StringUtils.isNullOrEmpty(notificationDetailsJson)) {
2829
// This logic is needed for apps that used the plugin prior to 0.3.4
30+
2931
Notification notification;
32+
int notificationId = intent.getIntExtra("notification_id", 0);
33+
3034
if (android.os.Build.VERSION.SDK_INT >= android.os.Build.VERSION_CODES.TIRAMISU) {
3135
notification = intent.getParcelableExtra("notification", Notification.class);
3236
} else {
3337
notification = intent.getParcelableExtra("notification");
3438
}
39+
40+
if (notification == null) {
41+
// This means the notification is corrupt
42+
FlutterLocalNotificationsPlugin.removeNotificationFromCache(context, notificationId);
43+
Log.e("notification", "Failed to parse a notification from Intent. ID: " + notificationId);
44+
return;
45+
}
46+
3547
notification.when = System.currentTimeMillis();
36-
int notificationId = intent.getIntExtra("notification_id", 0);
3748
NotificationManagerCompat notificationManager = NotificationManagerCompat.from(context);
3849
notificationManager.notify(notificationId, notification);
3950
boolean repeat = intent.getBooleanExtra("repeat", false);

0 commit comments

Comments
 (0)