Skip to content
This repository has been archived by the owner on Jan 14, 2025. It is now read-only.

Android 12: Notifications will no work/crash with SDK 31 #2129

Open
cristianoccazinsp opened this issue Aug 30, 2021 · 10 comments · Fixed by #2130
Open

Android 12: Notifications will no work/crash with SDK 31 #2129

cristianoccazinsp opened this issue Aug 30, 2021 · 10 comments · Fixed by #2130

Comments

@cristianoccazinsp
Copy link
Contributor

Bug

Environment info

react-native info output:

 System:
    OS: macOS 11.3
    CPU: (4) x64 Intel(R) Core(TM) i5-6267U CPU @ 2.90GHz
    Memory: 48.70 MB / 8.00 GB
    Shell: 5.8 - /bin/zsh
  Binaries:
    Node: 12.22.1 - /usr/local/bin/node
    Yarn: 1.22.5 - /usr/local/bin/yarn
    npm: 6.14.12 - /usr/local/bin/npm
    Watchman: 2021.06.07.00 - /usr/local/bin/watchman
  Managers:
    CocoaPods: 1.10.1 - /usr/local/bin/pod
  SDKs:
    iOS SDK:
      Platforms: iOS 14.5, DriverKit 20.4, macOS 11.3, tvOS 14.5, watchOS 7.4
    Android SDK:
      API Levels: 28, 29, 30, 31
      Build Tools: 28.0.3, 29.0.2, 29.0.3, 31.0.0
      Android NDK: Not Found
  IDEs:
    Android Studio: 4.1 AI-201.8743.12.41.7199119
    Xcode: 12.5.1/12E507 - /usr/bin/xcodebuild
  Languages:
    Java: 1.8.0_121 - /usr/bin/javac
    Python: 2.7.15 - /Library/Frameworks/Python.framework/Versions/2.7/bin/python
  npmPackages:
    @react-native-community/cli: Not Found
    react: 16.14.0 => 16.14.0 
    react-native: 0.62.2 => 0.62.2 
    react-native-macos: Not Found
  npmGlobalPackages:
    *react-native*: Not Found

Library version: 8.0.1

Steps To Reproduce

Running: PushNotification.localNotification({...}) will crash in Android 12.

Due to Android 12 changes, trying to display a local or remote notification will fail with the given error on Android 12 (when compiling with SDK 31):

2021-08-30 14:23:16.910 29950-30022/com.zinspector3.dev E/RNPushNotification: failed to send push notification
    java.lang.IllegalArgumentException: com.zinspector3.dev: 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.

Reference: https://developer.android.com/about/versions/12/behavior-changes-12#pending-intent-mutability

The fix should be as follows: https://github.com/zo0r/react-native-push-notification/blob/master/android/src/main/java/com/dieam/reactnativepushnotification/modules/RNPushNotificationHelper.java#L453

- PendingIntent pendingIntent = PendingIntent.getActivity(context, notificationID, intent, PendingIntent.FLAG_UPDATE_CURRENT);
+ PendingIntent pendingIntent = PendingIntent.getActivity(context, notificationID, intent, PendingIntent.FLAG_UPDATE_CURRENT | PendingIntent.FLAG_IMMUTABLE);

There are also other parts of the code that may use PendingIntent which I haven't tested as I don't use it, but all of these should be updated (https://github.com/zo0r/react-native-push-notification/blob/master/android/src/main/java/com/dieam/reactnativepushnotification/modules/RNPushNotificationHelper.java#L531). Also, PendingIntent.FLAG_IMMUTABLE was added in SDK 23, so a build check also needs to be performed, something like: Build.VERSION.SDK_INT >= Build.VERSION_CODES.M ? PendingIntent.FLAG_UPDATE_CURRENT | PendingIntent.FLAG_IMMUTABLE : PendingIntent.FLAG_UPDATE_CURRENT

@Dallas62

Describe what you expected to happen:

  1. No crash

Reproducible sample code

Please review the change and

@cristianoccazinsp cristianoccazinsp changed the title Android 12: PendingIntent for notifications will not work Android 12: Notifications will no work with SDK 31 Aug 30, 2021
@cristianoccazinsp cristianoccazinsp changed the title Android 12: Notifications will no work with SDK 31 Android 12: Notifications will no work/crash with SDK 31 Aug 30, 2021
@Dallas62
Copy link
Collaborator

Dallas62 commented Sep 3, 2021

Hi @cristianoccazinsp
Should be fixed in 8.1.0.
Thanks for the catch !
Regards

@codal-mpawar
Copy link

crash the application when get notification sdk is 30.

@cristianoccazinsp
Copy link
Contributor Author

Do you think the above PR introduced a crash with SDK 30?

@Dallas62
Copy link
Collaborator

Dallas62 commented Sep 6, 2021

Hi @codal-mpawar
A full stacktrace and some details about the dependencies might be appreciated.
Regards

@redexp
Copy link

redexp commented Sep 27, 2021

This fix creates this issue #2152

I think Build.VERSION.SDK_INT >= Build.VERSION_CODES.M (M = Android 6) should be changed to Build.VERSION.SDK_INT > Build.VERSION_CODES.R (R = Android 11)

@cristianoccazinsp
Copy link
Contributor Author

Wouldn't that fix the issue only for Android < 12? Meaning that Android 12 will still have the issue reported in #2152

The problem is probably related to the actual flags being used, which flag would solve the problem for all android versions?

@github-actions
Copy link

This issue has been automatically marked as stale because it has not had recent activity. It will be closed in 30 days if no further activity occurs. Thank you for your contributions.

@terryjiang2020
Copy link

Just wondering, is this issue fixed?

@BogdanHossu
Copy link

I have the issue mentioned in here and the solution here helped me https://stackoverflow.com/a/74549190/6084814. Hope it will help somebody else too.

@MorganTrudeau
Copy link

This fix creates this issue #2152

I think Build.VERSION.SDK_INT >= Build.VERSION_CODES.M (M = Android 6) should be changed to Build.VERSION.SDK_INT > Build.VERSION_CODES.R (R = Android 11)

Yes I am using android.os.Build.VERSION.SDK_INT >= android.os.Build.VERSION_CODES.S in my patch. This stopped working in sdk 31.

@github-actions github-actions bot removed the Stale label Aug 15, 2024
Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.
Labels
None yet
Projects
None yet
Development

Successfully merging a pull request may close this issue.

7 participants