Search code examples
androidparse-platformgoogle-cloud-messaging

Crash on Android 13 with Parse SDK: Targeting S+ (version 31 and above) requires that one of FLAG_IMMUTABLE or FLAG_MUTABLE be specified when creating


My app uses Parse SDK for Android and GCM to get notification after a change on Parse database.

The app's onCreate() calls the line:

ParseGCM.register(context);

On Android 13 device, this line causes a RuntimeException exception:

Caused by: java.lang.IllegalArgumentException: com.myapp: 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. at android.app.PendingIntent.checkFlags(PendingIntent.java:401) at android.app.PendingIntent.getBroadcastAsUser(PendingIntent.java:671) at android.app.PendingIntent.getBroadcast(PendingIntent.java:658) at com.firebase.jobdispatcher.GooglePlayDriver.(GooglePlayDriver.java:72) at com.parse.gcm.ParseGCM.register(ParseGCM.java:39)

The crash does not occur on Android 11


Solution

  • Worth checking if your code is dealing with pending intents, because for API level 31 and beyond, you will have to specify the mutability of each PendingIntent object that your app creates. Please just keep in mind things like https://developer.android.com/reference/android/app/PendingIntent#FLAG_IMMUTABLE are as of SDK 23 - hope that's not an issue.

    So if your code was like this:

    PendingIntent pendingIntent = PendingIntent.getActivity(context, <your request code>, 
        intent, 
        PendingIntent.FLAG_UPDATE_CURRENT
    );
    

    you will have to change it to something like this:

    PendingIntent pendingIntent = PendingIntent.getActivity(context, <your request code>,
        intent,
        PendingIntent.FLAG_IMMUTABLE | PendingIntent.FLAG_UPDATE_CURRENT
    );