2
votes

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

  1. Created a Windows Runtime Component Project in the same solution as my other projects (Called NotificationServer)
  2. Renamed the class to RawTask.cs and implemented IBackgroundTask and its Run method
  3. 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);
        }
    
  4. Added code to the Run method

    BackgroundTaskDeferral _deferral = taskInstance.GetDeferral();

            RawNotification notification = (RawNotification)taskInstance.TriggerDetails;
            string content = notification.Content;
    
            // ...
            SendNotification("test");
    
            // ...
    
            _deferral.Complete();
    
  5. Updated my App's manifest to Toast Capable = YES and Lock Screen Notifications = Badge

  6. Added a Declaration for a Background Task with Supported Task Type = Push Notification and Entry Point = NotificationServer.RawTask

  7. 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.

1
Hello @Sandeep. I also stuck in the same problem. Did you find the solution on this. Please help me. Thanks!Kishor Bikram Oli

1 Answers

-1
votes

There are four types of push notifications:

  1. Tile update
  2. Badge update
  3. Toast notification
  4. Raw notification

All Windows Runtime apps can use the first three push notifications when in the foreground. Only lock screen apps can receive raw push notifications from WNS. so, you must make your app a lock screen app.

You can follow this topic to do that.