Windowscommunitytoolkit: Notifications in Background Tasks does not seem to work

Created on 7 Mar 2017  路  25Comments  路  Source: windows-toolkit/WindowsCommunityToolkit

I am using Background Task in Single-Process Model to create notifications.
I create a ScheduledToastNotification each time the background is launched.

It works really great using the debug mode (with Visual Studio) but using in release mode is doing nothing..

I am guessing that's because the Run method is a void method and I use async/await in it so it should be a Task method. But that does not explain why it works in debug mode and not in release mode.

cc. @Code-ScottLe

bug

Most helpful comment

Correct, notifications will only appear while the PC is on. And if a ScheduledToastNotification misses a delivery time by more than 5 minutes (since the computer was off), it will get deleted.

All 25 comments

You mean you were trying to schedule a toast notification using SPM, but nothing happens in Release build? Can you post a sample code on this issue as well? I will take a look on this when I get home today.

Remember that if you will be using asynchronous code with background tasks, make sure to use defferal to make sure that the code will be given enough time to return from the asynchronous call.

If you run any asynchronous code in your background task, then your background task needs to use a deferral. If you don't use a deferral, then the background task process can terminate unexpectedly if the Run method completes before your asynchronous method call has completed.

https://docs.microsoft.com/en-us/windows/uwp/launch-resume/create-and-register-a-background-task

@Code-ScottLe Yes, I use a deferral. I'll post the code later today.

any updates? @Odonno

Sorry. Here is the zipped project.

BackgroundTaskNotifications.zip

Your TestBackgroundTask doesn't return a task, so it's not awaited, the deferral & background task completes before your background code had a chance to do anything

public class TestBackgroundTask : IBackgroundTask
{
    public async void Run(IBackgroundTaskInstance taskInstance)
    {
        // Do awaitable stuff
        await Task.Delay(1000);

Make it be

public async Task Run(IBackgroundTaskInstance

@anbare Yes, thanks for the info. But I asked myself why does it work in debug mode? There must be an explanation?

Did you set a breakpoint? Or perhaps as it is slower in debug, your code can be executed before the BackgroundTask is stopped

@deltakosh No breakpoint. Already tried 3 or 4 times and it always worked. It may be because it is slower in debug but that seems very odd.

I'll try with a Task like @anbare said and see if it change anything.

thanks!

Yeah debug mode is a lot less optimized, it won't kill background tasks immediately. I've seen this behavior myself. Release mode terminates immediately as soon as it can. Debug mode lets it hang around long enough that the code completes.

Someone who actually built the core Windows background tasks implementation probably knows why, but I'm not sure knowing the reason why changes anything, other than it's just fun to know things :)

I just downloaded the sample and it is like @anbare said. I usually don't create another IBackgroundTask implementation whenever I use SPM and just treat the OnBackgroundActivated event handler as a giant IBackgroundTask implementation. Async void is fire and forget and that's probably why (you guys discussed it too), but still don't make any sense in Debug mode lol.

I tried with Task, but same as before. It works on debug mode only.

I don't know why the TimeTrigger is not triggered.

Can you manually trigger it through the Debug Location Toolbar? (as you might already tried, since debug works, but just want to confirm)

@Code-ScottLe Sorry. I will try to be clear enough about the problem.

  • when I manually trigger in debug mode (using the toolbar), it works every time
  • when the app stays in background (whatever configuration I choose), the task is never triggered

It's cool. I will get back to you once I'm done with work. Seems a little weird because if the debug location can trigger it, then at least it is registered with the system.

@Odonno I was able to get a toast working yesterday. The time trigger is a little bit finicky but it does work. Haven't tried yours solution yet but mine was similar. Will be trying yours next.

@Code-ScottLe Ok. Keep me in touch.

Any updates? I'll close if no news

I saw something recently. I received notifications at midnight, which seems ok because it is the scheduled time of the notification but it only works if my PC is active at a precise datetime...

I don't know if it is the expected behavior. But if that's so, I should change my code to push the notification only when the PC is active.

Correct, notifications will only appear while the PC is on. And if a ScheduledToastNotification misses a delivery time by more than 5 minutes (since the computer was off), it will get deleted.

@anbare Is that the reason why Alarm & Clock never works on my Surface ? I learned that the hard way lol. I know the behavior well, but never got the explanation until now.

@anbare Ok, I will fix my code and see if it works as expected.

@Code-ScottLe Yep, the Alarms app has a nice warning when you use it on PC that your computer has to be awake

image

Ok, it seems that using a regular ToastNotification and not the scheduled one works better for me.

Thanks for your help.
Sorry for the inconvenience.

Was this page helpful?
0 / 5 - 0 ratings