Maury
Maury

Reputation: 604

Run background task every X amount of time

I would like to start a service that once in awhile on all platforms has checked is there a notification to appear or not. Is there any nuget to connect all platforms or some examples?

Upvotes: 6

Views: 15458

Answers (2)

user901366
user901366

Reputation: 91

I think that the best that you can do is following:

Unfortunately, the way that these two platforms have evolved to handle executing background code is completely different. As such, there is no way that we can abstract the backgrounding feature into the Xamarin.Forms library. Instead, we going to continue to rely on the native APIs to execute our shared background task.

Further information for this topic can be found here: https://robgibbens.com/backgrounding-with-xamarin-forms/

Upvotes: 1

pnavk
pnavk

Reputation: 4632

You can use the Device.StartTimer(TimeSpan minutes) method to start a background task that will repeat after the given time span. Here is a code example:

var minutes = TimeSpan.FromMinutes (3); 

Device.StartTimer (minutes, () => {

    // call your method to check for notifications here

    // Returning true means you want to repeat this timer
    return true;
});

This is included with Xamarin Forms, so you don't need any platform specific logic.

http://iosapi.xamarin.com/index.aspx?link=M%3AXamarin.Forms.Device.StartTimer(System.TimeSpan%2CSystem.Func%7BSystem.Boolean%7D)

Upvotes: 11

Related Questions