Add notification channel

This commit is contained in:
Karim Abou Zeid 2017-08-30 20:12:15 +02:00
commit 9da5f6c8d4
6 changed files with 30 additions and 10 deletions

View file

@ -1138,6 +1138,10 @@ public class MusicService extends Service implements SharedPreferences.OnSharedP
case PreferenceUtil.COLORED_NOTIFICATION: case PreferenceUtil.COLORED_NOTIFICATION:
updateNotification(); updateNotification();
break; break;
case PreferenceUtil.CLASSIC_NOTIFICATION:
initNotification();
updateNotification();
break;
} }
} }

View file

@ -1,8 +1,12 @@
package com.kabouzeid.gramophone.service.notification; package com.kabouzeid.gramophone.service.notification;
import android.app.Notification; import android.app.Notification;
import android.app.NotificationChannel;
import android.app.NotificationManager; 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 com.kabouzeid.gramophone.service.MusicService;
import static android.content.Context.NOTIFICATION_SERVICE; import static android.content.Context.NOTIFICATION_SERVICE;
@ -10,7 +14,7 @@ import static android.content.Context.NOTIFICATION_SERVICE;
public abstract class PlayingNotification { public abstract class PlayingNotification {
private static final int NOTIFICATION_ID = 1; 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_FOREGROUND = 1;
private static final int NOTIFY_MODE_BACKGROUND = 0; private static final int NOTIFY_MODE_BACKGROUND = 0;
@ -24,6 +28,9 @@ public abstract class PlayingNotification {
public synchronized void init(MusicService service) { public synchronized void init(MusicService service) {
this.service = service; this.service = service;
notificationManager = (NotificationManager) service.getSystemService(NOTIFICATION_SERVICE); notificationManager = (NotificationManager) service.getSystemService(NOTIFICATION_SERVICE);
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.O) {
createNotificationChannel();
}
} }
abstract public void update(); abstract public void update();
@ -54,4 +61,18 @@ public abstract class PlayingNotification {
notifyMode = newNotifyMode; 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);
}
}
} }

View file

@ -71,7 +71,7 @@ public class PlayingNotificationImpl extends PlayingNotification {
final PendingIntent clickIntent = PendingIntent.getActivity(service, 0, action, 0); final PendingIntent clickIntent = PendingIntent.getActivity(service, 0, action, 0);
final PendingIntent deleteIntent = buildPendingIntent(service, MusicService.ACTION_QUIT, null); 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) .setSmallIcon(R.drawable.ic_notification)
.setContentIntent(clickIntent) .setContentIntent(clickIntent)
.setDeleteIntent(deleteIntent) .setDeleteIntent(deleteIntent)

View file

@ -85,7 +85,7 @@ public class PlayingNotificationImpl24 extends PlayingNotification {
NotificationCompat.Action nextAction = new NotificationCompat.Action(R.drawable.ic_skip_next_white_24dp, NotificationCompat.Action nextAction = new NotificationCompat.Action(R.drawable.ic_skip_next_white_24dp,
service.getString(R.string.action_next), service.getString(R.string.action_next),
retrievePlaybackAction(ACTION_SKIP)); 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) .setSmallIcon(R.drawable.ic_notification)
.setLargeIcon(bitmap) .setLargeIcon(bitmap)
.setContentIntent(clickIntent) .setContentIntent(clickIntent)

View file

@ -260,13 +260,6 @@ public class SettingsActivity extends AbsBaseActivity implements ColorChooserDia
public boolean onPreferenceChange(Preference preference, Object newValue) { public boolean onPreferenceChange(Preference preference, Object newValue) {
// Save preference // Save preference
PreferenceUtil.getInstance(getActivity()).setClassicNotification((Boolean) newValue); PreferenceUtil.getInstance(getActivity()).setClassicNotification((Boolean) newValue);
final MusicService service = MusicPlayerRemote.musicService;
if (service != null) {
service.initNotification();
service.updateNotification();
}
return true; return true;
} }
}); });

View file

@ -301,4 +301,6 @@
<string name="app_shortcut_last_added_long">@string/last_added</string> <string name="app_shortcut_last_added_long">@string/last_added</string>
<string name="app_shortcut_last_added_short">@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="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> </resources>