Fixed notification update error

This commit is contained in:
Nite 2020-06-18 15:53:38 +02:00
parent 04f80be254
commit ad1ae62aff
No known key found for this signature in database
GPG Key ID: 1D1AD59B1C6386C1
1 changed files with 30 additions and 23 deletions

View File

@ -154,6 +154,7 @@ public class DownloadServiceImpl extends Service implements DownloadService
private final static int lockScreenBitmapSize = 500; private final static int lockScreenBitmapSize = 500;
private boolean isInForeground = false; private boolean isInForeground = false;
private NotificationCompat.Builder notificationBuilder;
static static
{ {
@ -262,6 +263,17 @@ public class DownloadServiceImpl extends Service implements DownloadService
instance = this; instance = this;
lifecycleSupport.onCreate(); 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 @Override
@ -723,10 +735,7 @@ public class DownloadServiceImpl extends Service implements DownloadService
if (currentPlaying != null) if (currentPlaying != null)
{ {
if (tabInstance != null) { if (tabInstance != null) {
if (Util.isNotificationEnabled(this)) { updateNotification();
startForeground(NOTIFICATION_ID, buildForegroundNotification());
isInForeground = true;
}
tabInstance.showNowPlaying(); tabInstance.showNowPlaying();
} }
} }
@ -2085,9 +2094,15 @@ public class DownloadServiceImpl extends Service implements DownloadService
{ {
if (Util.isNotificationEnabled(this)) { if (Util.isNotificationEnabled(this)) {
if (isInForeground == true) { 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); NotificationManagerCompat.from(this);
notificationManager.notify(NOTIFICATION_ID, buildForegroundNotification()); notificationManager.notify(NOTIFICATION_ID, buildForegroundNotification());
}
Log.w(TAG, "--- Updated notification"); Log.w(TAG, "--- Updated notification");
} }
else { else {
@ -2100,31 +2115,23 @@ public class DownloadServiceImpl extends Service implements DownloadService
@SuppressWarnings("IconColors") @SuppressWarnings("IconColors")
private Notification buildForegroundNotification() { private Notification buildForegroundNotification() {
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.O) { notificationBuilder.setSmallIcon(R.drawable.ic_stat_ultrasonic);
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.setAutoCancel(false);
NotificationCompat.Builder builder = new NotificationCompat.Builder(this, NOTIFICATION_CHANNEL_ID); notificationBuilder.setOngoing(true);
builder.setSmallIcon(R.drawable.ic_stat_ultrasonic); notificationBuilder.setOnlyAlertOnce(true);
notificationBuilder.setWhen(System.currentTimeMillis());
builder.setAutoCancel(false); notificationBuilder.setVisibility(NotificationCompat.VISIBILITY_PUBLIC);
builder.setOngoing(true);
builder.setWhen(System.currentTimeMillis());
builder.setVisibility(NotificationCompat.VISIBILITY_PUBLIC);
RemoteViews contentView = new RemoteViews(this.getPackageName(), R.layout.notification); RemoteViews contentView = new RemoteViews(this.getPackageName(), R.layout.notification);
Util.linkButtons(this, contentView, false); Util.linkButtons(this, contentView, false);
RemoteViews bigView = new RemoteViews(this.getPackageName(), R.layout.notification_large); RemoteViews bigView = new RemoteViews(this.getPackageName(), R.layout.notification_large);
Util.linkButtons(this, bigView, false); Util.linkButtons(this, bigView, false);
builder.setContent(contentView); notificationBuilder.setContent(contentView);
Intent notificationIntent = new Intent(this, DownloadActivity.class); 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) { if (playerState == PlayerState.PAUSED || playerState == PlayerState.IDLE) {
contentView.setImageViewResource(R.id.control_play, R.drawable.media_start_normal_dark); 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); 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) { if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.JELLY_BEAN) {
notification.bigContentView = bigView; notification.bigContentView = bigView;
} }