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

Commit

Permalink
Fix #236 close notification not working on android 8.0+
Browse files Browse the repository at this point in the history
  • Loading branch information
matinzd authored and tanguyantoine committed May 23, 2019
1 parent 8a78e22 commit 91ab64b
Show file tree
Hide file tree
Showing 2 changed files with 69 additions and 37 deletions.
Original file line number Diff line number Diff line change
@@ -1,5 +1,9 @@
package com.tanguyantoine.react;

import android.content.Intent;
import android.os.Build;
import android.support.v4.content.ContextCompat;

import com.facebook.react.bridge.Arguments;
import com.facebook.react.bridge.ReactApplicationContext;
import com.facebook.react.bridge.ReactContext;
Expand All @@ -11,13 +15,13 @@ private static void sendEvent(ReactApplicationContext context, String type, Obje
WritableMap data = Arguments.createMap();
data.putString("name", type);

if(value != null) {
if(value instanceof Double || value instanceof Float) {
data.putDouble("value", (double)value);
} else if(value instanceof Boolean) {
data.putBoolean("value", (boolean)value);
} else if(value instanceof Integer) {
data.putInt("value", (int)value);
if (value != null) {
if (value instanceof Double || value instanceof Float) {
data.putDouble("value", (double) value);
} else if (value instanceof Boolean) {
data.putBoolean("value", (boolean) value);
} else if (value instanceof Integer) {
data.putInt("value", (int) value);
}
}

Expand All @@ -35,6 +39,7 @@ public void onPlay() {
}

public void onPause() {
stopForegroundService();
sendEvent(context, "pause", null);
}

Expand Down Expand Up @@ -63,11 +68,22 @@ public void onRewind() {
}

public void onSetRating(float rating) {
sendEvent(context,"setRating", rating);
sendEvent(context, "setRating", rating);
}

public void onSetRating(boolean hasHeartOrThumb) {
sendEvent(context,"setRating", hasHeartOrThumb);
sendEvent(context, "setRating", hasHeartOrThumb);
}

public void onVolumeChange(int volume) {
sendEvent(context, "volume", volume);
}

public void onVolumeChange(int volume) { sendEvent(context, "volume", volume); }
private void stopForegroundService() {
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.O) {
Intent intent = new Intent(context, MusicControlNotification.NotificationService.class);
intent.setAction("StopService");
ContextCompat.startForegroundService(context, intent);
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -45,11 +45,11 @@ public MusicControlNotification(MusicControlModule module, ReactApplicationConte

// Optional custom icon with fallback to the play icon
smallIcon = r.getIdentifier("music_control_icon", "drawable", packageName);
if(smallIcon == 0) smallIcon = r.getIdentifier("play", "drawable", packageName);
if (smallIcon == 0) smallIcon = r.getIdentifier("play", "drawable", packageName);
}

public synchronized void setCustomNotificationIcon(String resourceName) {
if(resourceName == null) {
if (resourceName == null) {
customIcon = 0;
return;
}
Expand Down Expand Up @@ -82,19 +82,21 @@ public synchronized void updateActions(long mask, Map<String, Integer> options)

public Notification prepareNotification(NotificationCompat.Builder builder, boolean isPlaying) {
// Add the buttons


builder.mActions.clear();
if(previous != null) builder.addAction(previous);
if(skipBackward != null) builder.addAction(skipBackward);
if(play != null && !isPlaying) builder.addAction(play);
if(pause != null && isPlaying) builder.addAction(pause);
if(stop != null) builder.addAction(stop);
if(next != null) builder.addAction(next);
if(skipForward != null) builder.addAction(skipForward);
if (previous != null) builder.addAction(previous);
if (skipBackward != null) builder.addAction(skipBackward);
if (play != null && !isPlaying) builder.addAction(play);
if (pause != null && isPlaying) builder.addAction(pause);
if (stop != null) builder.addAction(stop);
if (next != null) builder.addAction(next);
if (skipForward != null) builder.addAction(skipForward);

// Set whether notification can be closed based on closeNotification control (default PAUSED)
if(module.notificationClose == MusicControlModule.NotificationClose.ALWAYS) {
if (module.notificationClose == MusicControlModule.NotificationClose.ALWAYS) {
builder.setOngoing(false);
} else if(module.notificationClose == MusicControlModule.NotificationClose.PAUSED) {
} else if (module.notificationClose == MusicControlModule.NotificationClose.PAUSED) {
builder.setOngoing(isPlaying);
} else { // NotificationClose.NEVER
builder.setOngoing(true);
Expand Down Expand Up @@ -122,7 +124,7 @@ public synchronized void show(NotificationCompat.Builder builder, boolean isPlay
public void hide() {
NotificationManagerCompat.from(context).cancel(NOTIFICATION_ID);

if(Build.VERSION.SDK_INT >= Build.VERSION_CODES.O){
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.O) {

Intent myIntent = new Intent(context, MusicControlNotification.NotificationService.class);
context.stopService(myIntent);
Expand All @@ -135,29 +137,30 @@ public void hide() {
* Replace this to PlaybackStateCompat.toKeyCode when React Native updates the support library
*/
private int toKeyCode(long action) {
if(action == PlaybackStateCompat.ACTION_PLAY) {
if (action == PlaybackStateCompat.ACTION_PLAY) {
return KeyEvent.KEYCODE_MEDIA_PLAY;
} else if(action == PlaybackStateCompat.ACTION_PAUSE) {
} else if (action == PlaybackStateCompat.ACTION_PAUSE) {
return KeyEvent.KEYCODE_MEDIA_PAUSE;
} else if(action == PlaybackStateCompat.ACTION_SKIP_TO_NEXT) {
} else if (action == PlaybackStateCompat.ACTION_SKIP_TO_NEXT) {
return KeyEvent.KEYCODE_MEDIA_NEXT;
} else if(action == PlaybackStateCompat.ACTION_SKIP_TO_PREVIOUS) {
} else if (action == PlaybackStateCompat.ACTION_SKIP_TO_PREVIOUS) {
return KeyEvent.KEYCODE_MEDIA_PREVIOUS;
} else if(action == PlaybackStateCompat.ACTION_STOP) {
} else if (action == PlaybackStateCompat.ACTION_STOP) {
return KeyEvent.KEYCODE_MEDIA_STOP;
} else if(action == PlaybackStateCompat.ACTION_FAST_FORWARD) {
} else if (action == PlaybackStateCompat.ACTION_FAST_FORWARD) {
return KeyEvent.KEYCODE_MEDIA_FAST_FORWARD;
} else if(action == PlaybackStateCompat.ACTION_REWIND) {
} else if (action == PlaybackStateCompat.ACTION_REWIND) {
return KeyEvent.KEYCODE_MEDIA_REWIND;
} else if(action == PlaybackStateCompat.ACTION_PLAY_PAUSE) {
} else if (action == PlaybackStateCompat.ACTION_PLAY_PAUSE) {
return KeyEvent.KEYCODE_MEDIA_PLAY_PAUSE;
}
return KeyEvent.KEYCODE_UNKNOWN;
}

private NotificationCompat.Action createAction(String iconName, String title, long mask, long action, NotificationCompat.Action oldAction) {
if((mask & action) == 0) return null; // When this action is not enabled, return null
if(oldAction != null) return oldAction; // If this action was already created, we won't create another instance
if ((mask & action) == 0) return null; // When this action is not enabled, return null
if (oldAction != null)
return oldAction; // If this action was already created, we won't create another instance

// Finds the icon with the given name
Resources r = context.getResources();
Expand All @@ -181,36 +184,49 @@ public IBinder onBind(Intent intent) {
return null;
}

private boolean isRunning;
private Notification notification;

@Override
public void onCreate() {
super.onCreate();

if(Build.VERSION.SDK_INT >= Build.VERSION_CODES.O) {
Notification notification = MusicControlModule.INSTANCE.notification.prepareNotification(MusicControlModule.INSTANCE.nb,false);
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.O) {
notification = MusicControlModule.INSTANCE.notification.prepareNotification(MusicControlModule.INSTANCE.nb, false);
startForeground(NOTIFICATION_ID, notification);
isRunning = true;
}
}

@Override
public int onStartCommand(Intent intent, int flags, int startId) {
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.O) {
if (intent.getAction() != null && intent.getAction().equals("StopService") && notification != null && isRunning) {
stopForeground(true);
isRunning = false;
stopSelf();
}
}
return START_NOT_STICKY;
}

@Override
public void onTaskRemoved(Intent rootIntent) {
isRunning = false;
// Destroy the notification and sessions when the task is removed (closed, killed, etc)
if(MusicControlModule.INSTANCE != null) {
if (MusicControlModule.INSTANCE != null) {
MusicControlModule.INSTANCE.destroy();
}
if(Build.VERSION.SDK_INT >= Build.VERSION_CODES.O) {
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.O) {
stopForeground(true);
}
stopSelf(); // Stop the service as we won't need it anymore
}

@Override
public void onDestroy() {
if(Build.VERSION.SDK_INT >= Build.VERSION_CODES.O) {
isRunning = false;
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.O) {
stopForeground(true);
}
}
Expand Down

0 comments on commit 91ab64b

Please sign in to comment.