Saturday, June 13, 2015

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.

  1. public const string LangzhSG = "zh-sg";
  2.       public const string LangenUS = "en-us";
  3.       public const string LangenGB = "en-gb";
  4.       public const string LangitIT = "it-it";
  5.       public const string LangsiLK = "si-lk";

 

  1. public static List<string> SupportedLanguages
  2.     {
  3.         get { return new List<string> { LangzhSG, LangenUS, LangenGB, LangitIT, LangsiLK }; }
  4.     }

3.  Localize the messages as your need. In here based on work order status message to be sent will be varied.

  1. public static Dictionary<string, string> GetMessageTranslations(WorkorderUpdateNotification message,
  2.                                                                       List<string> supportedLanguages)
  3.       {
  4.           string messageText = string.Empty;
  5.  
  6.           Dictionary<string, string> messageList = new Dictionary<string, string>();
  7.           if (message.Message.ToLower().Equals("failed"))
  8.           {
  9.  
  10.               foreach (var supportedLanguage in supportedLanguages)
  11.               {
  12.                   Resources.terms.Culture = new CultureInfo(supportedLanguage);
  13.                   messageText =
  14.                       string.Format(Resources.terms.webapi_helpers_pushnotification_messages_FailedToUpdateWO1,
  15.                                     message.WorkOrderNumber);
  16.                   messageList.Add(supportedLanguage, messageText);
  17.               }
  18.  
  19.           }
  20.  
  21.           return messageList;
  22.       }
  23.  
  24.   }

4. Create the class which has the properties which notification holds all required data.

  1.  
  2. public class WorkOrderUpdatePushNotification
  3. {
  4.     public string WorkOrderNumber { get; set; }
  5.     public bool IsSuccessful { get; set; }
  6.     public string From { get; set; }
  7.     public string Message { get; set; }
  8.     public string PushNotificationHub { get; set; }
  9. }

5. Create the required JSON string

  1. private static Dictionary<string, string> GetWorkorderUpdatePushNotificationsCollection(
  2.          WorkorderUpdateNotification message)
  3.      {
  4.  
  5.          List<string> supportedLanguages = ResourceKeyConstants.SupportedLanguages;
  6.          Dictionary<string, string> messageList = GetMessageTranslations(message, supportedLanguages);
  7.          Dictionary<string, string> workorderUpdateNotifications = new Dictionary<string, string>();
  8.          string notificationName = "WorkorderUpdateNotification_{0}";
  9.          string alertName = "WorkorderUpdateAlert_{0}";
  10.          foreach (var supportedLanguage in supportedLanguages)
  11.          {
  12.              string localizedNotificationName = string.Format(notificationName,
  13.                                                               supportedLanguage.Replace("-", string.Empty));
  14.              string localizedAlertName = string.Format(alertName, supportedLanguage.Replace("-", string.Empty));
  15.              Resources.terms.Culture = new CultureInfo(supportedLanguage);
  16.              string from = Resources.terms.webapi_helpers_pushnotification_messages_FromDtz;
  17.  
  18.              WorkOrderUpdatePushNotification workOrderUpdatePushNotification = new WorkOrderUpdatePushNotification
  19.                  {
  20.                      WorkOrderNumber = message.WorkOrderNumber,
  21.                      IsSuccessful = message.Message.ToLower().Equals("successful"),
  22.                      From = from,
  23.                      Message = messageList[supportedLanguage],
  24.                      PushNotificationHub = HybridConfig.GetAppSetting("PushNotificationHub")
  25.                  };
  26.  
  27.              var alert = JsonConvert.SerializeObject(workOrderUpdatePushNotification);
  28.  
  29.              workorderUpdateNotifications.Add(localizedNotificationName, alert);
  30.              workorderUpdateNotifications.Add(localizedAlertName, workOrderUpdatePushNotification.Message);
  31.          }
  32.  
  33.  
  34.  
  35.          return workorderUpdateNotifications;
  36.      }

6. Push the notification

  1. public static Dictionary<string, string> SendWorkorderUpdatePushNotificationTest(
  2.          WorkorderUpdateNotification message)
  3.      {
  4.          NotificationHubClient hub = NotificationHubClient.CreateClientFromConnectionString
  5.              (HybridConfig.GetAppSetting("PushNotificationServiceBus"),
  6.               HybridConfig.GetAppSetting("PushNotificationHub"));
  7.  
  8.  
  9.  
  10.          Dictionary<string, string> workorderUpdateNotifications =
  11.              GetWorkorderUpdatePushNotificationsCollection(message);
  12.  
  13.          hub.SendTemplateNotificationAsync(workorderUpdateNotifications, message.UserUpdated);
  14.  
  15.          return workorderUpdateNotifications;
  16.      }

 

Happy Coding !!!

No comments :

Post a Comment