简体   繁体   中英

Asp.Net Boilerplate Entity notifications

I have Notifications working in a .NET Core web application. However I'm trying to understand the Entity Notification operation. As I read the documentation and tried few coding some scenarios it doesn't seem to be working as I thought it might. My application has a Table for "Parcels". I created a Subscription based on one of the Parcels listed in the table

await _notificationSubscriptionManager
    .SubscribeAsync(new UserIdentifier(tenantId, userId), "ParcelTransfer", new EntityIdentifier(typeof(Parcel), 3041));

The Id Value I used is the Id value for a Parcel in the Parcel database table. The code then then fires a publish call but uses a different Parcel Id value

await _notificationPublisher.PublishAsync(notificationName, data: new MessageNotificationData(message), 
                    new EntityIdentifier(typeof(Parcel), 3042), userIds: userIds);

However, I still receive a Notification. I thought because I issued the Publish call using a different Parcel Identity Id value, a Notification would NOT be sent. I'm thinking I don't understand how the process works.

Figured it out. I didn't need to specify an EntityIdentifier object in the PublishAsync() method. The fact the User with a Tenant and UserId was specified in the Subscription SubscribeAsync() method was all I needed.

When the publish command was issued, the code just needed to look at the Subscriptions based on the subscription name.

I also had to modify my EmailRealTimeNotifier class for both methods SendNotifications() and SendNotificationsAsync() to

 [UnitOfWork]
    public void SendNotifications(UserNotification[] userNotifications)
    {
     
        foreach (var userNotification in userNotifications)
        {
            if (userNotification.Notification.Data is MessageNotificationData data)
            {

                using (_unitOfWorkManager.Current.SetTenantId(userNotification.TenantId))
                {
                    var user =  _userManager.GetUserById(userNotification.UserId);

                    _emailSender.Send(
                        to: user.EmailAddress,
                        subject: "You have a new notification!",
                        body: data.Message,
                        isBodyHtml: true
                    );
                }

            }
        }
    }

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM