1

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.

nortain32
  • 69
  • 1
  • 7
  • Use a periodic WorkManager to run in the background that listens to the user's location at periodic intervals of time and then if the condition satisfy, fire the notification – Abhishek sheshagiri Jan 10 '23 at 03:45
  • Will this run in the background? I don't want the user to be prompted with "App is still running, click here to close... etc". So how do I implement this? Can you link me to place that explains the basics, please? Thanks in advancd! – nortain32 Jan 10 '23 at 10:23
  • yes, it works in the background too. Check this documentation - https://developer.android.com/topic/libraries/architecture/workmanager I have used this to schedule periodic notifications in the past and many other such requirements – Abhishek sheshagiri Jan 10 '23 at 15:45

0 Answers0