NotificationService

  • 7 minutes to read

The NotificationService allows you to display popup notifications in your applications. You can use the native Windows 8/10 look & feel or create a custom template.

Create Windows 8/10 Native Notification

NotificationService Windows 10 Style

To display a Windows 8/10 native notification, specify the following NotificationService properties:

  • Create the ApplicationActivator instance and connect it to the service.

  • Specify the ApplicationId.

  • Set the UseWin8NotificationsIfAvailable to true.

  • Set the CreateApplicationShortcut property to true or create an application shortcut based on the custom logic:

    DevExpress.Data.ShellHelper.TryCreateShortcut([application id], [application name], [application icon], [application activator]);
    

Place the NotificationService into the Interaction.Behaviors collection and define your View Model's NotificationService as a POCO object.

<UserControl x:Class="DXSampleNotificationSevice.View.MainView"
    ... 
    xmlns:dxmvvm="http://schemas.devexpress.com/winfx/2008/xaml/mvvm" 
    xmlns:ViewModel="clr-namespace:DXSampleNotificationSevice.ViewModel"  
    DataContext="{dxmvvm:ViewModelSource Type={x:Type ViewModel:MainViewModel}}">
    ...
    <dxmvvm:Interaction.Behaviors>
        <dxmvvm:NotificationService x:Name="notificationService"
            UseWin8NotificationsIfAvailable="True"
            CreateApplicationShortcut="True"
            ApplicationActivator="{x:Type local:MyNotificationActivator}">
            <dxmvvm:NotificationService.ApplicationId>
                <Binding Source="{x:Static local:MainWindow.ApplicationID}" />
            </dxmvvm:NotificationService.ApplicationId>
        </dxmvvm:NotificationService>
    </dxmvvm:Interaction.Behaviors>
    ...
</UserControl>
NOTE

When View Model is inherited from the ViewModelBase, use the Services in ViewModelBase descendants topic approach.

Create a Custom Notification

The NotificationService uses its own notification mechanism to create a custom notification.

NotificationService - Custom Notifications

Use the NotificationService.CustomNotificationTemplate property to define a custom notification layout.

<UserControl x:Class="DXSampleNotificationSevice.View.MainView"
    ...
    xmlns:dxmvvm="http://schemas.devexpress.com/winfx/2008/xaml/mvvm" 
    xmlns:ViewModel="clr-namespace:DXSampleNotificationSevice.ViewModel"  
    DataContext="{dxmvvm:ViewModelSource Type={x:Type ViewModel:MainViewModel}}">
    <UserControl.Resources>
        <DataTemplate x:Key="CustomNotificationTemplate">
            <Border Background="DarkBlue" BorderThickness="1" BorderBrush="Black">
                <StackPanel Orientation="Vertical" HorizontalAlignment="Stretch" VerticalAlignment="Stretch">
                    <TextBlock HorizontalAlignment="Left" Text="{Binding Caption}" Foreground="White" FontSize="20" FontWeight="Bold" Margin="5" FontFamily="SegoeUI"/>
                    <TextBlock HorizontalAlignment="Center" Text="{Binding Content}" Foreground="LightGray" FontSize="16" Margin="3" FontFamily="SegoeUI"/>
                    <TextBlock HorizontalAlignment="Right" Text="{Binding Time}" Foreground="Gray" FontSize="14" Margin="3" FontFamily="SegoeUI"/>
                </StackPanel>
            </Border>
        </DataTemplate>
    </UserControl.Resources>
    <dxmvvm:Interaction.Behaviors>
        <dxmvvm:NotificationService x:Name="ServiceWithCustomNotifications" CustomNotificationTemplate="{StaticResource CustomNotificationTemplate}"/>
    </dxmvvm:Interaction.Behaviors>
    ...
</UserControl>

To create a custom notification, use the NotificationService.CreateCustomNotification method. This method requires a notification's View Model as a parameter.

[POCOViewModel]
public class MainViewModel {
    [ServiceProperty(Key = "ServiceWithCustomNotifications")]
    protected virtual INotificationService CustomNotificationService { get { return null; } }
    ...
    public void ShowCustomNotification() {
        CustomNotificationViewModel vm = ViewModelSource.Create(() => new CustomNotificationViewModel());
        vm.Caption = "DevAV Custom Notification";
        vm.Content= "3 cards are due soon...";
        vm.Time = String.Format("Time: {0}", DateTime.Now);
        INotification notification = CustomNotificationService.CreateCustomNotification(vm);
        notification.ShowAsync();
    }
    ...
}

Use the INotification.ShowAsync method to display the created notification. This method returns a NotificationResult enumeration value depending on how the notification is displayed and which notification element an end user clicks. The ShowAsync method returns the following values depending on the notification result:

  • Activated, if a user clicks the notification.
  • UserCanceled, if a user clicks the notification's close button.
  • TimedOut, if a user does not click the notification during the time specified in the NotificationService.CustomNotificationDuration or NotificationService.PredefinedNotificationDuration property.
  • ApplicationHidden, if you use the INotification.Hide method when the notification is displayed.
  • Dropped, if the system's notification queue is full and the notification cannot be displayed.

The code snippet below illustrates how to process the ShowAsync method's result.

public void ShowCustomNotification() {
    CustomNotificationViewModel vm = ViewModelSource.Create(() => new CustomNotificationViewModel());
    vm.Caption = "Custom Notification";
    vm.Content= "3 cards are due soon...";
    vm.Time = String.Format("Time: {0}", DateTime.Now);
    INotification notification = CustomNotificationService.CreateCustomNotification(vm);
    notification.ShowAsync().ContinueWith(result => OnContinueWith(result));
}
private void OnContinueWith(Task<NotificationResult> res) {
    ...
}

NotificationService Sample Project

Imports DevExpress.Mvvm.DataAnnotations

Namespace DXSampleNotificationSevice.ViewModel
    <POCOViewModel> _
    Public Class CustomNotificationViewModel
        Public Overridable Property Caption() As String
        Public Overridable Property Content() As String
    End Class
End Namespace