Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Android notifcation player doesn't go away #49

Closed
samyachour opened this issue Dec 8, 2016 · 8 comments · Fixed by #74
Closed

Android notifcation player doesn't go away #49

samyachour opened this issue Dec 8, 2016 · 8 comments · Fixed by #74
Labels

Comments

@samyachour
Copy link

When I close my app with app switcher (in android) the notifcation player doesn't go away, and the little 'x' button doesn't work.

I found a workaround though, just for everyone's information. In my android project I made a new service and used the method onTaskRemoved which gets called when the app is closed from the app switcher.

In that method I cancelled all the notifcations for the app, and voila! Will give more info if requested.

@tlenclos tlenclos added the bug label Dec 13, 2016
@tlenclos
Copy link
Owner

Thanks for the tip, feel free to submit a PR if you want ;)

@IndrekV
Copy link
Contributor

IndrekV commented Feb 24, 2017

@samyachour I would be very interested in a more detailed explanation of the fix. I'm faced with the same issue and my Android knowledge is lacking to find the fix myself.

@samyachour
Copy link
Author

samyachour commented Feb 24, 2017

@IndrekV no problem. I don't have much android knowledge either, so forgive my lack of depth in the explanation. But essentially we are creating a service (a background task) that will delete all notifications from the app when it's closed via the app switcher.

  1. Create a file under app/java/com.project-name called MyService.java, copy and paste the following code into it
package com.project-name;

import android.app.Service;
import android.content.Intent;
import android.os.IBinder;
import android.app.NotificationManager;
import android.content.Context;

public class MyService extends Service {

    private static final String TAG = MyService.class.getSimpleName();

    @Override
    public IBinder onBind(Intent intent) {
        return null;
    }

    @Override
    public void onTaskRemoved(Intent rootIntent) {
        super.onTaskRemoved(rootIntent);
        NotificationManager nMgr = (NotificationManager) getSystemService(Context.NOTIFICATION_SERVICE);
        nMgr.cancelAll();
    }

}

Now we have a service that will remove all notifications once the app is removed from the app switcher. You probably should rename the service for code clarity. The next step is simply iniating the service when the app starts.

  1. Open your MainActivity.java in the same folder. Add the following method to the public MainActivity Class.
@Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        Intent serviceIntent = new Intent(this, MyService.class);
        startService(serviceIntent);
    }

This calls our service when the app starts, as well as the rest of the stuff we need to call. Then just add these import lines to MainActivity.java and you should be good.

import android.os.Bundle;
import android.util.Log;
import android.content.Intent;

Let me know if you have any more questions!

EDIT: Added step

  1. According to @nusson you need to add the following line to your manifest (android/app/src/main/AndroidManifest.xml) inside the <application> tag
<service android:name="com.project-name.MyService" android:stopWithTask="false" />

@IndrekV
Copy link
Contributor

IndrekV commented Feb 25, 2017

@samyachour Thank you for the fast response. I tried the your suggested solution and sadly it did not work for me. I looked a bit more into the code and found that the issue was even simpler. I created PR to fix this issue #74. I hope @tlenclos merges this fast as it is pretty annoying bug.

@tlenclos
Copy link
Owner

@jerodb
Copy link

jerodb commented Apr 27, 2017

@tlenclos this is not fixed. #74 doesn't fix the problem for me. But the solution @samyachour propose works perfect.

@nusson
Copy link

nusson commented Aug 24, 2017

@samyachour
please add a final step to your fix :

3- Finally add the service in the app's AndroidManifest.xml (android/app/src/main) like this:
<service android:name="com.project-name.MyService" android:stopWithTask="false" />
(inside <application> )

@samyachour
Copy link
Author

@nusson added!

Haven't looked at this in a while, but your suggestion makes sense.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
Projects
None yet
Development

Successfully merging a pull request may close this issue.

5 participants