Appverse.Core.PushNotifications.PushNotificationsUtils.JSONSerialize C# (CSharp) Method

JSONSerialize() public static method

public static JSONSerialize ( object data ) : String
data object
return String
		public static String JSONSerialize(object data) {
			try {
				JavaScriptSerializer Serialiser = new JavaScriptSerializer (); 
				return Serialiser.Serialize(data);
			} catch (Exception ex) {
				SystemLogger.Log(SystemLogger.Module.PLATFORM, "Error trying to serialize object to JSON.", ex);
			}
			return null;
		}

Usage Example

コード例 #1
0
        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")))
            {
                                #if DEBUG
                log(" ******* PROCESSING REMOTE NOTIFICATION Notification Payload received");
                                #endif
                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();
                            }
                        }

                                                #if DEBUG
                        log("******* PROCESSING NOTIFICATION Notification Payload contains an alert message. Type [" + alertType + "]");
                                                #endif
                    }

                    //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();
                                                #if DEBUG
                        log("******* PROCESSING NOTIFICATION Notification Payload contains sound");
                                                #endif
                    }

                    //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);
                                                #if DEBUG
                        log("******* PROCESSING NOTIFICATION Notification Payload contains a badge number: " + badge);
                                                #endif
                    }

                    //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)
                    {
                                                #if DEBUG
                        log("******* PROCESSING NOTIFICATION app was running, so manually showing notification");
                                                #endif

                        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);

                                                #if DEBUG
                        log("******* PROCESSING NOTIFICATION types enabled: alert[" + alertEnabled + "], sound[" + soundEnabled + "], badge[" + badgeEnabled + "]");
                                                #endif
                        //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);
                } catch (System.Exception ex) {
                                        #if DEBUG
                    log(" ******* Unhanlded exception processing notification payload received. Exception message: " + ex.Message);
                                        #endif
                } finally {
                    notificationData.AlertMessage = alert;
                    notificationData.Badge        = badge;
                    notificationData.Sound        = sound;

                    PushNotificationsUtils.FireUnityJavascriptEvent("Appverse.PushNotifications.OnRemoteNotificationReceived", notificationData);
                }
            }
            else
            {
                                #if DEBUG
                log(" ******* NO Notification Payload received");
                                #endif
            }
        }