Add notification channel
This commit is contained in:
parent
c0c91688b4
commit
9da5f6c8d4
6 changed files with 30 additions and 10 deletions
|
|
@ -1138,6 +1138,10 @@ public class MusicService extends Service implements SharedPreferences.OnSharedP
|
|||
case PreferenceUtil.COLORED_NOTIFICATION:
|
||||
updateNotification();
|
||||
break;
|
||||
case PreferenceUtil.CLASSIC_NOTIFICATION:
|
||||
initNotification();
|
||||
updateNotification();
|
||||
break;
|
||||
}
|
||||
}
|
||||
|
||||
|
|
|
|||
|
|
@ -1,8 +1,12 @@
|
|||
package com.kabouzeid.gramophone.service.notification;
|
||||
|
||||
import android.app.Notification;
|
||||
import android.app.NotificationChannel;
|
||||
import android.app.NotificationManager;
|
||||
import android.os.Build;
|
||||
import android.support.annotation.RequiresApi;
|
||||
|
||||
import com.kabouzeid.gramophone.R;
|
||||
import com.kabouzeid.gramophone.service.MusicService;
|
||||
|
||||
import static android.content.Context.NOTIFICATION_SERVICE;
|
||||
|
|
@ -10,7 +14,7 @@ import static android.content.Context.NOTIFICATION_SERVICE;
|
|||
public abstract class PlayingNotification {
|
||||
|
||||
private static final int NOTIFICATION_ID = 1;
|
||||
protected static final String NOTIFICATION_CHANNEL = "notif_channel";
|
||||
protected static final String NOTIFICATION_CHANNEL_ID = "playing_notification";
|
||||
|
||||
private static final int NOTIFY_MODE_FOREGROUND = 1;
|
||||
private static final int NOTIFY_MODE_BACKGROUND = 0;
|
||||
|
|
@ -24,6 +28,9 @@ public abstract class PlayingNotification {
|
|||
public synchronized void init(MusicService service) {
|
||||
this.service = service;
|
||||
notificationManager = (NotificationManager) service.getSystemService(NOTIFICATION_SERVICE);
|
||||
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.O) {
|
||||
createNotificationChannel();
|
||||
}
|
||||
}
|
||||
|
||||
abstract public void update();
|
||||
|
|
@ -54,4 +61,18 @@ public abstract class PlayingNotification {
|
|||
|
||||
notifyMode = newNotifyMode;
|
||||
}
|
||||
|
||||
@RequiresApi(26)
|
||||
private void createNotificationChannel() {
|
||||
NotificationChannel notificationChannel = notificationManager.getNotificationChannel(NOTIFICATION_CHANNEL_ID);
|
||||
if (notificationChannel == null) {
|
||||
notificationChannel = new NotificationChannel(NOTIFICATION_CHANNEL_ID, service.getString(R.string.playing_notification_name), NotificationManager.IMPORTANCE_LOW);
|
||||
notificationChannel.setDescription(service.getString(R.string.playing_notification_description));
|
||||
notificationChannel.enableLights(false);
|
||||
notificationChannel.enableVibration(false);
|
||||
notificationChannel.setShowBadge(false);
|
||||
|
||||
notificationManager.createNotificationChannel(notificationChannel);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
|
|
|||
|
|
@ -71,7 +71,7 @@ public class PlayingNotificationImpl extends PlayingNotification {
|
|||
final PendingIntent clickIntent = PendingIntent.getActivity(service, 0, action, 0);
|
||||
final PendingIntent deleteIntent = buildPendingIntent(service, MusicService.ACTION_QUIT, null);
|
||||
|
||||
final Notification notification = new NotificationCompat.Builder(service, NOTIFICATION_CHANNEL)
|
||||
final Notification notification = new NotificationCompat.Builder(service, NOTIFICATION_CHANNEL_ID)
|
||||
.setSmallIcon(R.drawable.ic_notification)
|
||||
.setContentIntent(clickIntent)
|
||||
.setDeleteIntent(deleteIntent)
|
||||
|
|
|
|||
|
|
@ -85,7 +85,7 @@ public class PlayingNotificationImpl24 extends PlayingNotification {
|
|||
NotificationCompat.Action nextAction = new NotificationCompat.Action(R.drawable.ic_skip_next_white_24dp,
|
||||
service.getString(R.string.action_next),
|
||||
retrievePlaybackAction(ACTION_SKIP));
|
||||
NotificationCompat.Builder builder = new NotificationCompat.Builder(service, NOTIFICATION_CHANNEL)
|
||||
NotificationCompat.Builder builder = new NotificationCompat.Builder(service, NOTIFICATION_CHANNEL_ID)
|
||||
.setSmallIcon(R.drawable.ic_notification)
|
||||
.setLargeIcon(bitmap)
|
||||
.setContentIntent(clickIntent)
|
||||
|
|
|
|||
|
|
@ -260,13 +260,6 @@ public class SettingsActivity extends AbsBaseActivity implements ColorChooserDia
|
|||
public boolean onPreferenceChange(Preference preference, Object newValue) {
|
||||
// Save preference
|
||||
PreferenceUtil.getInstance(getActivity()).setClassicNotification((Boolean) newValue);
|
||||
|
||||
final MusicService service = MusicPlayerRemote.musicService;
|
||||
if (service != null) {
|
||||
service.initNotification();
|
||||
service.updateNotification();
|
||||
}
|
||||
|
||||
return true;
|
||||
}
|
||||
});
|
||||
|
|
|
|||
|
|
@ -301,4 +301,6 @@
|
|||
<string name="app_shortcut_last_added_long">@string/last_added</string>
|
||||
<string name="app_shortcut_last_added_short">@string/last_added</string>
|
||||
<string name="playlist_is_empty">Playlist is empty</string>
|
||||
<string name="playing_notification_description">The playing notification provides actions for play/pause etc.</string>
|
||||
<string name="playing_notification_name">Playing notification</string>
|
||||
</resources>
|
||||
|
|
|
|||
Loading…
Add table
Add a link
Reference in a new issue