diff --git a/ultrasonic/src/main/java/org/moire/ultrasonic/service/DownloadServiceImpl.java b/ultrasonic/src/main/java/org/moire/ultrasonic/service/DownloadServiceImpl.java index 5558ed92..3e5708be 100644 --- a/ultrasonic/src/main/java/org/moire/ultrasonic/service/DownloadServiceImpl.java +++ b/ultrasonic/src/main/java/org/moire/ultrasonic/service/DownloadServiceImpl.java @@ -154,6 +154,7 @@ public class DownloadServiceImpl extends Service implements DownloadService private final static int lockScreenBitmapSize = 500; private boolean isInForeground = false; + private NotificationCompat.Builder notificationBuilder; static { @@ -262,6 +263,17 @@ public class DownloadServiceImpl extends Service implements DownloadService instance = this; lifecycleSupport.onCreate(); + + if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.O) { + NotificationChannel channel = new NotificationChannel(NOTIFICATION_CHANNEL_ID, NOTIFICATION_CHANNEL_NAME, NotificationManager.IMPORTANCE_NONE); + channel.setLightColor(android.R.color.holo_blue_dark); + channel.setLockscreenVisibility(Notification.VISIBILITY_PUBLIC); + NotificationManager manager = (NotificationManager) getSystemService(Context.NOTIFICATION_SERVICE); + manager.createNotificationChannel(channel); + } + + // We should use a single notification builder, otherwise the notification may not be updated + notificationBuilder = new NotificationCompat.Builder(this, NOTIFICATION_CHANNEL_ID); } @Override @@ -723,10 +735,7 @@ public class DownloadServiceImpl extends Service implements DownloadService if (currentPlaying != null) { if (tabInstance != null) { - if (Util.isNotificationEnabled(this)) { - startForeground(NOTIFICATION_ID, buildForegroundNotification()); - isInForeground = true; - } + updateNotification(); tabInstance.showNowPlaying(); } } @@ -2085,9 +2094,15 @@ public class DownloadServiceImpl extends Service implements DownloadService { if (Util.isNotificationEnabled(this)) { if (isInForeground == true) { - final NotificationManagerCompat notificationManager = + if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.O) { + NotificationManager notificationManager = (NotificationManager) getSystemService(Context.NOTIFICATION_SERVICE); + notificationManager.notify(NOTIFICATION_ID, buildForegroundNotification()); + } + else { + final NotificationManagerCompat notificationManager = NotificationManagerCompat.from(this); - notificationManager.notify(NOTIFICATION_ID, buildForegroundNotification()); + notificationManager.notify(NOTIFICATION_ID, buildForegroundNotification()); + } Log.w(TAG, "--- Updated notification"); } else { @@ -2100,31 +2115,23 @@ public class DownloadServiceImpl extends Service implements DownloadService @SuppressWarnings("IconColors") private Notification buildForegroundNotification() { - if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.O) { - NotificationChannel channel = new NotificationChannel(NOTIFICATION_CHANNEL_ID, NOTIFICATION_CHANNEL_NAME, NotificationManager.IMPORTANCE_NONE); - channel.setLightColor(android.R.color.holo_blue_dark); - channel.setLockscreenVisibility(Notification.VISIBILITY_PUBLIC); - NotificationManager manager = (NotificationManager) getSystemService(Context.NOTIFICATION_SERVICE); - manager.createNotificationChannel(channel); + notificationBuilder.setSmallIcon(R.drawable.ic_stat_ultrasonic); - } - NotificationCompat.Builder builder = new NotificationCompat.Builder(this, NOTIFICATION_CHANNEL_ID); - builder.setSmallIcon(R.drawable.ic_stat_ultrasonic); - - builder.setAutoCancel(false); - builder.setOngoing(true); - builder.setWhen(System.currentTimeMillis()); - builder.setVisibility(NotificationCompat.VISIBILITY_PUBLIC); + notificationBuilder.setAutoCancel(false); + notificationBuilder.setOngoing(true); + notificationBuilder.setOnlyAlertOnce(true); + notificationBuilder.setWhen(System.currentTimeMillis()); + notificationBuilder.setVisibility(NotificationCompat.VISIBILITY_PUBLIC); RemoteViews contentView = new RemoteViews(this.getPackageName(), R.layout.notification); Util.linkButtons(this, contentView, false); RemoteViews bigView = new RemoteViews(this.getPackageName(), R.layout.notification_large); Util.linkButtons(this, bigView, false); - builder.setContent(contentView); + notificationBuilder.setContent(contentView); Intent notificationIntent = new Intent(this, DownloadActivity.class); - builder.setContentIntent(PendingIntent.getActivity(this, 0, notificationIntent, 0)); + notificationBuilder.setContentIntent(PendingIntent.getActivity(this, 0, notificationIntent, 0)); if (playerState == PlayerState.PAUSED || playerState == PlayerState.IDLE) { contentView.setImageViewResource(R.id.control_play, R.drawable.media_start_normal_dark); @@ -2174,7 +2181,7 @@ public class DownloadServiceImpl extends Service implements DownloadService bigView.setImageViewResource(R.id.notification_five_star_5, rating > 4 ? R.drawable.ic_star_full_dark : R.drawable.ic_star_hollow_dark); } - Notification notification = builder.build(); + Notification notification = notificationBuilder.build(); if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.JELLY_BEAN) { notification.bigContentView = bigView; }