Skip to content

Commit 00ffdcc

Browse files
biomancerGuichaguri
authored andcommitted
[androidFix: Bad notification for startForeground: java.lang.RuntimeException: invalid channel for service notification: null (doublesymmetry#723)
1 parent 9b60f3f commit 00ffdcc

File tree

3 files changed

+22
-16
lines changed

3 files changed

+22
-16
lines changed

android/src/main/java/com/guichaguri/trackplayer/service/MusicService.java

Lines changed: 3 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
package com.guichaguri.trackplayer.service;
22

3-
import android.app.NotificationChannel;
3+
import android.content.Context;
44
import android.content.Intent;
55
import android.os.Build;
66
import android.os.Bundle;
@@ -15,6 +15,7 @@
1515
import com.facebook.react.bridge.Arguments;
1616
import com.facebook.react.bridge.ReactContext;
1717
import com.facebook.react.jstasks.HeadlessJsTaskConfig;
18+
import com.guichaguri.trackplayer.service.Utils;
1819
import javax.annotation.Nullable;
1920

2021
/**
@@ -71,11 +72,7 @@ private void onStartForeground() {
7172

7273
// Checks whether there is a React activity
7374
if(reactContext == null || !reactContext.hasCurrentActivity()) {
74-
String channel = null;
75-
76-
if(Build.VERSION.SDK_INT >= Build.VERSION_CODES.O) {
77-
channel = NotificationChannel.DEFAULT_CHANNEL_ID;
78-
}
75+
String channel = Utils.getNotificationChannel((Context) this);
7976

8077
// Sets the service to foreground with an empty notification
8178
startForeground(1, new NotificationCompat.Builder(this, channel).build());

android/src/main/java/com/guichaguri/trackplayer/service/Utils.java

Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,9 +1,12 @@
11
package com.guichaguri.trackplayer.service;
22

3+
import android.app.NotificationChannel;
4+
import android.app.NotificationManager;
35
import android.content.ContentResolver;
46
import android.content.Context;
57
import android.content.res.Resources;
68
import android.net.Uri;
9+
import android.os.Build;
710
import android.os.Bundle;
811
import android.support.v4.media.RatingCompat;
912
import android.support.v4.media.session.PlaybackStateCompat;
@@ -156,4 +159,18 @@ public static int getInt(Bundle data, String key, int defaultValue) {
156159
}
157160
return defaultValue;
158161
}
162+
163+
public static String getNotificationChannel(Context context) {
164+
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.O) {
165+
NotificationChannel channel = new NotificationChannel(
166+
Utils.NOTIFICATION_CHANNEL,
167+
"MusicService",
168+
NotificationManager.IMPORTANCE_DEFAULT
169+
);
170+
channel.setShowBadge(false);
171+
channel.setSound(null, null);
172+
((NotificationManager) context.getSystemService(Context.NOTIFICATION_SERVICE)).createNotificationChannel(channel);
173+
}
174+
return Utils.NOTIFICATION_CHANNEL;
175+
}
159176
}

android/src/main/java/com/guichaguri/trackplayer/service/metadata/MetadataManager.java

Lines changed: 2 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -55,16 +55,8 @@ public MetadataManager(MusicService service, MusicManager manager) {
5555
this.service = service;
5656
this.manager = manager;
5757

58-
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.O) {
59-
NotificationChannel channel = new NotificationChannel(Utils.NOTIFICATION_CHANNEL, "Playback", NotificationManager.IMPORTANCE_DEFAULT);
60-
channel.setShowBadge(false);
61-
channel.setSound(null, null);
62-
63-
NotificationManager not = (NotificationManager) service.getSystemService(Context.NOTIFICATION_SERVICE);
64-
not.createNotificationChannel(channel);
65-
}
66-
67-
this.builder = new NotificationCompat.Builder(service, Utils.NOTIFICATION_CHANNEL);
58+
String channel = Utils.getNotificationChannel((Context) service);
59+
this.builder = new NotificationCompat.Builder(service, channel);
6860
this.session = new MediaSessionCompat(service, "TrackPlayer", null, null);
6961

7062
session.setFlags(MediaSessionCompat.FLAG_HANDLES_QUEUE_COMMANDS);

0 commit comments

Comments
 (0)