I have been working on a xamarin form app. The primary purpose of the app is to let user create a count down timer and if user doesn't stop the timer before it reaches zero then it sends SMS messages to some numbers.
I am using android foreground service to start a timer and display a count down timer. It is working fine, however, this solution is not 100% reliable as SMS will not be sent if for some reason phone is shut down after the timer has stared. Therefore, I would like to move the timer logic to .Net Web API (my app is already using .Net Web API for other functionality) so that when a user starts the timer, Web API on the server tracks the timer for the user who started the timer.
Can someone guide me to implement a reliable solution for web api for above scenario? I have looked at Hangfire and other scheduled tasks such as windows service but those seem too complicated for this seemingly easier task. The timer needs to be started and stopped dynamically via Web API and if it reaches zero then "SendSMS" method in Web API needs to be called.
2 Answers
Answers 1
You can use this example or other similar to send sms and implement the countdown timer on the web service side.
then you use HTTPClient class on Xamarin to inform the web service that the timer needs to be started or stoped.
Let me know if you need a code example.
Answers 2
You can take the advantage from System.Threading.Tasks Assembly.Like this
CancellationTokenSource tokenSource; private void CancelSendMessageTask() { tokenSource?.Cancel(); } private void StartTimer() { int SecondsToWait = 90; tokenSource = new CancellationTokenSource(TimeSpan.FromSeconds(SecondsToWait)); Task.Delay(TimeSpan.FromSeconds(SecondsToWait), tokenSource.Token).ContinueWith((t) => { if (t.IsCanceled) return; SendSMS(); }); }
0 comments:
Post a Comment