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:
expo init example-app
cd example-app
expo eject
yarn add react-native-notifications
- make the following changes to
android/build.gradle
buildscript {
ext {
buildToolsVersion = "29.0.3"
minSdkVersion = 21
- compileSdkVersion = 30
+ compileSdkVersion = 31
- targetSdkVersion = 30
+ targetSdkVersion = 31
+ kotlinVersion = "1.6.0"
}
repositories {
google()
mavenCentral()
jcenter()
}
dependencies {
classpath("com.android.tools.build:gradle:4.1.0")
+ classpath "org.jetbrains.kotlin:kotlin-gradle-plugin:$kotlinVersion"
}
}
- in
android/app/src/main/AndroidManifest.xml, add the android:exported="true" attribute to the <activity> tag named "MainActivity"
- add the following to
App.js:
import { Notifications } from "react-native-notifications";
Notifications.postLocalNotification({
title: "Local notification",
body: "This notification was generated by the app!",
extra: "data",
});
npx react-native run-android - crash!
In Android 12, you must specify the mutability of any
PendingIntent, or the app throws anIllegalArgumentException(see docs).Currently, when calling
postLocalNotification, this library tries to create aPendingIntentwithout a flag specifying the mutability, causing this error.NotificationIntentAdapter.javaline 15I believe the fix might be as simple as adding a mutability flag, probably
FLAG_IMMUTABLEsince 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:
I was able to get a minimal repro of this crash with the following steps:
expo init example-appcd example-appexpo ejectyarn add react-native-notificationsandroid/build.gradlebuildscript { ext { buildToolsVersion = "29.0.3" minSdkVersion = 21 - compileSdkVersion = 30 + compileSdkVersion = 31 - targetSdkVersion = 30 + targetSdkVersion = 31 + kotlinVersion = "1.6.0" } repositories { google() mavenCentral() jcenter() } dependencies { classpath("com.android.tools.build:gradle:4.1.0") + classpath "org.jetbrains.kotlin:kotlin-gradle-plugin:$kotlinVersion" } }android/app/src/main/AndroidManifest.xml, add theandroid:exported="true"attribute to the<activity>tag named "MainActivity"App.js:npx react-native run-android- crash!