Subscribe Notification and Publish Notification for Insert operation in any entity #6390
AbpNotificationSubscriptions table is having "EntityId" and "EntityTypeName" columns. We have implemented Notification when any user add data in our custom table X. We have also set Entity Type in AppNotificationProvider.cs file as "typeof(X)". Our notifications are being displayed on Notification Settings page.
In our case, when user subscribes for notification, EntityId and EntityType saved as NULL in database.
When notification is published, that time also EntityId and EntityType saved as NULL in database.
Following are our questions for Insert operation notification:
- For our case, these types of Notification can be called General or Entity based?
- For insert operation, EntityTypeName should be saved in "AbpNotificationSubsciptions" table or not?
- For insert operation, EntityTypeName and EntityId should be saved in "AbpTenantNotifications" table or not?
19 Answer(s)
-
0
We have implemented Notification when any user add data in our custom table X. We have also set Entity Type in AppNotificationProvider.cs file as "typeof(X)
Hi, if possible, please share some code about the implementation of the scenario above
- For our case, these types of Notification can be called General or Entity based?
This depends if the nofitication is created with entity Id and type. if they do have entity Id and type then they are considered as entity base.
- For insert operation, EntityTypeName should be saved in "AbpNotificationSubsciptions" table or not?
it depends on your definition of notification. e.g user creation notification should have entity id and type, and weekly summary notification most likely doesn't need to have entity info
- For insert operation, EntityTypeName and EntityId should be saved in "AbpTenantNotifications" table or not?
Yes if you want the published user/tenant notification to have the context of a specific entity
-
0
Thank you for quick response. Now I understand all scenarios. I have one more query regarding Publish Notification.
-It is working fine in local and server too. But in server sometimes it is sending notifications twice. 1st is with IST(local system time from where we are testing system) and 2nd is with UTC time(server time). In database also, AbpTenantNotifications table is having two rows with different CreationTime values. Can you help me in this issue?
Code is working fine and we have not called Publish notification method twice still it is calling two times sometimes.
Please let us know your response.
Thanks.
-
0
We have added seperate Notification definitions for each Entity and created generalized Publish notification methods for all including below methods. -"CommonMethodHelper.GetNotificationNameFromEntity" will get Notification name as per given entity. -"CommonMethodHelper.GetNotificationTextLocalizationString" will get Notification text message with provided dynamic parameters -"GetEntityTypeByEntityName" will get Entity Type(Document) from given Entity name(string)
-
0
@razkhan78
I have seen that code but we are not able to reproduce only using this part of the code. Because of that, I wanted to see the entire app. Otherwise, it is hard for us to help you because we are not able to reproduce it. Event you can't reproduce it on your dev environment which makes me think that the problem is related to something else than AspNet Zero.
-
0
/=====================AppNotificationNames.cs========================/
/=====================.xml file========================/
/=====================AppNotificationProvider.cs========================/
/* Document create */ context.Manager.Add( new NotificationDefinition( AppNotificationNames.NewDocumentCreated, displayName: L("NewDocumentCreatedNotificationDefinition"), permissionDependency: new SimplePermissionDependency(AppPermissions.Pages_Documents_Create) ) );
/==============================NotificationAppService.cs==============================/
public async Task
output.ReceiveNotifications = await SettingManager.GetSettingValueAsync<bool>(NotificationSettingNames.ReceiveNotifications); var notificationDefinitions = (await _notificationDefinitionManager.GetAllAvailableAsync(AbpSession.ToUserIdentifier())); output.Notifications = ObjectMapper.Map<List<NotificationSubscriptionWithDisplayNameDto>>(notificationDefinitions); var subscribedNotifications = (await _notificationSubscriptionManager .GetSubscribedNotificationsAsync(AbpSession.ToUserIdentifier())) .Select(ns => ns.NotificationName) .ToList(); output.Notifications.ForEach(n => n.IsSubscribed = subscribedNotifications.Contains(n.Name)); return output;
}
public async Task UpdateNotificationSettings(UpdateNotificationSettingsInput input) { await SettingManager.ChangeSettingForUserAsync(AbpSession.ToUserIdentifier(), NotificationSettingNames.ReceiveNotifications, input.ReceiveNotifications.ToString());
foreach (var notification in input.Notifications) { if (notification.IsSubscribed) { await _notificationSubscriptionManager.SubscribeAsync(AbpSession.ToUserIdentifier(), notification.Name); } else { await _notificationSubscriptionManager.UnsubscribeAsync(AbpSession.ToUserIdentifier(), notification.Name); } }
}
/==============================documentAppservice.cs==============================/
[AbpAuthorize(AppPermissions.Pages_Documents_Create)] private async Task
var createOrEditDocumentDto = ObjectMapper.Map<CreateOrEditDocumentDto>(document); /*Send Document Created Notification to all subscribed users*/ await _appNotifier.NewDocumentCreatedAsync(await GetCurrentUserAsync(), GetDtoForDocumentCreatedNotification(EntityTypeEnums.Document,createOrEditDocumentDto, documentTypedata.Name));
}
/==============================AppNotifier.cs==============================/
/Send Notification for Document Create event/ public async Task NewDocumentCreatedAsync(User createdByUser, NotificationForNewDocumentCreatedDto notificationForNewDocumentCreatedDto) { try { /Set Notification data for dynamic localization string and set parameters for Notification message text/ var notificationData = new LocalizableMessageNotificationData( new LocalizableString( "NewDocumentCreatedNotificationMessage", LoadStopConsts.LocalizationSourceName ) );
notificationData["documentName"] = notificationForNewDocumentCreatedDto.CreateOrEditDocumentDto.Name; notificationData["createdByUser"] = createdByUser.FullName; //Write logs Logger.Info("New document Publish Notification Start: " + notificationForNewDocumentCreatedDto.CreateOrEditDocumentDto.Name); await _notificationPublisher.PublishAsync(AppNotificationNames.NewDocumentCreated, notificationData, null, NotificationSeverity.Info, null, excludedUserIds: new[] { createdByUser.ToUserIdentifier() }); Logger.Info("New document Publish Notification End: " + notificationForNewDocumentCreatedDto.CreateOrEditDocumentDto.Name); } catch (Exception ex) { var errorMessage = ex.Message; }
}