wix / react-native-notifications

React Native Notifications
MIT License
3.25k stars 764 forks source link

Missing FLAG_IMMUTABLE on PendingIntent causes crash when sending local notification on Android 12 #808

Closed junebugfix closed 2 years ago

junebugfix commented 2 years ago

In Android 12, you must specify the mutability of any PendingIntent, or the app throws an IllegalArgumentException (see docs).

Currently, when calling postLocalNotification, this library tries to create a PendingIntent without a flag specifying the mutability, causing this error. NotificationIntentAdapter.java line 15

return PendingIntent.getService(appContext, (int) System.currentTimeMillis(), intent, PendingIntent.FLAG_ONE_SHOT);

I believe the fix might be as simple as adding a mutability flag, probably FLAG_IMMUTABLE since it seems like that one is used in the majority of cases, but that would change the mutability of the intent, since before Android 12, PendingIntents were considered mutable by default.

Making this change in the same place mentioned above fixed the crash for me:

- return PendingIntent.getService(appContext, (int) System.currentTimeMillis(), intent, PendingIntent.FLAG_ONE_SHOT);
+ return PendingIntent.getService(appContext, (int) System.currentTimeMillis(), intent, PendingIntent.FLAG_ONE_SHOT | PendingIntent.FLAG_IMMUTABLE);

I was able to get a minimal repro of this crash with the following steps:

Notifications.postLocalNotification({ title: "Local notification", body: "This notification was generated by the app!", extra: "data", });


- `npx react-native run-android` - crash!
artdevgame commented 2 years ago

@DanielEliraz @weihangChen Is this something you can help push through to production?

I'm also experiencing the issue and can confirm @hankhester suggestion fixes the issue for me too.

DanielEliraz commented 2 years ago

thanks guys, new version with fix will come soon.

DanielEliraz commented 2 years ago

fixed in 4.2.0