Search code examples
c#windows-phone-8.1background-task

Disabling background task


My app handles raw push notifications, but it processes them differently when it's running on foreground or as a background task.

Right now when the application starts I check if there's a bg task registered and if it is then I unregistered the task and let the app handle push notifications and when the app is being closed or suspended I re-register the bg task.

This approach doesn't seem very clean to me. Is there a better way to pause the background task when the app is active or a way to check application's state from IBackgroundTask?


Solution

  • First please check if your notification is passed to BackgroundTask, as MSDN says:

    When your app's cloud service sends a notification to Windows, your app has the opportunity to intercept and handle that notification before it displays a toast, updates a tile or badge, or delivers a raw notification to a background task.

    In case you have running app, you should be able to intercept the notification before it goes to background task or elsewhere.

    In case you want to differentiate the background task's job, depending on UI, you can provide a flag in LocalSettings which is responsible for UI state, then let your BTask check the flag and decide if to perform work.

    The UI only changes the flag in settings, you don't have to unregister BTask then.