question

Stesvis-5434 avatar image
0 Votes"
Stesvis-5434 asked Stesvis-5434 commented

Advice on background tasks with timer

Hello,

I am looking for a quick advice. My client requires his employees using the app to be reminded to "check-in" every 2 hours (kind of like to confirm "yes, i am still alive", :slight_smile: ).
Then, if they don't check-in within 10 minutes, their boss wants to be notified so he can call them (they work alone in remote places and it's a safety requirement).

So, basically:

  1. On the phone there must be a task being executed every two hours (the reminder)

  2. If there is no response within 10 minutes, send a notification back

How would you approach this?

My idea would be to:

  • set up a scheduled task on the server, that runs every 2 hours, and sends them a push notification

  • run another task 10 minutes later to check if the user checked-in

  • when the user clicks on the notification, i submit a check in

Now, i think it should work, however I have to run 2 scheduled tasks every 2 hours etc etc.

Is there a better way? I thought about handling this on the phone but i think it would require background tasks to run all the time in iOS and Android, which would be a little painful to set up, especially if you can't make them run when the app is closed etc...

Any idea? Thanks!

dotnet-xamarin
5 |1600 characters needed characters left characters exceeded

Up to 10 attachments (including images) can be used with a maximum of 3.0 MiB each and 30.0 MiB total.

1 Answer

KyleWang-MSFT avatar image
0 Votes"
KyleWang-MSFT answered Stesvis-5434 commented

Hi Stesvis-5434,

Welcome to our Microsoft Q&A platform!

To achieve such "check-in notification", I think using Device.StartTimer, which starts a recurring timer using the device clock capabilities, would be a much easier way.

You only need to call Device.StartTimer twice and declare a boolean variable to flag whether the user check-in or not.

Here is a simple demo.

MainPage.xaml

 <Button x:Name="StartTaskButton" Text="Start Task" Clicked="StartTaskButton_Clicked"/>
 <Button x:Name="CheckInButton" Text="check-in" Clicked="CheckInButton_Clicked" IsEnabled="False"/>

MainPage.xaml.cs

 int count = 0;
 bool isChecked = false;
    
 private void StartTaskButton_Clicked(object sender, EventArgs e)
 {
     // remind every 10 minutes
     Device.StartTimer(TimeSpan.FromMinutes(10), () =>
     {
         Task.Run(() =>
         {
             Debug.WriteLine($"Time is up {++count}");
             StartSendNotificationTask();
             Device.BeginInvokeOnMainThread(() =>
             {
                 CheckInButton.IsEnabled = true;
             });
         });
         return true;
     });
 }
    
 private void StartSendNotificationTask()
 {
     // need check-in in 1 minute
     Device.StartTimer(TimeSpan.FromMinutes(1), () =>
     {
         // if checked, cancel the timer
         if (isChecked)
         {
             isChecked = !isChecked;
             return false;
         }
         Task.Run(() =>
         {
             Debug.WriteLine($"Notification sended.");
             Device.BeginInvokeOnMainThread(() =>
             {
                 CheckInButton.IsEnabled = false;
             });
         });
         return false;
     });
 }
    
 private void CheckInButton_Clicked(object sender, EventArgs e)
 {
     CheckInButton.IsEnabled = false;
     isChecked = true;
 }

Regards,
Kyle


If the response is helpful, please click "Accept Answer" and upvote it.

Note: Please follow the steps in our documentation to enable e-mail notifications if you want to receive the related email notification for this thread.

· 3
5 |1600 characters needed characters left characters exceeded

Up to 10 attachments (including images) can be used with a maximum of 3.0 MiB each and 30.0 MiB total.

Thanks @KyleWang-MSFT what happens if they exit the app, restart the phone etc?
I think it will stop working right?

0 Votes 0 ·

@Stesvis-5434 Yes, it will stop. If so, notification from server will be the better choice. About notification when app closed, you can refer to these threads:
Xamarin.Forms How to show a notification even app is closed
receiving push notifications when app is closed in xamarin

0 Votes 0 ·

Thank you, on the mobile app I use the Firebase Push Notification Plugin
https://github.com/CrossGeeks/FirebasePushNotificationPlugin

So all the settings should be already set up correctly for when the app is closed.

0 Votes 0 ·