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
695 stars 265 forks source link

(Android 9 / Samsung S9) Unable to create service com.tanguyantoine.react.MusicControlNotification$NotificationService: #244

Closed jamsch closed 4 years ago

jamsch commented 5 years ago

Description

App crashes with the following devices in a production app:

All devices listed above are running Android 9

  1. Platform ?

    • [ ] iOS
    • [x] Android
  2. Device

    • [ ] Simulator
    • [x] Real device

Stack trace

Selection_029

java.lang.NullPointerException: Attempt to invoke virtual method 'android.app.Notification com.tanguyantoine.react.MusicControlNotification.a(android.support.v4.app.y$c, boolean)' on a null object reference
    at com.tanguyantoine.react.MusicControlNotification$NotificationService.onCreate
    at android.app.ActivityThread.handleCreateService(ActivityThread.java:3728)
    at android.app.ActivityThread.access$1400(ActivityThread.java:235)
    at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1784)
    at android.os.Handler.dispatchMessage(Handler.java:106)
    at android.os.Looper.loop(Looper.java:214)
    at android.app.ActivityThread.main(ActivityThread.java:6981)
    at java.lang.reflect.Method.invoke(Method.java)
    at com.android.internal.os.RuntimeInit$MethodAndArgsCaller.run(RuntimeInit.java:493)
    at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:1445)
java.lang.RuntimeException: Unable to create service com.tanguyantoine.react.MusicControlNotification$NotificationService: java.lang.NullPointerException: Attempt to invoke virtual method 'android.app.Notification com.tanguyantoine.react.MusicControlNotification.a(android.support.v4.app.y$c, boolean)' on a null object reference
    at android.app.ActivityThread.handleCreateService(ActivityThread.java:3740)
    at android.app.ActivityThread.access$1400(ActivityThread.java:235)
    at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1784)
    at android.os.Handler.dispatchMessage(Handler.java:106)
    at android.os.Looper.loop(Looper.java:214)
    at android.app.ActivityThread.main(ActivityThread.java:6981)
    at java.lang.reflect.Method.invoke(Method.java)
    at com.android.internal.os.RuntimeInit$MethodAndArgsCaller.run(RuntimeInit.java:493)
    at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:1445)
appaaaa commented 5 years ago

You should register foreground service in manifest permission on api 28 Just add below code to android/app/src/AndroidManifest.xml

uses-permission android:name="android.permission.FOREGROUND_SERVICE"

tanguyantoine commented 5 years ago

@appaaaa any chance you update the readme? :-)

The0racle commented 5 years ago

I'm still seeing this crash on our production builds. Am I missing something?

marf commented 5 years ago

Same problem here

tanguyantoine commented 5 years ago

did you try this <uses-permission android:name="android.permission.FOREGROUND_SERVICE" /> ?

The0racle commented 5 years ago

@tanguyantoine yes.

jamsch commented 5 years ago

image

image

Issue still persists using the latest version. Affects Samsung Galaxy devices. I've already added the FOREGROUND_SERVICE Android permission a while back.

marf commented 5 years ago

Are there any solutions for this problem?

sampsonjoliver commented 5 years ago

We're also seeing this issue in our app.

The0racle commented 5 years ago

Unfortunately this is the number of crash in our app. It literally increases crashes by 2%. If someone has a fix for this, it's much appreciated.

The0racle commented 5 years ago

Maybe related to #276 ?

tanguyantoine commented 5 years ago

does one of you have any Android knowledges?

The0racle commented 5 years ago

Unfortunately I don't.

garfiaslopez commented 5 years ago

@jamsch did you fix this?

sampsonjoliver commented 5 years ago

Just for context, I'm seeing this issue in production across more than just Samsung devices. From a very small release % of our app's user base, this is what I've already seen:

image

sampsonjoliver commented 5 years ago

After a little bit of digging, what I've seen so far is:

The MusicControlModule.init method sets the notification instance, and then starts the MusicControlNotification.NotificationService.

Then MusicControlNotification.NotificationService.onCreate references the MusicControlModule.INSTANCE.notification and calls methods on it, however notification is null. This wouldn't be executing if the foreground service was failing to be created due to permissions or similar, so it's clear that isn't the problem.

So to my understanding, this can only happen if MusicControlModule.stopControl has been called, which will set notification = null after calling notification.hide() which stops the service.

My best guess is that MusicControl.init is being called more than once and a second instance of NotificationService is attempting to run its onCreate after MusicControlModule.stopControl has been called, either by the application, in the react-native side, or by MusicControlModule.destroy. Either way something is running out of order.

🤷‍♂ hope this helps give some direction to someone. I'm not sure I'll have the time to try diagnosing further to see what it is that's causing the service to run its onCreate after the notification has been stopped.

pabloluz commented 5 years ago

Thanks for the insight, @sampsonjoliver.

From my end, I can also confirm the issue is happening in production on devices running Android 9 and 10 (mainly in, but not limited to, Samsung Galaxy and Moto G families).

tanguyantoine commented 4 years ago

reproducible with 0.10.8?

mordonez-me commented 4 years ago

This is happening to me also:

Process: com.mb.bl.app, PID: 4314 java.lang.RuntimeException: Unable to create service com.tanguyantoine.react.MusicControlNotification$NotificationService: java.lang.NullPointerException: Attempt to invoke virtual method 'android.app.Notification com.tanguyantoine.react.MusicControlNotification.prepareNotification(androidx.core.app.NotificationCompat$Builder, boolean)' on a null object reference at android.app.ActivityThread.handleCreateService(ActivityThread.java:3414) at android.app.ActivityThread.-wrap4(Unknown Source:0) at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1683) at android.os.Handler.dispatchMessage(Handler.java:105) at android.os.Looper.loop(Looper.java:164) at android.app.ActivityThread.main(ActivityThread.java:6541) at java.lang.reflect.Method.invoke(Native Method) at com.android.internal.os.Zygote$MethodAndArgsCaller.run(Zygote.java:240) at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:767) Caused by: java.lang.NullPointerException: Attempt to invoke virtual method 'android.app.Notification com.tanguyantoine.react.MusicControlNotification.prepareNotification(androidx.core.app.NotificationCompat$Builder, boolean)' on a null object reference at com.tanguyantoine.react.MusicControlNotification$NotificationService.onCreate(MusicControlNotification.java:198) at android.app.ActivityThread.handleCreateService(ActivityThread.java:3404)

kalpeshp0310 commented 4 years ago

Some more info. Those crashes occur after Audio focus changes or App runs out of memory. Crash logs from our instaBug account.

Console
20:58:27
E/AndroidRuntime(21112): at com.tanguyantoine.react.MusicControlNotification$NotificationService.onCreate(MusicControlNotification.java:198)
Console
20:58:27
E/AndroidRuntime(21112): Caused by: java.lang.NullPointerException: Attempt to invoke virtual method 'android.app.Notification com.tanguyantoine.react.MusicControlNotification.prepareNotification(androidx.core.app.NotificationCompat$Builder, boolean)' on a null object reference
....
E/AndroidRuntime(21112): at android.app.ActivityThread.handleCreateService(ActivityThread.java:3817)
Console
20:58:27
E/AndroidRuntime(21112): java.lang.RuntimeException: Unable to create service com.tanguyantoine.react.MusicControlNotification$NotificationService: java.lang.NullPointerException: Attempt to invoke virtual method 'android.app.Notification com.tanguyantoine.react.MusicControlNotification.prepareNotification(androidx.core.app.NotificationCompat$Builder, boolean)' on a null object reference
Console
20:58:27
E/AndroidRuntime(21112): Process: ai.abridge.nativeclient.release, PID: 21112
Console
20:58:27
E/AndroidRuntime(21112): FATAL EXCEPTION: main
Console
20:58:27
D/AndroidRuntime(21112): Shutting down VM
Console
20:58:27
D/AudioManager(21112): dispatching onAudioFocusChange(-1) to android.media.AudioManager@fd19e2acom.tanguyantoine.react.MusicControlAudioFocusListener@8ed541b
Console
03:10:00
E/AndroidRuntime(31256): at com.tanguyantoine.react.MusicControlNotification$NotificationService.onCreate(MusicControlNotification.java:198)
Console
03:10:00
E/AndroidRuntime(31256): Caused by: java.lang.NullPointerException: Attempt to invoke virtual method 'android.app.Notification com.tanguyantoine.react.MusicControlNotification.prepareNotification(androidx.core.app.NotificationCompat$Builder, boolean)' on a null object reference
...
Console
03:10:00
E/AndroidRuntime(31256): at android.app.ActivityThread.handleCreateService(ActivityThread.java:3965)
Console
03:10:00
E/AndroidRuntime(31256): java.lang.RuntimeException: Unable to create service com.tanguyantoine.react.MusicControlNotification$NotificationService: java.lang.NullPointerException: Attempt to invoke virtual method 'android.app.Notification com.tanguyantoine.react.MusicControlNotification.prepareNotification(androidx.core.app.NotificationCompat$Builder, boolean)' on a null object reference
Console
03:10:00
E/AndroidRuntime(31256): Process: ai.abridge.nativeclient.release, PID: 31256
Console
03:10:00
E/AndroidRuntime(31256): FATAL EXCEPTION: main
Console
03:10:00
D/AndroidRuntime(31256): Shutting down VM
Console
03:10:00
W/MusicControlModule(31256): Control resources are being removed due to system's low memory (Level: 15)
ali-pedram commented 4 years ago

is anybody merging this fix ?

FabricioCabeca commented 4 years ago

Updated yesterday to version 0.11.0, no crashes reported so far

tanguyantoine commented 4 years ago

Should we close it?

FabricioCabeca commented 4 years ago

Should we close it?

It is happening much less often, but there are still some crashes in the new version, now it crashes in the onStartCommand call instead of onCreate, below is the stack trace:

java.lang.RuntimeException:

at android.app.ActivityThread.handleServiceArgs (ActivityThread.java:3903)

at android.app.ActivityThread.access$1700 (ActivityThread.java:213)

at android.app.ActivityThread$H.handleMessage (ActivityThread.java:1847)

at android.os.Handler.dispatchMessage (Handler.java:106)

at android.os.Looper.loop (Looper.java:193)

at android.app.ActivityThread.main (ActivityThread.java:6923)

at java.lang.reflect.Method.invoke (Native Method)

at com.android.internal.os.RuntimeInit$MethodAndArgsCaller.run (RuntimeInit.java:493)

at com.android.internal.os.ZygoteInit.main (ZygoteInit.java:870) Caused by: java.lang.NullPointerException:

at com.tanguyantoine.react.MusicControlNotification$NotificationService.onStartCommand (MusicControlNotification.java:194)

at android.app.ActivityThread.handleServiceArgs (ActivityThread.java:3884)

pabloluz commented 4 years ago

@tanguyantoine I'm experiencing the same as @FabricioCabeca

laurens-lamberts commented 4 years ago

We are also experiencing this issue at production on a daily basis. Devices where the crashes appear most often;

We use version 0.10.8 of react-native-music-control, and we have about 16 users affected by crashes every month. We do have the FOREGROUND_SERVICE permission configured.

Do you think upgrading to 0.11.0 would reduce the amount of crashes experienced? Thanks for any help!

pabloluz commented 4 years ago

@weareyou-laurens 0.11.0 doesn't fix the issue entirely. See @FabricioCabeca 's comment above.

noway commented 3 years ago

This should be reopened. There's no fix for the crash @FabricioCabeca is experiencing yet.

noway commented 3 years ago

@FabricioCabeca have you had a chance to try out the #381 fix? I wonder if that fixes your issue since it touches onStartCommand too.

leogiroux commented 3 years ago

I can confirm that I have a lot of crashes from this lib Attempt to invoke virtual method 'android.app.Notification com.tanguyantoine.react.MusicControlNotification.prepareNotification(androidx.core.app.NotificationCompat$Builder, boolean)' on a null object reference

Do you guys have an alternative or a solution for this ? I will try #381 too.

nahuelb commented 3 years ago

I tried #381 and I am still having this issue.

krunalbad commented 3 years ago

@nahuelb Do you have any other media pkg installed in ur application? Cuz Different channels might create conflicts in Android devices.