Skip to content
This repository has been archived by the owner on Oct 6, 2022. It is now read-only.

Context.startForegroundService() did not then call Service.startForeground() #266

Closed
2 of 4 tasks
gazedash opened this issue Jun 6, 2019 · 29 comments · Fixed by #344
Closed
2 of 4 tasks

Context.startForegroundService() did not then call Service.startForeground() #266

gazedash opened this issue Jun 6, 2019 · 29 comments · Fixed by #344

Comments

@gazedash
Copy link

gazedash commented Jun 6, 2019

Description

Context.startForegroundService() did not then call Service.startForeground()
#159

Often happens on Samsung, 9.0 android version
I have
<uses-permission android:name="android.permission.FOREGROUND_SERVICE" /> in my manifest.

I use the latest version of react-native-music-control ("0.10.4")

  1. Sample code (provide repo url or sample code)
  1. Platform ?

    • iOS
    • Android
  1. Device
  • Simulator
  • Real device
@dengue8830
Copy link

i'm facing the same, @gazedash do you solve it?

@hiennguyen92
Copy link
Contributor

Got the same issue, I tried many workaround but can't solved completely

@marf
Copy link
Contributor

marf commented Aug 25, 2019

Same problem here!

@fjmorant
Copy link

same 👍

@garfiaslopez
Copy link

+1

@benoitvogel
Copy link

Same problem here. Android 10 in an emulator.

Crashes consistently when calling setNowPlaying() after either resetNowPlaying() or stopControl().

@bunmiedee
Copy link
Contributor

bunmiedee commented Nov 23, 2019

Turns out that the conditional block in MusicControlNotification onCreate method isn't necessary. You can also replace the conditional block in MusicControlModule with

ContextCompat.startForegroundService(context, myIntent);

In MusicControlNotification Replace with:

public void onCreate() {
            super.onCreate();
            notification = MusicControlModule.INSTANCE.notification.prepareNotification(MusicControlModule.INSTANCE.nb, false);
            startForeground(NOTIFICATION_ID, notification);
            isRunning = true;
        }

@tanguyantoine
Copy link
Owner

reproducible with 0.10.8?

@zatloeri
Copy link

zatloeri commented Jan 23, 2020

reproducible with 0.10.8?

@tanguyantoine Yes, still getting this error on some hw devices in 0.10.8.
It started happening after I started calling resetNowPlaying() or stopControl() shortly after app start.

@pabloluz
Copy link

Same issue here. I'm on 0.11.0.

@pabloluz
Copy link

(Just realized many currently open issues might be related to this one, like #244, for example)

@tanguyantoine, I appreciate in advance your attention on that ;)

@tanguyantoine
Copy link
Owner

@bunmiedee would you mind creating a pull request ?

@bunmiedee
Copy link
Contributor

@tanguyantoine sure - I can look into it over the weekend

@sparcbr
Copy link

sparcbr commented Feb 26, 2020

While we don't fix this I'm using this workaround to prevent multiple calls to startForegroundService. Seems to stop the bug from happening.

I just commented out all the code that unregisters and releases eveything, preventing init() from running more than once.

--- a/android/src/main/java/com/tanguyantoine/react/MusicControlModule.java
+++ b/android/src/main/java/com/tanguyantoine/react/MusicControlModule.java
@@ -35,6 +35,7 @@ import java.net.URLConnection;
 import java.util.HashMap;
 import java.util.Locale;
 import java.util.Map;
+import androidx.core.content.ContextCompat;
 
 public class MusicControlModule extends ReactContextBaseJavaModule implements ComponentCallbacks2 {
     private static final String TAG = MusicControlModule.class.getSimpleName();
@@ -198,12 +199,7 @@ public class MusicControlModule extends ReactContextBaseJavaModule implements Co
 
         afListener = new MusicControlAudioFocusListener(context, emitter, volume);
 
-        if(Build.VERSION.SDK_INT >= Build.VERSION_CODES.O){
-            context.startForegroundService(myIntent);
-
-        }
-        else
-            context.startService(myIntent);
+        ContextCompat.startForegroundService(context, myIntent);
 
         context.registerComponentCallbacks(this);
 
@@ -218,6 +214,10 @@ public class MusicControlModule extends ReactContextBaseJavaModule implements Co
 
         if (notification != null)
             notification.hide();
+
+        session.setActive(false);
+
+                /*
         session.release();
 
         ReactApplicationContext context = getReactApplicationContext();
@@ -239,6 +239,7 @@ public class MusicControlModule extends ReactContextBaseJavaModule implements Co
         nb = null;
 
         init = false;
+                */
     }
 
     synchronized public void destroy() {

@bradfloodx
Copy link
Collaborator

This was a problem for me too. Reopening to be fixed.

PRs welcome! @sparcbr @bunmiedee @pabloluz @gazedash

@bradfloodx
Copy link
Collaborator

Should be fixed in #329

@rborn
Copy link
Contributor

rborn commented Jun 22, 2020

@bradleyflood @bunmiedee I still get this (a lot) with the latest version 😔

@rborn
Copy link
Contributor

rborn commented Jun 22, 2020

@bradleyflood @bunmiedee I'll try this fix and make a PR if it works https://stackoverflow.com/a/53402038, it will take a while because I cannot reproduce on any of my devices so I need to push to prod 🙄

@bradfloodx
Copy link
Collaborator

Thanks @rborn I'll reopen this issue.

@bradfloodx bradfloodx reopened this Jun 23, 2020
@rborn
Copy link
Contributor

rborn commented Jul 21, 2020

Just a heads-up, the above "fix" didn't work, we still have a lot of crashes on android 9/10 :(

@hiroyaiizuka
Copy link

any updates?
still get this error ...😣

@rborn
Copy link
Contributor

rborn commented Aug 21, 2020

@SBShane cold call I know 😅 but I see you are versed with android ? any chance you could look into this ? 😻

@rborn
Copy link
Contributor

rborn commented Aug 21, 2020

@SBShane I found this fork, will let you know if I can merge to mine and if it works - https://github.com/natashache/react-native-music-control

@rborn
Copy link
Contributor

rborn commented Aug 26, 2020

Small update: we launched to prod with the changes done using the above fork and it seems to be fixed. I'll like to a few more days to be sure.

@natashache I have my fork where I merged your code 😻, synced with current master and added a small fix. Would you like to make a PR or can I do it? 😻 https://github.com/rborn/react-native-music-control

rborn referenced this issue in natashache/react-native-music-control Aug 28, 2020
@rborn
Copy link
Contributor

rborn commented Sep 7, 2020

@bradleyflood I have been testing the fix in production and it seems to work fine, however I got no reply from @natashache nor @hiennguyen92 (natashache@c28a931#commitcomment-41838029) so I don't know how to proceed :(

@hiennguyen92
Copy link
Contributor

@rborn You are free to use it at your disposal.

@rborn
Copy link
Contributor

rborn commented Sep 7, 2020

@hiennguyen92 thank you, I'll create a PR soon 😻

@rborn rborn mentioned this issue Sep 8, 2020
@rborn
Copy link
Contributor

rborn commented Sep 8, 2020

@bradleyflood PR done, can we maybe have a release 😅 ? This code is tested in production, we got no errors related to the startForeground anymore (there are some commits after that, which fix small issues but the crashes were only a few)

@hiennguyen92, @natashache thank you for your fixes 😻

@bradfloodx
Copy link
Collaborator

bradfloodx commented Sep 8, 2020 via email

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.