I'm making a location-based TODO notifying app. One of it's main features - is to send a notification to the user - if he's close to one of the location he defined.
What I need to do is to listen to the user's location - even when the app is closed.
I have come across the idea of a foreground service (This are 2 of it's function):
@Override
public void onCreate() {
super.onCreate();
if (Build.VERSION.SDK_INT > Build.VERSION_CODES.O)
{
startForegroundCustom();
}
else
{
startForeground(1, new Notification());
}
}
@RequiresApi(Build.VERSION_CODES.O)
private void startForegroundCustom()
{
// this is cause of the O api that requires this notification settings...
String NOTIFICATION_CHANNEL_ID = "example.permanence";
String channelName = "Background Service";
NotificationChannel chan = new NotificationChannel
(NOTIFICATION_CHANNEL_ID, channelName, NotificationManager.IMPORTANCE_NONE);
chan.setLightColor(Color.BLUE);
chan.setLockscreenVisibility(Notification.VISIBILITY_PRIVATE);
NotificationManager manager = (NotificationManager) getSystemService(Context.NOTIFICATION_SERVICE);
assert manager != null;
manager.createNotificationChannel(chan);
NotificationCompat.Builder notificationBuilder = new NotificationCompat.Builder
(this, NOTIFICATION_CHANNEL_ID);
Notification notification =
notificationBuilder.setOngoing(true)
.setContentTitle("Hello from remindify!")
.setPriority(NotificationManager.IMPORTANCE_NONE)
.setCategory(Notification.CATEGORY_SERVICE)
.setSmallIcon(R.drawable.ic_launcher_background)
.build();
System.out.println(notification);
startForeground(2, notification);
}
Basically the startForeground at the end start the "Timer" of the Service.
Problem - it HAS to display a notification. I checked, and there's no way to not display a notification. As said here - How to startForeground() without showing notification?
My question is - do I have an alternative? or do I have to use this foreground service?
TL;DR: I need a way to listen to the user's location and send him notifications, even when the app is closed - which isn't a ForegroundService.