@ismcagdas
If you are not able to reproduce issue then can you try by hosting your test application on Azure server? as we are facing issue only on live server.
/=====================AppNotificationNames.cs========================/
/=====================.xml file========================/
<!-- Notification Definition -->
<text name="NewDocumentCreatedNotificationDefinition" value="On Document creation"></text>
<!-- Notification Message -->
<text name="NewDocumentCreatedNotificationMessage" value="New Document - New Document {documentName} type of {documentCategory} - {documentType} is created for {entity} with Expiration {ExpirationDate} by {createdByUser}"></text>
/=====================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<GetNotificationSettingsOutput> GetNotificationSettings() { var output = new GetNotificationSettingsOutput();
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<CreateOrEditDocumentDto> Create(CreateOrEditDocumentDto input) { var document = ObjectMapper.Map<Document>(input); await _documentRepository.InsertAsync(document); await UnitOfWorkManager.Current.SaveChangesAsync();
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;
}
}
@ismcagdas Please check this comment https://support.aspnetzero.com/QA/Questions/6390#answer-45564aa4-85ff-cb00-d268-39ebc8f5015e in same thread for code.
/*=======================documentAppService.cs==============================*/
[AbpAuthorize(AppPermissions.Pages_Documents_Edit)]
private async Task<CreateOrEditDocumentDto> Update(CreateOrEditDocumentDto input)
{
var user = await UserManager.FindByIdAsync(AbpSession.GetUserId().ToString());
var document = await _documentRepository.FirstOrDefaultAsync((int)input.Id);
var oldDocumentType = document.DocumentyTypeName;
var documentMappedEntity = ObjectMapper.Map(input, document);
await _documentRepository.UpdateAsync(documentMappedEntity);
await UnitOfWorkManager.Current.SaveChangesAsync();
if(oldDocumentType != documentMappedEntity.DocumentTypeName)
{
/*Send notification for Document type update to all subscribed users*/
_appNotifier.NotificationForDocumentTypeChangedAsync(user,notificationForDocumentDto);
}
return ObjectMapper.Map<CreateOrEditDocumentDto>(documentMappedEntity);
}
/*=================AppNotifier.cs===================*/
public async Task NotificationForDocumentTypeChangedAsync(User createdByUser, CreateOrEditDocumentDto notificationForDocumentDto)
{
try
{
/*Set Notification data for dynamic localization string and set parameters for Notification message text*/
var notificationData = new LocalizableMessageNotificationData(
new LocalizableString(
CommonMethodHelper.GetNotificationTextLocalizationString("DocumentTypeChangedNotificationMessage"),
LoadStopConsts.LocalizationSourceName
)
);
notificationData["documentName"] = notificationForNewDocumentCreatedDto.CreateOrEditDocumentDto.Name;
notificationData["documentType"] = notificationForNewDocumentCreatedDto.DocumentTypeName;
notificationData["user"] = createdByUser.FullName;
//Write logs
Logger.Info("document type changed notification Publish Notification Start: " + notificationForNewDocumentCreatedDto.CreateOrEditDocumentDto.Name);
await _notificationPublisher.PublishAsync(AppNotificationNames.DocumentTypeChanged, notificationData, null,
NotificationSeverity.Info, null, excludedUserIds: new[] { createdByUser.ToUserIdentifier() });
Logger.Info("document type changed notification Publish Notification End: " + notificationForNewDocumentCreatedDto.CreateOrEditDocumentDto.Name);
}
catch(Exception ex)
{
var errorMessage = ex.Message;
}
}
/*=====================.xml file========================*/
<text name="DocumentTypeChangedNotificationMessage" value="Document type changed to {documentType} for Document: {documentName} by {user}"></text>
/*=====================AppNotificationNames.cs========================*/
public const string DocumentTypeChanged = "App.DocumentTypeChanged";
/*=====================AppNotificationProvider.cs========================*/
/* Document create */
context.Manager.Add(
new NotificationDefinition(
AppNotificationNames.NewDocumentCreated,
displayName: L("NewDocumentCreatedNotificationDefinition"),
permissionDependency: new SimplePermissionDependency(AppPermissions.Pages_Documents_Create)
)
);
/* Document type update */
context.Manager.Add(
new NotificationDefinition(
AppNotificationNames.DocumentTypeChanged,
displayName: L("DocumentTypeChangedNotificationDefinition"),
permissionDependency: new SimplePermissionDependency(AppPermissions.Pages_Documents_Edit)
)
);
For eg. I have created one document "MyLicenseDoc" with "DocType1". Now in edit mode, I have changed it to "DocType2". So in this case, all subscribed users will get notification.
==============================AppNotificationProvider.cs==============================
/* Document create */
context.Manager.Add(
new NotificationDefinition(
AppNotificationNames.NewDocumentCreated,
displayName: L("NewDocumentCreatedNotificationDefinition"),
permissionDependency: new SimplePermissionDependency(AppPermissions.Pages_Documents_Create)
)
);
/* Document type update */
context.Manager.Add(
new NotificationDefinition(
AppNotificationNames.DocumentTypeChanged,
displayName: L("DocumentTypeChangedNotificationDefinition"),
permissionDependency: new SimplePermissionDependency(AppPermissions.Pages_Documents_Edit)
)
);
==============================NotificationAppService.cs==============================
public async Task<GetNotificationSettingsOutput> GetNotificationSettings()
{
var output = new GetNotificationSettingsOutput();
output.ReceiveNotifications = await SettingManager.GetSettingValueAsync<bool>(NotificationSettingNames.ReceiveNotifications);
/*Get general notifications, not entity related notifications*/
//var notificationDefinitions = (await _notificationDefinitionManager.GetAllAvailableAsync(AbpSession.ToUserIdentifier())).Where(nd => nd.EntityType == null);
/*Get all notifications (general+entity related)*/
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);
}
}
}
==============================AppNotifier.cs==============================
/*We have called this function on Document save method:*/
await _notificationPublisher.PublishAsync(AppNotificationNames.NewDocumentCreated, notificationData, null,
NotificationSeverity.Info, null, excludedUserIds: new[] { createdByUser.ToUserIdentifier() });
/*We have called this function on change of Document type:*/
await _notificationPublisher.PublishAsync(AppNotificationNames.DocumentTypeChanged, notificationData, null,
NotificationSeverity.Info, null, excludedUserIds: new[] { createdByUser.ToUserIdentifier() });
We have passed required dynamic fields values in notification data object.
In database, subscriptions and tenant notifications are populating fine still it is creating issues in Notifications.
We are facing this issue only in our server. In Local, everything is working fine. We have hosted our application on Azure. Is there any setting that we might need to do for Notifications in Azure?
Hi @ismcagdas, We have added logs and there is only one log for "NewEntityCreatedAsync" method. We have also added logs before and after "PublishAsync" method but only one log is created.
Please let us know how can we track this issue.
We have debugged code found that and NewEntityCreatedAsync is called only one time. Issue is, this error is not occuring always. So we are not able to track this. Still we will add logs for these methods and try once.
Thank you