Skip to content

Commit

Permalink
Don't use notification for "old" non-foreground based services - ensu…
Browse files Browse the repository at this point in the history
…re service does shutdown by sending intent broadcast
  • Loading branch information
aritchie committed Nov 2, 2023
1 parent a719bcd commit fa9c8af
Show file tree
Hide file tree
Showing 2 changed files with 7 additions and 10 deletions.
3 changes: 2 additions & 1 deletion src/Shiny.Core/Platforms/Android/AndroidPlatform.cs
Original file line number Diff line number Diff line change
Expand Up @@ -129,7 +129,8 @@ public void StopService(Type serviceType)
{
var intent = new Intent(this.AppContext, serviceType);
intent.SetAction(ActionServiceStop);
this.AppContext.StopService(intent);
this.AppContext.StartService(intent);
//this.AppContext.StopService(intent);
}


Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -28,7 +28,6 @@ protected int NotificationId
this.privateNotificationId ??= ++startNotificationId;
return this.privateNotificationId.Value;
}
//set => this.notificationId = value;
}

protected NotificationCompat.Builder? Builder { get; private set; }
Expand Down Expand Up @@ -94,16 +93,14 @@ protected virtual void Start(Intent? intent)
var notification = this.Builder.Build();
notification.Flags |= NotificationFlags.ForegroundService;

this.NotificationManager!.Notify(this.NotificationId, notification);

if (OperatingSystemShim.IsAndroidVersionAtLeast(31))
{
this.StartForeground(this.NotificationId, notification);
this.Logger.LogDebug("Started Foreground Service");
}
else
{
this.NotificationManager!.Notify(this.NotificationId!, notification);
//this.NotificationManager!.Notify("SERVICE", this.NotificationId!, notification);
this.Logger.LogDebug("Started Classic Foreground Service");
}

Expand All @@ -117,24 +114,23 @@ protected void Stop()
this.DestroyWith?.Dispose();
this.DestroyWith = null;

// this doesn't seem to work for 13 or 14 for me
//if (OperatingSystemShim.IsAndroidVersionAtLeast(33))
//{
// this.Logger.LogDebug("API level 33+ foreground service shutdown");
// this.StopForeground(StopForegroundFlags.Detach);
// this.StopSelf();
//}
//else
if (OperatingSystemShim.IsAndroidVersionAtLeast(31))
{
this.Logger.LogDebug("API level 31+ foreground service shutdown");
this.StopForeground(true);
this.StopSelf();
//this.StopSelf();
}
else
{
this.Logger.LogDebug("API level classic foreground service shutdown");
//this.NotificationManager!.Cancel(this.NotificationId);
this.StopSelf();
this.NotificationManager!.Cancel(this.NotificationId);
}

this.Logger.LogDebug("Foreground service stopped successfully");
Expand Down Expand Up @@ -162,7 +158,7 @@ protected virtual NotificationCompat.Builder CreateNotificationBuilder()
var build = new NotificationCompat.Builder(this.Platform.AppContext, NotificationChannelId)
.SetSmallIcon(this.Platform.GetNotificationIconResource())
.SetForegroundServiceBehavior((int)NotificationForegroundService.Immediate)
.SetOngoing(false) //.SetOngoing(true)
.SetOngoing(true)
.SetOnlyAlertOnce(true)
.SetTicker("...")
.SetContentTitle("Shiny Service")
Expand Down

0 comments on commit fa9c8af

Please sign in to comment.