NotificationService OnNotice

Hi!

Is there any way for me to raise an event when a notification is displayed or when the Notify function is called by the NotificationService? I know that it is not possible with the Radzen IDE but in VS?

Thanks!

Hi @Gottegubben,

This isn't supported. Only the DialogService has similar API - OnOpen / OnClose events on the service itself.

1 Like

Is this something that maybe would be added in the future?

To be honest I am not sure. We added the OnOpen / OnClose events mostly because there was no way to get the result of a dialog initially. Why do you need such an event? Cant you invoke your code after calling the Notify method?

The big reason is to have a custom logger. So everytime i called on OnNotice and the sensitivity level is Error, then i will make a logpost in my logsystem aswell. Its an easy way to find all user interaction errors.

And yes I can invoke my own code after each one but then each time i create a new page or a new place the Notify Method is called i need to remember to put my own code there aswell. therefor i want to have a OnNotice where i always can invoke my code each time a notice is shown automaticly.

Maybe you can use the Messages property of the NotificationService and listen to its CollectionChange event.

Good call. I will investigate the option and get back to you if i had any success. Thanks alot!

In Startup.cs:

services.AddScoped<EventLogService>();

In EventLogService.cs:

 public class EventLogService
    {
        public NotificationService NotificationService { get; set; }
        public EventLogService(NotificationService NotificationService)
        {
            this.NotificationService = NotificationService;
            this.NotificationService.Messages.CollectionChanged += OnNotify;
        }   

        protected void OnNotify(object Sender, NotifyCollectionChangedEventArgs e)
        {
            // Do custom logging here
        }
    }

Is this a good approach to the problem? The OnNotify didnt seem to trigger when a Notifcation was displayed..

What happens when you debug the application? The Notify method adds to the Messages collection which is supposed to trigger the CollectionChanged event (at least that's what Microsoft's documentation says: Occurs when an item is added, removed, changed, moved, or the entire list is refreshed.)

I added a breakpoint on the start of my OnNotify function but it is never called even if i see a notification pop up on the page.

The Notification itself relies on that same event. Not sure why it is not working for you.

Make sure the service is registered and its constructor is called. If you added services.AddScoped<EventLogService>(); to Startup.cs Radzen has probably overwritten it - you need to add it in a partial Startup class as shown here.

I have added it to my partial Startup.cs. Maybe it is that you can only subscribe to an event one time and the second time you do it it wont register.
So this code subscirbes to the event

 protected override void OnInitialized()
    {
        Service.Messages.CollectionChanged += Update;
    }

And then when i try to subscirbe it dosnt register?

public EventLogService(NotificationService NotificationService)
        {
            this.NotificationService = NotificationService;
            this.NotificationService.Messages.CollectionChanged += OnNotify;
        }

Some more information about this small journey, It seams that I need to include my EventLogService on each page for this to work. I didn´t know that was a must before but it is. So now my OnNotify is triggerd when the Notify function in NotificationService is called.

That explains it. You need to inject your service at least once otherwise it won't get instantiated. Maybe you won't need to inject it in all pages - just in the start page of your application.

1 Like

Thanks alot @korchev for the help!