Example #1
0
        private static NetmeraPushDetail parsePushResponse(JObject channelJson)
        {
            NetmeraPushDetail channelDetail = new NetmeraPushDetail();

            try
            {
                JObject result = channelJson.Value <JObject>("result");
                channelDetail.setError(result.Value <String>("error"));
                channelDetail.setFailed(result.Value <int>("failed"));
                channelDetail.setSuccessful(result.Value <int>("successful"));
                channelDetail.setMessage(result.Value <String>("message"));
                channelDetail.setStatus(result.Value <String>("status"));
                channelDetail.setPath(result.Value <String>("path"));
            }
            catch (JsonException)
            {
            }
            return(channelDetail);
        }
Example #2
0
        public static Dictionary <Netmera.BasePush.PushChannel, NetmeraPushDetail> sendPushMessage(String url, Dictionary <string, object> postParameters)
        {
            string userAgent = "Someone";

            StringBuilder strBuild = new StringBuilder(url);

            strBuild.Append("&").Append(NetmeraConstants.Netmera_SDK_Params).Append(NetmeraConstants.Netmera_SDK_Value);
            strBuild.Append("&").Append(NetmeraConstants.Netmera_SDKVERSION_Params).Append(NetmeraConstants.Netmera_SDKVERSION_Value);

            HttpWebResponse response = MultipartFormData.MultipartFormDataPost(url, userAgent, postParameters);

            if (response != null)
            {
                int statusCode = (int)response.StatusCode;

                if (statusCode == 200)
                {
                    Stream responseStream = response.GetResponseStream();
                    if (responseStream != null)
                    {
                        StreamReader responseStreamReader = new StreamReader(responseStream);
                        string       responseStr          = responseStreamReader.ReadToEnd();

                        JObject json = JObject.Parse(responseStr);
                        if (json.Value <int>("code") == 1000)
                        {
                            json = json.Value <JObject>("result");

                            Dictionary <Netmera.BasePush.PushChannel, NetmeraPushDetail> result = new Dictionary <Netmera.BasePush.PushChannel, NetmeraPushDetail>();

                            if (json[NetmeraConstants.Netmera_Push_Type_Android] != null)
                            {
                                NetmeraPushDetail androidDetail = new NetmeraPushDetail();
                                JObject           androidJson   = json.Value <JObject>(NetmeraConstants.Netmera_Push_Type_Android);
                                if (androidJson.Value <int>("code") == 1000)
                                {
                                    androidDetail = parsePushResponse(androidJson);
                                }
                                else
                                {
                                    String error = androidJson.Value <String>("message");
                                    androidDetail.setError(error);
                                }
                                result.Add(Netmera.BasePush.PushChannel.android, androidDetail);
                            }
                            if (json[NetmeraConstants.Netmera_Push_Type_Ios] != null)
                            {
                                NetmeraPushDetail iosDetail = new NetmeraPushDetail();
                                JObject           iosJson   = json.Value <JObject>(NetmeraConstants.Netmera_Push_Type_Ios);
                                if (iosJson.Value <int>("code") == 1000)
                                {
                                    iosDetail = parsePushResponse(iosJson);
                                }
                                else
                                {
                                    String error = iosJson.Value <String>("message");
                                    iosDetail.setError(error);
                                }
                                result.Add(Netmera.BasePush.PushChannel.ios, iosDetail);
                            }
                            if (json[NetmeraConstants.Netmera_Push_Type_Wp] != null)
                            {
                                NetmeraPushDetail wpDetail = new NetmeraPushDetail();
                                JObject           wpJson   = json.Value <JObject>(NetmeraConstants.Netmera_Push_Type_Wp);
                                if (wpJson.Value <int>("code") == 1000)
                                {
                                    wpDetail = parsePushResponse(wpJson);
                                }
                                else
                                {
                                    String error = wpJson.Value <String>("message");
                                    wpDetail.setError(error);
                                }
                                result.Add(Netmera.BasePush.PushChannel.wp, wpDetail);
                            }
                            return(result);
                        }
                        else
                        {
                            String error = json.Value <String>("message");
                            throw new NetmeraException(NetmeraException.ErrorCode.EC_PUSH_ERROR, error);
                        }
                    }
                    else
                    {
                        throw new NetmeraException(NetmeraException.ErrorCode.EC_NULL_EXCEPTION, "Response entity is null while sending notification to devices");
                    }
                }
                else
                {
                    throw new NetmeraException(NetmeraException.ErrorCode.EC_HTTP_PROTOCOL_EXCEPTION, "Http exception occurred while sending notification to android devices");
                }
            }
            else
            {
                throw new NetmeraException(NetmeraException.ErrorCode.EC_NULL_EXCEPTION, "Response is null while sending notification to android devices");
            }
        }