Skip to content

[Android] Fix NPE in setSmallIcon when iconResourceId is null#2806

Open
mtallenca wants to merge 2 commits into
MaikuB:masterfrom
mtallenca:fix/setsmallicon-null-iconresourceid
Open

[Android] Fix NPE in setSmallIcon when iconResourceId is null#2806
mtallenca wants to merge 2 commits into
MaikuB:masterfrom
mtallenca:fix/setsmallicon-null-iconresourceid

Conversation

@mtallenca

Copy link
Copy Markdown

Description

Fixes a NullPointerException that crashes the app when a scheduled notification is delivered on Android.

setSmallIcon falls into its legacy backwards-compat branch when the notification has no icon and no persisted default icon in shared preferences. That branch then calls:

builder.setSmallIcon(notificationDetails.iconResourceId);

iconResourceId is a boxed Integer, so passing it to setSmallIcon(int) auto-unboxes it. When it is null, this throws:

Fatal Exception: java.lang.RuntimeException: Unable to start receiver
  com.dexterous.flutterlocalnotifications.ScheduledNotificationReceiver:
  java.lang.NullPointerException: Attempt to invoke virtual method
  'int java.lang.Integer.intValue()' on a null object reference
    at ...FlutterLocalNotificationsPlugin.setSmallIcon(FlutterLocalNotificationsPlugin.java:490)
    at ...FlutterLocalNotificationsPlugin.createNotification(FlutterLocalNotificationsPlugin.java:391)
    at ...FlutterLocalNotificationsPlugin.showNotification(FlutterLocalNotificationsPlugin.java:1297)
    at ...ScheduledNotificationReceiver.onReceive(ScheduledNotificationReceiver.java:61)

How a notification reaches this state

ScheduledNotificationReceiver.onReceive deserializes the NotificationDetails payload that was persisted at schedule time. A notification serialized by an older version of the plugin (before the notification carried an icon string) deserializes with both icon == null and iconResourceId == null. If that alarm fires after the app has been updated — but before the app has run initialize() again to persist a default icon — all three fall-throughs are hit and the receiver crashes on delivery. This is especially common on devices that auto-update apps in the background without ever being launched.

Fix

Guard the null iconResourceId in the legacy branch and fall back to the application's own icon (context.getApplicationInfo().icon), which is always a valid resource for an installed app, so notification delivery never crashes.

Fixes #298
Fixes #2049

Type of Change

  • 🐛 Bug Fix (non-breaking change which fixes an issue)

setSmallIcon reaches the legacy backwards-compat branch when a scheduled
notification has no icon and no persisted default icon. It then called
builder.setSmallIcon(notificationDetails.iconResourceId) directly, which
auto-unboxes the boxed Integer iconResourceId. When that field is null
(e.g. a notification serialized by an older plugin version that fires
after an app update), this throws:

  NullPointerException: Attempt to invoke virtual method
  'int java.lang.Integer.intValue()' on a null object reference

crashing the ScheduledNotificationReceiver on delivery. Guard the null and
fall back to the application's own icon so delivery never crashes.

Fixes MaikuB#298, MaikuB#2049
…null-iconresourceid

# Conflicts:
#	flutter_local_notifications/CHANGELOG.md
#	flutter_local_notifications/pubspec.yaml
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

App crashing on Android with type java.lang.RuntimeException. Possibly related to small icon NullPointerException in setSmallIcon

1 participant