locked
[UWP][HTML]What is wrong with my background task registration? RRS feed

  • Question

  • Hello, I have a backgroundtask which is triggered on ToastNotificationActionTrigger. I tried the backgroundtask in C# and it's working just fine, but when I try to register the backgroundtask in Javascript, nothing happens.

    I set a debug stop point inside my background task and I don't even get into this point. Something must be wrong with my background task registration. 

    Here is what I did:

    function RegisterBackgroundTask() {
    	    const taskName = "ToastBackgroundTask";
    
    	    var backgroundTaskRegistration = Windows.ApplicationModel.Background.BackgroundTaskRegistration;
    	    var task = null;
    	    var taskRegistered = false;
    	    var iter = backgroundTaskRegistration.allTasks.first();
    
            // Check if task is already registered
    	    while (iter.hasCurrent) {
    	        task = iter.current.value;
    	        if (task.name === taskName) {
    	            taskRegistered = true;
    	            break;
    	        }
    	        iter.moveNext();
    	    }
    
            // If task isnt registered, register new task
    	    if (taskRegistered === false) {
    	        Windows.ApplicationModel.Background.BackgroundExecutionManager.requestAccessAsync().then(function () {
    	            var builder = new Windows.ApplicationModel.Background.BackgroundTaskBuilder();
    
    	            builder.name = taskName;
    	            builder.taskEntryPoint = "RuntimeComponent.ToastNotificationBackgroundTask";
    	            builder.setTrigger(new Windows.ApplicationModel.Background.ToastNotificationActionTrigger());
    
    	            builder.register();
    	        });
    	    }
    	}

    Thursday, November 24, 2016 7:41 PM

Answers

  • Hi,

    Sorry for late response. I tested your project. You are registering the background task correctly. But since it is a UWP(JS) project with C# Runtime Component. It can't be debugged in normal way.

    You need to enable the debug tracing and check the event logs of backgroundtaskInfrastructure:

     1. Control Panel->Administrative Tools->Open Event Viewer->View menu->check Show Analytic and Debug Logs.

     2.  Expand Applications and Service logs->Microsoft->Windows->BackgroundTaskInfrastructure->Select Diagnostic and right click enable logs->Run your application again and you will see the logs for your backgroundtask.

    Details please refer to https://msdn.microsoft.com/en-us/windows/uwp/launch-resume/debug-a-background-task?f=255&MSPPError=-2147217396.

    Best Regards,

    Elvis Xia

    • Marked as answer by VictorJOD Tuesday, December 13, 2016 3:10 AM
    Monday, December 12, 2016 5:33 AM

All replies

  • This is not a direct answer, but it may helps you:
    You can confirm that the your task is registered or not by using powershell. Open powershell with administrative rights, and run 'Get-AppBackgroundTask'. All of registered tasks are listed. If you can't find your task from the list, there are some problems at registration.
    Following tips also available but it's based on c#.
    http://stackoverflow.com/questions/38526948/
    Friday, November 25, 2016 5:16 AM
  • I did this and it seems like my background task is registered. Maybe there is something wrong with my trigger? Do you maybe happen to know if I have to change any code inside my runtime component? It worked fine when I tried it in C#, though.
    Friday, November 25, 2016 11:35 AM
  • Hi,

    Please try rename your background task name and run again.

    Best Regards,

    Elvis Xia

    Monday, November 28, 2016 2:29 AM
  • Hello VictorJOD,

    Any updates on this issue?

    Best Regards,

    Xavier


    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.


    Friday, December 2, 2016 1:56 AM
  • Yes! I'm sorry for the late reply. I changed the name multiple times but it still wouldn't work.

    I don't know what to do anymore. Here is the Visual Studio project file. I'm using the background task that also worked in my C# project, it worked just fine in C#, that's why I think it must be the registration that is wrong. Sadly, I can't find any examples of a backgroundtask registration in Javascript using a runtime component. 

    Edit: When I run this I'm setting a breakpoint to the beginning of the trigger

    https://1drv.ms/u/s!ArNSvnSslEHXx3Ng9qt1nJtFCDBp


    Edit: For some reason this link is not working anymore. Here is a new one https://1drv.ms/u/s!ArNSvnSslEHX1GJ5VceNvBgKyMc-
    • Edited by VictorJOD Tuesday, December 6, 2016 11:07 AM broken link
    Friday, December 2, 2016 11:16 PM
  • Hi,

    Could you please share a basic demo that can reproduce this problem?

    Best Regards,

    Elvis Xia

    Tuesday, December 6, 2016 3:02 AM
  • I have uploaded the project in my previous post

    https://1drv.ms/u/s!ArNSvnSslEHX1GJ5VceNvBgKyMc-

    in main.js there are two functions:

    RegisterBackgroundTask()

    addToast()

    I'm calling RegisterBackgroundTask after WinJS.Ui.processAll and then I add the eventlistener to the button that will create the toast with addToast().

    In the Runtime Componet ToastNotificationBackgroundTask.cs you can pretty much ignore everything after 

            public void Run(IBackgroundTaskInstance taskInstance)
            {

    or

                    if (taskInstance.TriggerDetails is ToastNotificationActionTriggerDetail)
                    {

    and set breakpoint. But as it is now, I won't even get into the backgroundtask since something is wrong with my registration, I think.

    Tuesday, December 6, 2016 11:06 AM
  • Hi,

    Sorry for late response. I tested your project. You are registering the background task correctly. But since it is a UWP(JS) project with C# Runtime Component. It can't be debugged in normal way.

    You need to enable the debug tracing and check the event logs of backgroundtaskInfrastructure:

     1. Control Panel->Administrative Tools->Open Event Viewer->View menu->check Show Analytic and Debug Logs.

     2.  Expand Applications and Service logs->Microsoft->Windows->BackgroundTaskInfrastructure->Select Diagnostic and right click enable logs->Run your application again and you will see the logs for your backgroundtask.

    Details please refer to https://msdn.microsoft.com/en-us/windows/uwp/launch-resume/debug-a-background-task?f=255&MSPPError=-2147217396.

    Best Regards,

    Elvis Xia

    • Marked as answer by VictorJOD Tuesday, December 13, 2016 3:10 AM
    Monday, December 12, 2016 5:33 AM