2
votes

I am developing a Silverlight 8.1 app in c#. I want to use some of the old phone APIs inside my background periodic task, so I can't use the new IBackgroundTask RT interface.

I created an old style Background agent with OnInvoke override then registered as before in WMAppManifest.xml: <ExtendedTask Name="BackgroundTask"> <BackgroundServiceAgent Specifier="ScheduledTaskAgent" Name="BackgroundAgent" Source="BackgroundAgent" Type="BackgroundAgent.ScheduledAgent" /> </ExtendedTask>

Where BackgroundAgent is my background agent library and ScheduledAgent is my class.

I have enabled Toast notifications for the app in the Package.appxmanifest. My notification system is WNS (in WMAppManifest).

I am trying to launch the task in the old school way:

BackgroundExecutionManager.RequestAccessAsync();
  var periodicTask = ScheduledActionService.Find("BackgroundTask");

  if (periodicTask != null)
  {
    ScheduledActionService.Remove("BackgroundTask");
  }

  periodicTask = new PeriodicTask("BackgroundTask");
  (periodicTask as ScheduledTask).Description = "Hello, world.";
  ScheduledActionService.Add(periodicTask);

  ScheduledActionService.LaunchForTest("BackgroundTask", TimeSpan.FromSeconds(60));

Inside the OnNavigatedTo method of my MainPage.xaml.cs.

I can see this code run.

However, I never see any code run inside my background agent - it is supposed to send me a toast (I tried both with the ShellToast and the new ToastNotificationManager for xml-based toasts) and make a http call - both of which don't happen.

I watched the Build™ video where they mentioned that background agents should be fully supported in Silverlight 8.1 apps.

I can also confirm that my app appears as 'allowed' inside the battery Saver settings, which indicates that the background agent was registered with the os.

What am I doing wrong?

1
can you update the answer with full solution, so that it will help others..loop

1 Answers

3
votes

I think you need to have a background task of type "System event" with entry point "AgHost.BackgroundTask" in the Package.appxmanifest. That's what's hosting the SL background agent in WP8.1 SL apps. If you don't have this task in the manifest file, add it yourself and see it fixes your problem.

Update

From my experience the "System event" type should work, but zaitsman says that he needed to use "Timer", so if the first one does not work for you - try the other.

Update 2

In other words, you need to have this:

  <Extensions>
    <Extension Category="windows.backgroundTasks" EntryPoint="AgHost.BackgroundTask">
      <BackgroundTasks>
        <Task Type="systemEvent" />
        <Task Type="timer" />
      </BackgroundTasks>
    </Extension>
  </Extensions>

inside the Application tag of the Package.appxmanifest file (not to be confused with WMAppManifest.xml) of your WP 8.1 SL app.