0
votes

I have been trying to write Background Task that would show raw push notification as toast. I got push notifications working when app is running.

This is my background task class:

public sealed class BackgroundNotificationsTask : IBackgroundTask
{

    public void Run(IBackgroundTaskInstance taskInstance)
    {
        RawNotification notification = (RawNotification)taskInstance.TriggerDetails;
        string content = notification.Content;

        Debug.WriteLine("Background raw notification obtained!");

        //SendNotification(content);
    }

    private void SendNotification(string text)
    {
        XmlDocument toastXml = ToastNotificationManager.GetTemplateContent(ToastTemplateType.ToastText01);

        XmlNodeList elements = toastXml.GetElementsByTagName("text");
        foreach (IXmlNode node in elements)
        {
            node.InnerText = text;
        }

        ToastNotification notification = new ToastNotification(toastXml);
        ToastNotificationManager.CreateToastNotifier().Show(notification);
    }

}

Then I Register In MainPage.xaml.cs

    private void RegisterTasks()
    {
        BackgroundAccessStatus status = await BackgroundExecutionManager.RequestAccessAsync();

        var taskRegistered = false;
        var exampleTaskName = "NotificationsBackground";

        foreach (var task in BackgroundTaskRegistration.AllTasks)
        {
            if (task.Value.Name == exampleTaskName)
            {
                taskRegistered = true;
                break;
            }
        }

        if(!taskRegistered)
        {
            var builder = new BackgroundTaskBuilder();

            builder.Name = exampleTaskName;
            builder.TaskEntryPoint = "BackgroundTasks.NotificationsBackground";
            builder.SetTrigger(new Windows.ApplicationModel.Background.PushNotificationTrigger());

            try
            {
                BackgroundTaskRegistration task = builder.Register();

                Debug.WriteLine("Background Task registered.");
            }
            catch (Exception e)
            {
                Debug.WriteLine("Background Task register exception: " + e.ToString());
            }
        }
    }

Now in appxmanifest I have set 'Lock screen notifications' to Badge, then in Declarations I have added Background Task with properies Push notification selected and entry point set as BackgroundNotificationsTask.cs

![screen][2]

Am I doing something wron or is there something that I am missing?

EDIT:

Right now when i obtain push notification the app closes... anyone know why?

hah

1
Show how you are registering your task - RenDishen

1 Answers

5
votes

There are a couple of things you're doing wrong.

1) Put your BackgroundTask in a separate project

BackgroundTask projects should be Windows Runtime Components. Also make sure that your background task resides under an accessible namespace. Do not forget to reference the background task project from your app project.

2) Register the correct class

When registering your background task, always use the fully qualified class name and not the file name:

BackgroundTasks.BackgroundNotificationsTask

This is the entry point you'll have to use in the package manifest file and in your code (given that the task class is in the project explained under 1) and the namespace is called BackgroundTasks).

3) Call RequestAccessAsync()

Make sure you call this before registering any tasks:

BackgroundAccessStatus status = await BackgroundExecutionManager.RequestAccessAsync();

Edit: There is a pretty good walkthrough on MSDN https://msdn.microsoft.com/en-us/library/windows/apps/xaml/hh977055.aspx