Appverse.Core.PushNotifications.AbstractPushNotifications.ProcessRemoteNotification C# (CSharp) Method

ProcessRemoteNotification() private static method

private static ProcessRemoteNotification ( NSDictionary options, bool fromFinishedLaunching, UIApplicationState applicationState ) : void
options NSDictionary
fromFinishedLaunching bool
applicationState UIApplicationState
return void
		private static void ProcessRemoteNotification(NSDictionary options, bool fromFinishedLaunching, UIApplicationState applicationState) {

			//Check to see if the dictionary has the aps key.  This is the notification payload you would have sent
			if (options!=null && options.ContainsKey(new NSString("aps")))
			{
				SystemLogger.Log (SystemLogger.Module.PLATFORM, " ******* PROCESSING REMOTE NOTIFICATION Notification Payload received");

				NotificationData notificationData = new NotificationData ();
				string alert = string.Empty;
				string sound = string.Empty;
				int badge = -1;

				try {
					//Get the aps dictionary
					NSDictionary aps = options.ObjectForKey (new NSString ("aps")) as NSDictionary;


					//Extract the alert text
					//NOTE: Just for the simple alert specified by "  aps:{alert:"alert msg here"}  "
					//      For complex alert with Localization keys, etc., the "alert" object from the aps dictionary
					//      will be another NSDictionary... Basically the json gets dumped right into a NSDictionary, so keep that in mind
					if (aps.ContainsKey (new NSString ("alert"))) {
						string alertType = "undefined";
						if (aps[new NSString ("alert")].GetType () == typeof(NSString)) {
							alert = (aps [new NSString ("alert")] as NSString).ToString ();
							alertType = "NSString";
						} else if (aps [new NSString ("alert")].GetType () == typeof(NSDictionary)) {
							NSDictionary alertNSDictionary = aps.ObjectForKey (new NSString ("alert")) as NSDictionary;
							alertType = "NSDictionary";
							// We only get "body" key from that dictionary
							if (alertNSDictionary.ContainsKey (new NSString ("body")) 
								&& (alertNSDictionary[new NSString ("body")].GetType () == typeof(NSString))) {
								alert = (alertNSDictionary [new NSString ("body")] as NSString).ToString ();
							}
						}

						SystemLogger.Log (SystemLogger.Module.PLATFORM, "******* PROCESSING NOTIFICATION Notification Payload contains an alert message. Type [" + alertType + "]");

					}

					//Extract the sound string
					if (aps.ContainsKey (new NSString ("sound")) && (aps [new NSString ("sound")].GetType() == typeof(NSString))) {
						sound = (aps [new NSString ("sound")] as NSString).ToString ();
						SystemLogger.Log (SystemLogger.Module.PLATFORM, "******* PROCESSING NOTIFICATION Notification Payload contains sound");
					
					}

					//Extract the badge
					if (aps.ContainsKey (new NSString ("badge")) && (aps [new NSString ("badge")].GetType() == typeof(NSObject))) {
						string badgeStr = (aps [new NSString ("badge")] as NSObject).ToString ();
						int.TryParse (badgeStr, out badge);
						SystemLogger.Log (SystemLogger.Module.PLATFORM, "******* PROCESSING NOTIFICATION Notification Payload contains a badge number: " + badge);

					}

					//If this came from the ReceivedRemoteNotification while the app was running,
					// we of course need to manually process things like the sound, badge, and alert.
					if (!fromFinishedLaunching && applicationState == UIApplicationState.Active) {

						SystemLogger.Log (SystemLogger.Module.PLATFORM, "******* PROCESSING NOTIFICATION app was running, so manually showing notification");

						UIRemoteNotificationType enabledRemoteNotificationTypes = UIApplication.SharedApplication.EnabledRemoteNotificationTypes;

						bool alertEnabled = ((enabledRemoteNotificationTypes & UIRemoteNotificationType.Alert) == UIRemoteNotificationType.Alert);
						bool soundEnabled = ((enabledRemoteNotificationTypes & UIRemoteNotificationType.Sound) == UIRemoteNotificationType.Sound);
						bool badgeEnabled = ((enabledRemoteNotificationTypes & UIRemoteNotificationType.Badge) == UIRemoteNotificationType.Badge);

						SystemLogger.Log (SystemLogger.Module.PLATFORM, "******* PROCESSING NOTIFICATION types enabled: alert[" + alertEnabled+"], sound[" + soundEnabled + "], badge[" + badgeEnabled+ "]");

						//Manually set the badge in case this came from a remote notification sent while the app was open
						if (badgeEnabled) {
							UpdateApplicationIconBadgeNumber (badge);
						}

						//Manually play the sound
						if (soundEnabled) {
							PlayNotificationSound (sound);
						}

						//Manually show an alert
						if (alertEnabled) {
							ShowNotificationAlert ("Notification", alert);
						}
					}


					Dictionary<String,Object> customDic = PushNotificationsUtils.ConvertToDictionary (new NSMutableDictionary (options));
					customDic.Remove ("aps"); // it is not needed to pass the "aps" (notification iOS data) inside the "custom data json string"
					notificationData.CustomDataJsonString = PushNotificationsUtils.JSONSerialize (customDic);
					notificationData.AppWasRunning = applicationState == UIApplicationState.Active ? true : false;

				} catch (System.Exception ex) {
					SystemLogger.Log (SystemLogger.Module.PLATFORM,  "******* Unhanlded exception processing notification payload received. Exception message: " + ex.Message);

				} finally {

					notificationData.AlertMessage = alert;
					notificationData.Badge = badge;
					notificationData.Sound = sound;

					PushNotificationsUtils.FireUnityJavascriptEvent ("Appverse.PushNotifications.OnRemoteNotificationReceived", notificationData);
				}

			} else {
				SystemLogger.Log (SystemLogger.Module.PLATFORM, " ******* NO Notification Payload received");
			}
		}