[UWP] PushTrigger background task not invoking

Sarath Vanga 6 Reputation points
2022-06-27T12:36:56.63+00:00

I am building a Unity game application targeted for UWP. My aim to add WNS push notifications to the project. I am using raw push notifications.

For raw push notifications to work I followed this link https://learn.microsoft.com/en-us/windows/apps/design/shell/tiles-and-notifications/raw-notification-overview.

As specified in the link above, I have added one Background task for PushNotificationTrigger and also added this task in the package manifest file. I am receiving the push notification via 'PushNotificationReceived' event when the app is in foreground, but when the app is NOT live, the background task never got invoked. When I check the 'Event Viewer' application for details, I found this error "The background task with entry point UWPNotifications.PushBackgroundTask and name uwpnotifications.pushbackgroundtask failed to activate with error code 0x80270254."
The background task code looks like this:

     public sealed class PushBackgroundTask : IBackgroundTask  
    {  
        private const string TASK_NAME = "notif_push_task";  
  
        public static void Register()  
        {  
            if (IsBackgroundTaskRegistered(TASK_NAME))  
            {  
                Console.WriteLine("Push background task already registered");  
                return;  
            }  
  
            BackgroundTaskBuilder builder = new BackgroundTaskBuilder();   
  
            builder.TaskEntryPoint = typeof(PushBackgroundTask).FullName;  
            builder.Name = TASK_NAME;  
            builder.SetTrigger(new PushNotificationTrigger());  
  
            try  
            {  
                BackgroundTaskRegistration task = builder.Register();  
            } catch(Exception ex)  
            {  
                Console.WriteLine("Push task registration failed {0}", ex.Message);  
            }  
        }  
  
        public void Run(IBackgroundTaskInstance taskInstance)  
        {  
            RawNotification notification = (RawNotification)taskInstance.TriggerDetails;  
            Console.WriteLine("Raw WNS content {0}", notification.Content);  
        }  

        private static bool IsBackgroundTaskRegistered(string name)  
        {  
            foreach (var task in BackgroundTaskRegistration.AllTasks)  
            {  
                if (task.Value.Name == name)  
                {  
                    return true;  
                }  
            }  
            return false;  
        }  
}  

What is the issue here? I have created the application package from "Publish --> Create app packages --> Microsoft Store app as AppName" and installed this on my windows10 pc for testing.

I have a few other doubts regarding the WNS raw notifications and background tasks.

  1. Is it mandatory that the background task code should be present as a separate library(.winmd) ?
  2. Are there any restrictions on background task name? Can we have a custom name to it ?
  3. Is there a way to test WNS push notifications with out the need to generate the store package ?

I appreciate any help on this.

Thanks,
Sarath.

Not Monitored
Not Monitored
Tag not monitored by Microsoft.
35,882 questions
{count} vote

1 answer

Sort by: Most helpful
  1. Enrico Rossini 176 Reputation points
    2024-02-25T12:34:16.0966667+00:00

    By any chance, did you fix this issue? How? Thank you in advance, Enrico

    0 comments No comments