NotificationService
- 4 minutes to read
The NotificationService allows you to display popup notifications in your applications. You can display native Windows 10/11 notifications or create custom ones.
Create a Native Windows 10/11 Notification
To display a native Windows 10/11 notification, do the following:
Create an application shortcut. Do one of the following:
- Set the NotificationService.CreateApplicationShortcut property to true.
Create an application shortcut and specify its application id, name, icon, and activator:
using System.Windows; using DevExpress.Data; // ... public partial class MainWindow : Window { public MainWindow() { RegisterApplication(); InitializeComponent(); } void RegisterApplication() { if(!ShellHelper.IsApplicationShortcutExist(ApplicationName)) { ShellHelper.RegisterComServer(typeof(CustomNotificationActivator)); ShellHelper.TryCreateShortcut(ApplicationID, ApplicationName, null, typeof(CustomNotificationActivator)); } } // ... }
Create an ApplicationActivator instance and register a COM-Server.
[Guid("5A430E6E-2E66-4A46-BBFA-A5CC432673A0"), ComVisible(true)] public class CustomNotificationActivator : ToastNotificationActivator { public override void OnActivate(string arguments, Dictionary<string, string> data) { Application.Current.Dispatcher.Invoke(() => { MainWindow.SendActivatorMessage(); }); } }
Add the NotificationService to the Interaction.Behaviors collection and specify the ApplicationId. Set the UseWin8NotificationsIfAvailable property to true.
<dxmvvm:Interaction.Behaviors> <dxmvvm:NotificationService PredefinedNotificationTemplate="LongText" UseWin8NotificationsIfAvailable="True" ApplicationActivator="{x:Type local:CustomNotificationActivator}" CreateApplicationShortcut="False" ApplicationName="{x:Static local:MainWindow.ApplicationName}"> <dxmvvm:NotificationService.ApplicationId> <Binding Source="{x:Static local:MainWindow.ApplicationID}" /> </dxmvvm:NotificationService.ApplicationId> </dxmvvm:NotificationService> </dxmvvm:Interaction.Behaviors>
When multiple native Windows notifications are displayed simultaneously, the optional Id parameter of the NotificationService.CreatePredefinedNotification method enables the service to identify the notification with which the user interacts. NotificationActivator passes the Id value to the arguments parameter of the OnActivate method.
Example
Create a Custom Notification
You can use the NotificationService to create a custom notification that does not depend on the Windows 10/11 notification API.
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="White" BorderThickness="1" BorderBrush="Black">
<StackPanel Orientation="Vertical" HorizontalAlignment="Stretch" VerticalAlignment="Stretch">
<TextBlock HorizontalAlignment="Left" Text="{Binding Caption}" Foreground="Blue" FontSize="20" FontWeight="Bold" Margin="5"/>
<TextBlock HorizontalAlignment="Center" Text="{Binding Content}" Foreground="Black" FontSize="16" Margin="3"/>
</StackPanel>
</Border>
</DataTemplate>
</UserControl.Resources>
<dxmvvm:Interaction.Behaviors>
<dxmvvm:NotificationService CustomNotificationTemplate="{StaticResource CustomNotificationTemplate}" CustomNotificationPosition="BottomRight" />
</dxmvvm:Interaction.Behaviors>
<Grid>
<Button Content="Show Custom Notification" Command="{Binding ShowCustomNotificationCommand}" VerticalAlignment="Center" Height="30"/>
</Grid>
</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 {
protected virtual INotificationService CustomNotificationService { get { return null; } }
public async void ShowCustomNotification() {
CustomNotificationViewModel vm = ViewModelSource.Create(() => new CustomNotificationViewModel());
vm.Caption = "Custom Notification";
vm.Content = String.Format("Time: {0}", DateTime.Now);
INotification notification = CustomNotificationService.CreateCustomNotification(vm);
NotificationResult result = await notification.ShowAsync();
ProcessNotificationResult(result);
}
void ProcessNotificationResult(NotificationResult result) {
...
}
...
}
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 the user clicks. The ShowAsync method returns the following values depending on the notification result:
- Activated, if the user clicks the notification.
- UserCanceled, if the user clicks the notification’s close button.
- TimedOut, if the user does not click the notification within the time span specified in the NotificationService.CustomNotificationDuration or NotificationService.PredefinedNotificationDuration property.
- ApplicationHidden, if the INotification.Hide method is called while the notification is displayed.
- Dropped, if the system’s notification queue is full and the notification cannot be displayed.