Esempio n. 1
0
        public static PushNotificationData GetNotificationData(RemoteMessage message)
        {
            /*
             * JSON Sample for notifications
             *
             *
             *
             *  {
             *     "data": {
             *        "title": "",
             *        "body": "",
             *        "action": 0, // Default value 0 stands for plain message (title and body)
             *        "payload": { // Optional. A custom JSON payload to be used in conjunction with the action }
             *     }
             *  }
             *
             */

            if (message == null)
            {
                return(null);
            }

            var data = new PushNotificationData();

            data.Add("title", message.Data.ContainsKey("title") ? message.Data["title"] : null);
            data.Add("body", message.Data.ContainsKey("body") ? message.Data["body"] : null);
            data.Add("action", message.Data.ContainsKey("action") ? Int32.Parse(message.Data["action"]) : 0);
            data.Add("payload", message.Data.ContainsKey("payload") ? message.Data["payload"] : null);

            return(data);
        }
Esempio n. 2
0
        public static PushNotificationData GetNotificationData(NSDictionary message)
        {
            /*
             * JSON Sample for notifications
             *
             *
             *
             *  {
             *     "aps":{
             *        "alert": {
             *          "title": "",
             *          "body": ""
             *        }
             *     },
             *     "data":{
             *        "action": 0, // Default value 0 stands for plain message (title and body)
             *        "payload":{ // Optional. A custom JSON payload to be used in conjunction with the action }
             *     }
             *  }
             *
             */

            if (message == null)
            {
                return(null);
            }

            PushNotificationData notification = new PushNotificationData();

            // Are we getting this from an "remote notification launch options" ?
            if (message.ContainsKey(UIApplication.LaunchOptionsRemoteNotificationKey))
            {
                message = (NSDictionary)message.ObjectForKey(UIApplication.LaunchOptionsRemoteNotificationKey);
            }

            // Check to see if the dictionary has the aps key.
            // This is the notification payload you would have sent
            if (message.ContainsKey(new NSString("aps")))
            {
                //Get the aps dictionary
                NSDictionary aps = message.ObjectForKey(new NSString("aps")) as NSDictionary;
                if (aps != null)
                {
                    //Extract the alert text
                    // NOTE: If you're using the simple alert by just specifying
                    // "  aps:{alert:"alert msg here"}  ", this will work fine.
                    // But if you're using a complex alert with Localization keys, etc.,
                    // your "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")))
                    {
                        if (aps.ObjectForKey(new NSString("alert")) is NSString)
                        {
                            var alert = aps.ObjectForKey(new NSString("alert")) as NSString;
                            notification.Add("title", null);
                            notification.Add("body", alert.ToString());
                        }
                        else if (aps.ObjectForKey(new NSString("alert")) is NSDictionary)
                        {
                            var alert = aps.ObjectForKey(new NSString("alert")) as NSDictionary;
                            if (alert != null)
                            {
                                if (alert.ContainsKey(new NSString("title")))
                                {
                                    notification.Add("title", alert.ObjectForKey(new NSString("title")).ToString());
                                }

                                if (alert.ContainsKey(new NSString("body")))
                                {
                                    notification.Add("body", alert.ObjectForKey(new NSString("body")).ToString());
                                }
                            }
                        }
                    }
                }
            }

            // Check to see if the dictionary has the data key.
            // This is the custom payload you would have sent
            if (message.ContainsKey(new NSString("data")))
            {
                //Get the data dictionary
                NSDictionary data = message.ObjectForKey(new NSString("data")) as NSDictionary;
                if (data != null)
                {
                    notification.Add("action", (data.ObjectForKey(new NSString("action")) as NSNumber).Int32Value);

                    if (data.ContainsKey(new NSString("payload")))
                    {
                        if (data.ObjectForKey(new NSString("payload")) is NSString)
                        {
                            NSString payload = data.ObjectForKey(new NSString("payload")) as NSString;
                            notification.Add("payload", payload.ToString());
                        }
                        else if (data.ObjectForKey(new NSString("payload")) is NSDictionary)
                        {
                            NSDictionary target = data.ObjectForKey(new NSString("payload")) as NSDictionary;
                            if (target != null)
                            {
                                NSError  error = null;
                                NSData   d     = NSJsonSerialization.Serialize(target, NSJsonWritingOptions.PrettyPrinted, out error);
                                NSString json  = NSString.FromData(d, NSStringEncoding.UTF8);
                                notification.Add("payload", json.ToString());
                            }
                        }
                    }
                }
            }
            else
            {
                notification.Add("action", 0);
                notification.Add("payload", null);
            }

            return(notification);
        }