[Android] Fix NPE in setSmallIcon when iconResourceId is null#2806
Open
mtallenca wants to merge 2 commits into
Open
[Android] Fix NPE in setSmallIcon when iconResourceId is null#2806mtallenca wants to merge 2 commits into
mtallenca wants to merge 2 commits into
Conversation
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
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
Description
Fixes a
NullPointerExceptionthat crashes the app when a scheduled notification is delivered on Android.setSmallIconfalls into its legacy backwards-compat branch when the notification has noiconand no persisted default icon in shared preferences. That branch then calls:iconResourceIdis a boxedInteger, so passing it tosetSmallIcon(int)auto-unboxes it. When it isnull, this throws:How a notification reaches this state
ScheduledNotificationReceiver.onReceivedeserializes theNotificationDetailspayload that was persisted at schedule time. A notification serialized by an older version of the plugin (before the notification carried aniconstring) deserializes with bothicon == nullandiconResourceId == null. If that alarm fires after the app has been updated — but before the app has runinitialize()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
iconResourceIdin 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