tanguyantoine / react-native-music-control

Display and manage media controls on lock screen and notification center for iOS and Android.
https://www.npmjs.com/package/react-native-music-control
MIT License
697 stars 264 forks source link

Fix Android Apps crashing when targeting v12 / SDK31 #397

Closed rafaelmaeuer closed 2 years ago

rafaelmaeuer commented 2 years ago

What's this PR does?

Conforming lib to Behavior changes: Apps targeting Android 12 - Pending intents mutability. Fixes Android Apps crashing when compileSdkVersion and/or targetSdkVersion is set to 31.

Which issue(s) is it related to?

/

Screenshots (if appropriate)

/

How to test:

  1. Setup Example Project without this PR
  2. Call one of following methods and the App will crash
    • MusicControl.on(Command.play, ()=> {})
    • MusicControl.on(Command.pause, ()=> {})
E/unknown:ReactNative: Exception in native call
    java.lang.IllegalArgumentException: com.app.name: 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:375)
        at android.app.PendingIntent.getBroadcastAsUser(PendingIntent.java:645)
        at android.app.PendingIntent.getBroadcast(PendingIntent.java:632)
        at com.tanguyantoine.react.MusicControlNotification.createAction(MusicControlNotification.java:179)
        at com.tanguyantoine.react.MusicControlNotification.updateActions(MusicControlNotification.java:61)
        at com.tanguyantoine.react.MusicControlModule.enableControl(MusicControlModule.java:565)
        at java.lang.reflect.Method.invoke(Native Method)
        at com.facebook.react.bridge.JavaMethodWrapper.invoke(JavaMethodWrapper.java:372)
        at com.facebook.react.bridge.JavaModuleWrapper.invoke(JavaModuleWrapper.java:188)
        at com.facebook.react.bridge.queue.NativeRunnable.run(Native Method)
        at android.os.Handler.handleCallback(Handler.java:938)
        at android.os.Handler.dispatchMessage(Handler.java:99)
        at com.facebook.react.bridge.queue.MessageQueueThreadHandler.dispatchMessage(MessageQueueThreadHandler.java:27)
        at android.os.Looper.loopOnce(Looper.java:201)
        at android.os.Looper.loop(Looper.java:288)
        at com.facebook.react.bridge.queue.MessageQueueThreadImpl$4.run(MessageQueueThreadImpl.java:226)
        at java.lang.Thread.run(Thread.java:920)
  1. Setup Example Project with this PR
  2. Call one of following methods
    • MusicControl.on(Command.play, ()=> {})
    • MusicControl.on(Command.pause, ()=> {})
  3. The App won't crash nor show an error message
redreceipt commented 2 years ago

@tanguyantoine what's the timeline on a release look like with this in it?

redreceipt commented 2 years ago

or @bradleyflood maybe? ^

rajpootathar commented 2 years ago

Still facing same issue when calling MusicControl.setNowPlaying(options)

Tried with PendingIntent.FLAG_MUTABLE flag still not resolved

MusicControlNotification.java

PendingIntent.getBroadcast(context, 0, remove, PendingIntent.FLAG_IMMUTABLE | PendingIntent.FLAG_MUTABLE | PendingIntent.FLAG_UPDATE_CURRENT)

Android 12 (Api 32)