Base solution for your next web application
Open Closed

variable insertions in notification #9198


User avatar
0
BobIngham created

Given the following notification when a device is added to my app:

                    var notificationData = new LocalizableMessageNotificationData(
                        new LocalizableString(
                            "NewDeviceRegisteredNotificationDefinition",
                            NuagecareConsts.LocalizationSourceName
                            )
                        );
                    _notificationPublisher.Publish(AppNotificationNames.NewDeviceRegistered, notificationData, severity: NotificationSeverity.Info);

How do I inject variables into the language string to show the manufacturer, model of the device and the date the device was added?


6 Answer(s)
  • User Avatar
    0
    ismcagdas created
    Support Team

    Hi @bobingham

    You can use string.Format after retrieving the value of statement below;

    new LocalizableString(
    	"NewDeviceRegisteredNotificationDefinition",
    	NuagecareConsts.LocalizationSourceName
    	)
    )
    
  • User Avatar
    0
    BobIngham created

    Sorry, @ismcagdas - I can't see how to do that. Where do I put the string.Format to inject a device model and manufacturer?

  • User Avatar
    0
    ismcagdas created
    Support Team

    Hi @bobingham

    Could you share the final message you are trying to show the end user ?

  • User Avatar
    0
    BobIngham created

    Hi @ismcagdas, sure, thanks for helping. The user registers a device by entering the tenancyName in an app. I have a DeviceRegistrationManager.RegisterDevice method which is passed details about the new device such as model, manufacturer, serial number etc. So, the key code is here:

    var model = ObjectMapper.Map<NcDevice>(input);
    model.TenantId = tenant.Id;
    deviceId = await _deviceRepository.InsertAndGetIdAsync(model);
    
    //Notifications
    var notificationData = new LocalizableMessageNotificationData(
        new LocalizableString(
            "NewDeviceRegisteredNotificationDefinition",
            NuagecareConsts.LocalizationSourceName
            )
        );
    _notificationPublisher.Publish(AppNotificationNames.NewDeviceRegistered, notificationData, severity: NotificationSeverity.Info);
    

    The localisation string for NewDeviceRegisteredNotificationDefinition is:

    <text name="NewDeviceRegisteredNotificationDefinition">New device registered, please authorise.</text>
    

    What I woud like to say in the notification is "A new device has been registered; device id {Id}, ({Samsung}, {A790F}, {aaappp0001117777333}) please authorise".

  • User Avatar
    0
    ismcagdas created
    Support Team

    Hi,

    You are right, it seems not possible with LocalizableMessageNotificationData. You can try to use MessageNotificationData.cs.

    Define your localization like this;

    <text name="NewDeviceRegisteredNotificationDefinition">
    A new device has been registered; device id {0}, ({1}, {2}, {3}) please authorise
    </text>
    

    and then;

    var model = ObjectMapper.Map<NcDevice>(input);
    model.TenantId = tenant.Id;
    deviceId = await _deviceRepository.InsertAndGetIdAsync(model);
    
    var message = string.Format(_localizationManager.GetString(NuagecareConsts.LocalizationSourceName,  "NewDeviceRegisteredNotificationDefinition"), deviceId, "Samsung", "A790F", "aaappp0001117777333");
    
    //Notifications
    var notificationData = new MessageNotificationData(message);
    _notificationPublisher.Publish(AppNotificationNames.NewDeviceRegistered, notificationData, severity: NotificationSeverity.Info);
    
  • User Avatar
    0
    BobIngham created

    Perfect, thanks so much for your help. You learn something new about Zero every day!