1
votes

I'm trying to get raw push notifications to go to a background task. This already works when the app is in the foreground, so I know it's not anything with getting the notification delivered.

Here's the steps I used.

In the manifest:

  <Extensions>
    <Extension Category="windows.backgroundTasks" 
               EntryPoint="TestApp.BackgroundNotifications">
      <BackgroundTasks>
        <Task Type="pushNotification" />
      </BackgroundTasks>
    </Extension>
  </Extensions>

The background class:

namespace TestApp {
  public sealed class BackgroundNotifications : IBackgroundTask {
    public void Run(IBackgroundTaskInstance taskInstance) {
      Debug.WriteLine("Got background notification");
    }
  }
}

And then the registration:

  BackgroundAccessStatus status =
    await BackgroundExecutionManager.RequestAccessAsync();
  BackgroundTaskBuilder taskBuilder =
    new BackgroundTaskBuilder {
      Name = "MyBackgroundNotifications",
      TaskEntryPoint = "TestApp.BackgroundNotifications"
    };
  taskBuilder.SetTrigger(new PushNotificationTrigger());
  BackgroundTaskRegistration myTask = taskBuilder.Register();

When I send a raw push notification I see this in the log but not my debug message:

The program '[16660] backgroundTaskHost.exe' has exited with code 1 (0x1).

Is there a step I'm missing?

1

1 Answers

0
votes

The code you've posted is right. In your steps, it seems you've forgotten to Reference the background tasks project from your Universal Windows Platform (UWP) app project.

In your UWP app, you can right click on References and select Add Reference. Under Projects, select Solution and then select the name of your background task project and click OK.

After this, you can rebuild your project, then your background task should be able to work.

For more info, please see Create and register an out-of-process background task.