Android API 31 level causes error for Flutter local notifications in the app

627 Views Asked by At

As per Google's policy, I updated my targetSdkVersion and compileSdkVersion to 31. But I noticed that the Flutter local notification package now throws error for incoming messages as below: Targeting S+ (version 31 and above) requires that one of FLAG_IMMUTABLE or FLAG_MUTABLE be specified when creating a PendingIntent. Strongly consider using FLAG_IMMUTABLE, only use FLAG_MUTABLE if some functionality depends on the PendingIntent being mutable, e.g. if it needs to be used with inline replies or bubbles.

I went through all the Stack overflow answers related to this error and made following changes:

  1. Added below Java and Kotlin dependencies in build.gradle file:build.gradle file
  2. Added this receiver below activity tag: Receiver tag
  3. Added exported flag under activity tag: Exported flag
  4. Updated gradle version to latest in gradle-wrapper.properties: Latest gradle version I am not using any PendingIntent in my code, and simulating app on simulator with API 31 level. Could someone please help me in resolving this issue with message notifications?
1

There are 1 best solutions below

0
On

Looks like there's an issue in the flutter_local_notifications repo that provides the solution. See the issue here

If you don't want to update the version of the package you can fork the version you're using and fix the issues locally.

  1. You can go to FlutterLocalNotificationsPlugin.java file
  2. Find all error lines, such as PendingIntent pendingIntent = PendingIntent.getBroadcast(context, notificationDetails.id, notificationIntent, PendingIntent.FLAG_UPDATE_CURRENT) and replace with PendingIntent pendingIntent = PendingIntent.getBroadcast(context, notificationDetails.id, notificationIntent, PendingIntent.FLAG_UPDATE_CURRENT | PendingIntent.FLAG_IMMUTABLE)

You have to do this for every instance of PendingIntent. It worked for me.