Localizing Azure Push Notification in Server end
Suppose you mobile application supports localization. And you need to send localized push notifications for your mobile app users. In that case now Azure push notification has templates which supports localization.
See the following link which gives good guidance on it.
Use Notification Hubs to send localized breaking news
In this post I will show how we can use resource files and localize some messaged to be sent.
1. First Configure the Resource file as you want with the messages.
2. Configure the supported languages for the notification.
- public const string LangzhSG = "zh-sg";
- public const string LangenUS = "en-us";
- public const string LangenGB = "en-gb";
- public const string LangitIT = "it-it";
- public const string LangsiLK = "si-lk";
- public static List<string> SupportedLanguages
- {
- get { return new List<string> { LangzhSG, LangenUS, LangenGB, LangitIT, LangsiLK }; }
- }
3. Localize the messages as your need. In here based on work order status message to be sent will be varied.
- public static Dictionary<string, string> GetMessageTranslations(WorkorderUpdateNotification message,
- List<string> supportedLanguages)
- {
- string messageText = string.Empty;
- Dictionary<string, string> messageList = new Dictionary<string, string>();
- if (message.Message.ToLower().Equals("failed"))
- {
- foreach (var supportedLanguage in supportedLanguages)
- {
- Resources.terms.Culture = new CultureInfo(supportedLanguage);
- messageText =
- string.Format(Resources.terms.webapi_helpers_pushnotification_messages_FailedToUpdateWO1,
- message.WorkOrderNumber);
- messageList.Add(supportedLanguage, messageText);
- }
- }
- return messageList;
- }
- }
4. Create the class which has the properties which notification holds all required data.
- public class WorkOrderUpdatePushNotification
- {
- public string WorkOrderNumber { get; set; }
- public bool IsSuccessful { get; set; }
- public string From { get; set; }
- public string Message { get; set; }
- public string PushNotificationHub { get; set; }
- }
5. Create the required JSON string
- private static Dictionary<string, string> GetWorkorderUpdatePushNotificationsCollection(
- WorkorderUpdateNotification message)
- {
- List<string> supportedLanguages = ResourceKeyConstants.SupportedLanguages;
- Dictionary<string, string> messageList = GetMessageTranslations(message, supportedLanguages);
- Dictionary<string, string> workorderUpdateNotifications = new Dictionary<string, string>();
- string notificationName = "WorkorderUpdateNotification_{0}";
- string alertName = "WorkorderUpdateAlert_{0}";
- foreach (var supportedLanguage in supportedLanguages)
- {
- string localizedNotificationName = string.Format(notificationName,
- supportedLanguage.Replace("-", string.Empty));
- string localizedAlertName = string.Format(alertName, supportedLanguage.Replace("-", string.Empty));
- Resources.terms.Culture = new CultureInfo(supportedLanguage);
- string from = Resources.terms.webapi_helpers_pushnotification_messages_FromDtz;
- WorkOrderUpdatePushNotification workOrderUpdatePushNotification = new WorkOrderUpdatePushNotification
- {
- WorkOrderNumber = message.WorkOrderNumber,
- IsSuccessful = message.Message.ToLower().Equals("successful"),
- From = from,
- Message = messageList[supportedLanguage],
- PushNotificationHub = HybridConfig.GetAppSetting("PushNotificationHub")
- };
- var alert = JsonConvert.SerializeObject(workOrderUpdatePushNotification);
- workorderUpdateNotifications.Add(localizedNotificationName, alert);
- workorderUpdateNotifications.Add(localizedAlertName, workOrderUpdatePushNotification.Message);
- }
- return workorderUpdateNotifications;
- }
6. Push the notification
- public static Dictionary<string, string> SendWorkorderUpdatePushNotificationTest(
- WorkorderUpdateNotification message)
- {
- NotificationHubClient hub = NotificationHubClient.CreateClientFromConnectionString
- (HybridConfig.GetAppSetting("PushNotificationServiceBus"),
- HybridConfig.GetAppSetting("PushNotificationHub"));
- Dictionary<string, string> workorderUpdateNotifications =
- GetWorkorderUpdatePushNotificationsCollection(message);
- hub.SendTemplateNotificationAsync(workorderUpdateNotifications, message.UserUpdated);
- return workorderUpdateNotifications;
- }
Happy Coding !!!
Comments
Post a Comment