0

I am developing a Windows 10 UWP application and I have a problem with raw notification sending and handling.

I am sending the notification from a server written in php, and when app is open I can receive the sent raw notification. Below is the template of my notification:

           <toast launch='args'>
                <visual>
                    <binding template = 'ToastGeneric'>
                         <text> başlık </text>
                         <text> Açıklama </text>
                        </binding>
                    </visual>
            </toast> 

I sent the template above as a raw notification.

Also, I am using the method below as backgroundtask

public void Run(IBackgroundTaskInstance taskInstance)
    {
        // Get the background task details
        ApplicationDataContainer settings = ApplicationData.Current.LocalSettings;
        string taskName = taskInstance.Task.Name;

        Debug.WriteLine("Background " + taskName + " starting...");

        // Store the content received from the notification so it can be retrieved from the UI.
        RawNotification notification = (RawNotification)taskInstance.TriggerDetails;
        settings.Values[taskName] = notification.Content;
        Windows.Data.Xml.Dom.XmlDocument doc = new XmlDocument();

        var toast = new ToastNotification(doc);
        var center = ToastNotificationManager.CreateToastNotifier();
        center.Show(toast);


        Debug.WriteLine("Background " + taskName + " completed!");
    }

I set the task entry point in appmanifest, I could not test it with debugger.

I register the background task with the code below:

 private void RegisterBackgroundTask()
    {
        BackgroundTaskBuilder taskBuilder = new BackgroundTaskBuilder();
        PushNotificationTrigger trigger = new PushNotificationTrigger();
        taskBuilder.SetTrigger(trigger);

        // Background tasks must live in separate DLL, and be included in the package manifest
        // Also, make sure that your main application project includes a reference to this DLL
        taskBuilder.TaskEntryPoint = SAMPLE_TASK_ENTRY_POINT;
        taskBuilder.Name = SAMPLE_TASK_NAME;

        try
        {
            BackgroundTaskRegistration task = taskBuilder.Register();
            task.Completed += BackgroundTaskCompleted;
        }
        catch (Exception ex)
        {
            // rootPage.NotifyUser("Registration error: " + ex.Message, NotifyType.ErrorMessage);
            UnregisterBackgroundTask();
        }
    }

Finally, my pushreceived event:

private async void OnPushNotificationReceived(PushNotificationChannel sender, PushNotificationReceivedEventArgs e)
    {
        if (e.NotificationType == PushNotificationType.Raw)
        {
            e.Cancel = false;

            Windows.Data.Xml.Dom.XmlDocument doc = new Windows.Data.Xml.Dom.XmlDocument();

            var t = @" <toast launch='args'>
                <visual>
                    <binding template = 'ToastGeneric'>
                         <text> başlık </text>
                         <text> Açıklama </text>
                        </binding>
                    </visual>
            </toast> ";

            doc.LoadXml(t);

            var toast = new ToastNotification(doc);
            var center = ToastNotificationManager.CreateToastNotifier();
            center.Show(toast);
        }
    }

the inside of the method is only for testing.

Now, I want to ask that what is wrong or missin in my structure? Any suggession, example or help is appreciated.

Thanks in advance.

fatih
  • 73
  • 1
  • 13

1 Answers1

1

Did you check in manifest Declarations that you have checked "Push notifications"?

Also you need at top the RegisterBackgroundTask method call

await BackgroundExecutionManager.RequestAccessAsync();

Here is documentation https://msdn.microsoft.com/pl-pl/library/hh700494.aspx where is important sentence "Requests that the app be permitted to run background tasks."

At the end check entry point and name of your background task

matihuf
  • 352
  • 1
  • 11
  • Hi @matihuf I'm trying to develop a test UWP and receive Background Raw Notifications even when is closed, this is our first App and we need a very basic example to achieve this. By now we have an Azure Account and the WNS is set, also have a project inside my solution which name is backgroundtask.cs and the objetive is to receive the notification and can't find a basic startup for this, can you help us with this? Thanks – yehanny Sep 28 '17 at 23:11
  • I don't have any sample code right now, but please read this https://learn.microsoft.com/en-us/windows/uwp/controls-and-patterns/tiles-and-notifications-raw-notification-overview you are interested in the section "Background tasks triggered by raw notifications" – matihuf Sep 30 '17 at 17:23
  • Thanks for the help buddy I'll take a look and reply back the results... :) – yehanny Oct 02 '17 at 14:52