5

App crashes at runtime with the following error :

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 tried all solutions available but the app still crashing on Android 12.

private Notification buildNotification() {
    int playButtonResId = isPaused ? R.drawable.ic_play_arrow_white_24dp : R.drawable.ic_pause_white_24dp;

    PendingIntent clickIntent = PendingIntent.getActivity(
            this, 1,
            new Intent(this, MainActivity.class),
            PendingIntent.FLAG_IMMUTABLE);

1 Answers1

-2
PendingIntent clickIntent;
if (android.os.Build.VERSION.SDK_INT >= android.os.Build.VERSION_CODES.M) {
    clickIntent = PendingIntent.getActivity(
        getApplicationContext(),
        1,
        intent, 
        PendingIntent.FLAG_IMMUTABLE | PendingIntent.FLAG_UPDATE_CURRENT
    );
} else {
    clickIntent = PendingIntent.getActivity(getApplicationContext(), 1, intent, 0);
}
Saeed Zhiany
  • 2,051
  • 9
  • 30
  • 41
  • Please do not post just code, some explanation would be nice. – Evgeniy Mishustin Aug 17 '22 at 06:32
  • Please read [How do I write a good answer?](https://stackoverflow.com/help/how-to-answer). While this code block may answer the OP's question, this answer would be much more useful if you explain how this code is different from the code in the question, what you've changed, why you've changed it and why that solves the problem without introducing others. – Saeed Zhiany Aug 17 '22 at 11:12