none
UWP Toasted notification invoked OnBackgroundActivated multiple times

    Question

  • [Ref] https://stackoverflow.com/questions/55136828/uwp-toasted-notification-invoked-onbackgroundactivated-multiple-times

    Hi All,

    Microsoft given a code example to use background toasted notifications.
    https://github.com/WindowsNotifications/quickstart-toast-pending-update

    If you try the same example locally, you will observer that OnBackgroundActivated method get invoked multiple times. 

    If you invoke the toasted notification and lock the windows and  press order button then `OnBackgroundActivated` method get invoked (In my case 5 times with same argument).
    After that even if you unlock the windows, `OnBackgroundActivated` method get invoked multiple times.

    Is that a bug or there are any workaround to fix this issue. I have to keep my toasted notification on the screen because of that i want to use `Background` `ActivationType` with 
    `PendingUpdate` as `ActivationOption`

    Can someone help me to understand why this is get called multiple times.
    Thanks

    Wednesday, March 13, 2019 7:55 AM

Answers

  • Hi,

    Well, there is an error in the sample code. In the App.xaml.cs, the background is register for multiple times because the if check sentence is checking for a wrong background task name.

     private void RegisterBackgroundTasks()
            {
                // Actually the background task name registered is 'MyToastNotificationActionTrigger'
               // const string taskName = "ToastBackgroundTask";
               // so it should be like this:
                const string taskName = "MyToastNotificationActionTrigger";
    
                // If background task is already registered, do nothing
                if (BackgroundTaskRegistration.AllTasks.Any(i => i.Value.Name.Equals(taskName)))
                    return;
    
                // Create the background task
                BackgroundTaskBuilder builder = new BackgroundTaskBuilder()
                {
                    Name = "MyToastNotificationActionTrigger",
                };
    
                // Assign the toast action trigger
                builder.SetTrigger(new ToastNotificationActionTrigger());
    
                // And register the task
                BackgroundTaskRegistration registration = builder.Register();
            }
    I suggest that you need to uninstalled the sample from your device and then run the sample again. It should works correctly in that time.

    Best regards,

    Roy


    MSDN Community Support
    Please remember to click "Mark as Answer" the responses that resolved your issue, and to click "Unmark as Answer" if not. This can be beneficial to other community members reading this thread. If you have any compliments or complaints to MSDN Support, feel free to contact MSDNFSF@microsoft.com.

    Thursday, March 14, 2019 9:57 AM
    Moderator