I've been trying to implement a BackgroundTask for Raw Push Notifications on my Windows and Windows Phone 8.1 apps but it doesn't seem to be working. I've successfully managed to get toast based push notifications working but as far as I'm aware a Raw notification silently pushes data to the app and it's up to the app to display a toast notification or update the app's tile.
I've looked at the BackgroundTask Sample and followed it exactly yet nothing works (https://code.msdn.microsoft.com/windowsapps/Background-Task-Sample-9209ade9).
Here's the steps I've taken
- Created a Windows Runtime Component Project in the same solution as my other projects (Called NotificationServer)
- Renamed the class to RawTask.cs and implemented
IBackgroundTask
and itsRun
method Created a method to create a toast notification
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); }
Added code to the Run method
BackgroundTaskDeferral _deferral = taskInstance.GetDeferral();
RawNotification notification = (RawNotification)taskInstance.TriggerDetails; string content = notification.Content; // ... SendNotification("test"); // ... _deferral.Complete();
Updated my App's manifest to
Toast Capable = YES
andLock Screen Notifications = Badge
Added a Declaration for a Background Task with
Supported Task Type = Push Notification
andEntry Point = NotificationServer.RawTask
Added code to register the Background Task
public static BackgroundTaskRegistration RegisterBackgroundTask(string taskEntryPoint, string taskName, IBackgroundTrigger trigger, IBackgroundCondition condition) { // // Check for existing registrations of this background task. //
foreach (var cur in BackgroundTaskRegistration.AllTasks) { if (cur.Value.Name == taskName) { // // The task is already registered. // return (BackgroundTaskRegistration)(cur.Value); } } // // Register the background task. // var builder = new BackgroundTaskBuilder(); builder.Name = taskName; builder.TaskEntryPoint = taskEntryPoint; builder.SetTrigger(trigger); if (condition != null) { builder.AddCondition(condition); } BackgroundTaskRegistration task = builder.Register(); return task; }
And executing it with
var reg = RegisterBackgroundTask("NotificationServer.RawTask", "RawNotifications", new PushNotificationTrigger(), null);
Is there something I'm missing here, my app doesn't seem to be responding to the Push Notification event. I have made sure my app is associated with the app in the store and the pushes are being sent with the correct client secret and app ID.