How receives push notification in Windows with MAUI

Enrico Rossini 181 Reputation points
2024-02-23T00:27:46.38+00:00

I'm creating an application using .NET8 MAUI. The application registers the device in an Azure Notification Hub. From here, I can send push notifications to the different devices.

The problem I have with Windows is that the message arrives on the Windows application, only if the application is open. I like to understand if it is possible to register the application to wait for messages in the background.

I saw in the Package.appxmanifest for Windows those settings

enter image description here

but I can't find a way to use it. How do I have to configure the application in order to receive push notifications also when the application is closed?

Azure Notification Hubs
Azure Notification Hubs
An Azure service that is used to send push notifications to all major platforms from the cloud or on-premises environments.
295 questions
.NET MAUI
.NET MAUI
A Microsoft open-source framework for building native device applications spanning mobile, tablet, and desktop.
3,149 questions
0 comments No comments
{count} votes

1 answer

Sort by: Most helpful
  1. brtrach-MSFT 15,786 Reputation points Microsoft Employee
    2024-02-24T23:43:25.5566667+00:00

    @Enrico Rossini Please note that this is outside the scope of Azure Notifications Hubs, which is what I support. Please go easy on me as I would like to try and assist you. To receive push notifications in the background on Windows with .NET MAUI, you need to register a background task in your application. This background task will be triggered when a push notification is received, even if the application is closed. To register a background task, you need to add the following code to your App.xaml.cs file:

    private async void RegisterBackgroundTask()
    {
        var builder = new BackgroundTaskBuilder();
        builder.Name = "PushNotificationBackgroundTask";
        builder.SetTrigger(new PushNotificationTrigger());
        builder.Register();
    }
    

    This code creates a new BackgroundTaskBuilder object, sets the name of the background task to "PushNotificationBackgroundTask", and sets the trigger to a PushNotificationTrigger. Finally, it registers the background task. You can call this method from the OnLaunched method of your App.xaml.cs file to register the background task when the application is launched:

    protected override void OnLaunched(LaunchActivatedEventArgs args)
    {
        RegisterBackgroundTask();
    
        // Rest of the OnLaunched method
    }
    

    You also need to add the uap:Capability element to your Package.appxmanifest file, as you mentioned. This element should be added as a child of the Package element, like this:

    <Package
        ...
        xmlns:uap="http://schemas.microsoft.com/appx/manifest/uap/windows10"
        IgnorableNamespaces="uap">
    
        <uap:Capability Name="internetClient" />
        <uap:Capability Name="internetClientServer" />
        <uap:Capability Name="privateNetworkClientServer" />
        <uap:Capability Name="userNotificationListener" />
        <uap:Capability Name="removableStorage" />
        <uap:Capability Name="backgroundMediaPlayback" />
        <uap:Capability Name="backgroundVoIP" />
        <uap:Capability Name="backgroundTasks" />
        <uap:Capability Name="pushNotifications" />
        
        ...
    </Package>
    
    

    Make sure to add the pushNotifications capability to enable push notifications.