public void Simple()
        {
            var service = new PushService
                {
                    RequestBuilder = RequestBuilderHelper.Build()
                };
            var pushNotification = new PushNotificationRequest
                {
                    DeviceTokens = new List<string>
                        {
                            RemoteSettings.AppleDeviceId
                        },
                    Payload = new PushPayload
                        {
                            Alert = "Alert"
                        },
                    CustomData = new Dictionary<string, string>
                        {
                         {"Key", "Value"}
                        }
                };

            var asyncTestHelper = new AsyncTestHelper();
            service.Execute(pushNotification, response => asyncTestHelper.Callback(null), asyncTestHelper.HandleException);
            asyncTestHelper.Wait();
        }
    public void Simple()
    {
        var service = new PushService
            {
                RequestBuilder = RequestBuilderHelper.Build()
            };
        var request = new PushNotificationRequest
            {
                DeviceTokens = new List<string>
                    {
                        "BadToken"
                    },
                Payload = new PushPayload
                    {
                        Alert = "Alert"
                    },
            };

        Exception exception = null;
        try
        {

            var asyncTestHelper = new AsyncTestHelper();
            service.Execute(request, respone => asyncTestHelper.Callback(null), asyncTestHelper.HandleException);
            asyncTestHelper.Wait();
        }
        catch (Exception e)
        {
            exception = e;
        }

        var remoteException = exception as RemoteException;
        Assert.IsNotNull(remoteException);
        Assert.AreEqual("{\"error_code\": 40001, \"details\": {\"device_tokens.0.device_token\": [\"device_token contains an invalid device token: BADTOKEN\"]}, \"error\": \"Data validation error\"}", remoteException.Message);
    }
        static JsonObject JsonObj(PushNotificationRequest notification)
        {
            var jsonObj = new JsonObject();

            jsonObj["aps"] = BatchPushRequestSerializer.JsonObject(notification.Payload);
            if (notification.DeviceTokens != null)
            {
                jsonObj["device_tokens"] = notification.DeviceTokens.ToJsonArray();
            }
            if (notification.Aliases != null)
            {
                jsonObj["aliases"] = notification.Aliases.ToJsonArray();
            }
            if (notification.Tags != null)
            {
                jsonObj["tags"] = notification.Tags.ToJsonArray();
            }
            if (notification.ExcludeTokens != null)
            {
                jsonObj["exclude_tokens"] = notification.ExcludeTokens.ToJsonArray();
            }

            if (notification.CustomData != null)
            {
                foreach (var pair in notification.CustomData)
                {
                    BatchPushRequestSerializer.ValidateKey(pair.Key);
                    jsonObj[pair.Key] = pair.Value;
                }
            }

            return(jsonObj);
        }
        static JsonObject JsonObj(PushNotificationRequest notification)
        {
            var jsonObj = new JsonObject();

            jsonObj["aps"] = BatchPushRequestSerializer.JsonObject(notification.Payload);
            if (notification.DeviceTokens != null)
            {
                jsonObj["device_tokens"] = notification.DeviceTokens.ToJsonArray();
            }
            if (notification.Aliases != null)
            {
                jsonObj["aliases"] = notification.Aliases.ToJsonArray();
            }
            if (notification.Tags != null)
            {
                jsonObj["tags"] = notification.Tags.ToJsonArray();
            }
            if (notification.ExcludeTokens != null)
            {
                jsonObj["exclude_tokens"] = notification.ExcludeTokens.ToJsonArray();
            }

            if (notification.CustomData != null)
            {
                foreach (var pair in notification.CustomData)
                {
                    BatchPushRequestSerializer.ValidateKey(pair.Key);
                    jsonObj[pair.Key] = pair.Value;
                }
            }

            return jsonObj;
        }
Esempio n. 5
0
        public void Execute(PushNotificationRequest request, Action<PushNotificationResponse> responseCallback, Action<Exception> exceptionCallback)
        {
            var webRequest = RequestBuilder.Build("https://go.urbanairship.com/api/push/");
            webRequest.Method = "POST";
            webRequest.ContentType = "application/json";
            //TODO: must have tags or tokens... validate

            var asyncRequest = new AsyncRequest
                                   {
                                       WriteToRequest = stream => stream.WriteToStream(request.Serialize),
                                       Request = webRequest,
                                       ReadFromResponse = o => responseCallback(new PushNotificationResponse()),
                                       ExceptionCallback = exceptionCallback,
                                   };

            asyncRequest.Execute();
        }
Esempio n. 6
0
        public void Execute(PushNotificationRequest request, Action <PushNotificationResponse> responseCallback, Action <Exception> exceptionCallback)
        {
            var webRequest = RequestBuilder.Build("https://go.urbanairship.com/api/push/");

            webRequest.Method      = "POST";
            webRequest.ContentType = "application/json";
            //TODO: must have tags or tokens... validate

            var asyncRequest = new AsyncRequest
            {
                WriteToRequest    = stream => stream.WriteToStream(request.Serialize),
                Request           = webRequest,
                ReadFromResponse  = o => responseCallback(new PushNotificationResponse()),
                ExceptionCallback = exceptionCallback,
            };

            asyncRequest.Execute();
        }
        public void ToTag()
        {
            var service = new PushService
                {
                    RequestBuilder = RequestBuilderHelper.Build()
                };
            var pushNotification = new PushNotificationRequest
                {
                    Tags = new List<string> { "africa" },
                    Payload = new PushPayload
                        {
                            Alert = "Alert 2"
                        }
                };

            var asyncTestHelper = new AsyncTestHelper();
            service.Execute(pushNotification, response => asyncTestHelper.Callback(null), asyncTestHelper.HandleException);
            asyncTestHelper.Wait();
        }
 public void Simple()
 {
     var service = new PushService
                       {
                           RequestBuilder = ServerRequestBuilder.Instance
                       };
     var notification = new PushNotificationRequest
                            {
                                Tags = new List<string> {"MyTag"},
                                ExcludeTokens = new List<string> {"TokenToExclude"},
                                DeviceTokens = new List<string> {"AppleDeviceId"},
                                Aliases = new List<string> {"MyAlias"},
                                Payload = new PushPayload
                                              {
                                                  Alert = "Alert 2",
                                                  Badge = Badge.SetTo(2),
                                                  Sound = "Sound1"
                                              }
                            };
     service.Execute(notification, response => Debug.WriteLine("Success"),ExceptionHandler.Handle);
 }
        public void ToAlias()
        {
            var service = new PushService
            {
                RequestBuilder = RequestBuilderHelper.Build()
            };

            var random = new Random();
            var pushNotification = new PushNotificationRequest
            {
                Aliases = new List<string>(new string[] { "1gzod" }),                
                Payload = new PushPayload
                {
                    Badge = random.Next(100),
                    Alert = "What's up iPhone",
                }
            };

            var asyncTestHelper = new AsyncTestHelper();
            service.Execute(pushNotification, response => asyncTestHelper.Callback(null), asyncTestHelper.HandleException);
            asyncTestHelper.Wait();
        }
 public void Single()
 {
     var notification = new PushNotificationRequest
                            {
                                Payload = new PushPayload
                                              {
                                                  Alert = "My Alert",
                                                  Sound = "My Sound",
                                                  Badge = Badge.Increment()
                                              },
                                Aliases = new List<string> {"alias1"},
                                Tags = new List<string> {"tag1"},
                                DeviceTokens = new List<string> {"token1"},
                                ExcludeTokens = new List<string> {"exclude1"},
                            };
     var text = notification.Serialize().FormatAsJson();
     var expected = @"
     {
       'aps': {
     'alert': 'My Alert',
     'badge': '+1',
     'sound': 'My Sound'
       },
       'device_tokens': [
     'token1'
       ],
       'aliases': [
     'alias1'
       ],
       'tags': [
     'tag1'
       ],
       'exclude_tokens': [
     'exclude1'
       ]
     }".Replace("\r\n", "\n");
     Assert.AreEqual(expected, text);
 }
        public static string Serialize(this PushNotificationRequest notification)
        {
            var jsonObj = JsonObj(notification);

            return(jsonObj.ToString());
        }