Ejemplo n.º 1
0
        public void Serialize(EntityDescription description, XmlWriter writer)
        {
            // Convert FCM descriptions into their GCM counterparts
            if (description.GetType().Name == "FcmRegistrationDescription")
            {
                description = new GcmRegistrationDescription((FcmRegistrationDescription)description);
            }

            if (description.GetType().Name == "FcmTemplateRegistrationDescription")
            {
                description = new GcmTemplateRegistrationDescription((FcmTemplateRegistrationDescription)description);
            }

            DataContractSerializer serializer;

            if (description is RegistrationDescription)
            {
                serializer = GetSerializer(typeof(RegistrationDescription).Name);
            }
            else
            {
                serializer = GetSerializer(description.GetType().Name);
            }

            serializer.WriteObject(writer, description);
        }
Ejemplo n.º 2
0
        public async Task CreateRegistrationAsync_PassValidGcmTemplateRegistration_GetCreatedRegistrationBack()
        {
            LoadMockData();
            await DeleteAllRegistrationsAndInstallations();

            var registration = new GcmTemplateRegistrationDescription(_configuration["GcmDeviceToken"], "{\"data\":{\"message\":\"Message\"}}");

            registration.PushVariables = new Dictionary <string, string>()
            {
                { "var1", "value1" }
            };
            registration.Tags = new HashSet <string>()
            {
                "tag1"
            };
            registration.TemplateName = "Template Name";

            var createdRegistration = await _hubClient.CreateRegistrationAsync(registration);

            Assert.NotNull(createdRegistration.RegistrationId);
            Assert.NotNull(createdRegistration.ETag);
            Assert.NotNull(createdRegistration.ExpirationTime);
            Assert.Contains(new KeyValuePair <string, string>("var1", "value1"), createdRegistration.PushVariables);
            Assert.Contains("tag1", createdRegistration.Tags);
            Assert.Equal(registration.GcmRegistrationId, createdRegistration.GcmRegistrationId);
            Assert.Equal(registration.BodyTemplate.Value, createdRegistration.BodyTemplate.Value);
            Assert.Equal(registration.TemplateName, createdRegistration.TemplateName);
            RecordTestResults();
        }
Ejemplo n.º 3
0
        private RegistrationDescription CriaRegistration(RegistroNotificacaoViewModel model)
        {
            RegistrationDescription registration;

            switch (model.Plataforma)
            {
            case PlataformaNotificacao.WNS:
                var template = @"<toast><visual><binding template=""ToastText01""><text id=""1"">$(message)</text></binding></visual></toast>";
                registration = new WindowsTemplateRegistrationDescription(model.Handle, template);
                break;

            case PlataformaNotificacao.GCM:
                template     = "{\"data\":{\"message\":\"$(message)\"}}";
                registration = new GcmTemplateRegistrationDescription(model.Handle, template);
                break;

            case PlataformaNotificacao.APNS:
                template     = "{\"aps\":{\"alert\":\"$(message)\"}}";
                registration = new AppleTemplateRegistrationDescription(model.Handle, template);
                break;

            default:
                throw new InvalidOperationException();
            }
            return(registration);
        }
Ejemplo n.º 4
0
        public string Serialize(EntityDescription description)
        {
            var stringBuilder = new StringBuilder();

            var settings = new XmlWriterSettings
            {
                OmitXmlDeclaration = true
            };

            // Convert FCM descriptions into their GCM counterparts
            if (description.GetType().Name == "FcmRegistrationDescription")
            {
                description = new GcmRegistrationDescription((FcmRegistrationDescription)description);
            }

            if (description.GetType().Name == "FcmTemplateRegistrationDescription")
            {
                description = new GcmTemplateRegistrationDescription((FcmTemplateRegistrationDescription)description);
            }

            var serializer = GetSerializer(description.GetType().Name);

            using (var xmlWriter = XmlWriter.Create(stringBuilder, settings))
            {
                serializer.WriteObject(xmlWriter, description);
            }

            return(stringBuilder.ToString());
        }
Ejemplo n.º 5
0
        async public Task <string> Register(DeviceRegistration registration)
        {
            if (string.IsNullOrWhiteSpace(registration.Handle))
            {
                return(null);
            }

            const string templateBodyAPNS = @"{ ""aps"" : { ""content-available"" : 1, ""alert"" : { ""title"" : ""$(title)"", ""body"" : ""$(message)"", ""badge"":""#(badge)"" } }, ""payload"" : ""$(payload)"" }";
            const string templateBodyFCM  = @"{""data"":{""title"":""$(title)"",""message"":""$(message)"",""payload"":""$(payload)""}}";

            string newRegistrationId = null;
            // make sure there are no existing registrations for this push handle (used for iOS and Android)
            var registrations = await _hub.GetRegistrationsByChannelAsync(registration.Handle, 100);

            foreach (var reg in registrations)
            {
                if (newRegistrationId == null)
                {
                    newRegistrationId = reg.RegistrationId;
                }
                else
                {
                    await _hub.DeleteRegistrationAsync(reg);
                }
            }

            if (newRegistrationId == null)
            {
                newRegistrationId = await _hub.CreateRegistrationIdAsync();
            }

            RegistrationDescription description;

            switch (registration.Platform)
            {
            case "iOS":
                registration.Handle = registration.Handle.Replace("<", "").Replace(">", "").Replace(" ", "").ToUpper();
                description         = new AppleTemplateRegistrationDescription(registration.Handle, templateBodyAPNS, registration.Tags);
                break;

            case "Android":
                description = new GcmTemplateRegistrationDescription(registration.Handle, templateBodyFCM, registration.Tags);
                break;

            default:
                return(null);
            }

            description.RegistrationId = newRegistrationId;
            var result = await _hub.CreateOrUpdateRegistrationAsync(description);

            return(result?.RegistrationId);
        }
Ejemplo n.º 6
0
        // PUT api/register/5
        // This creates or updates a registration (with provided channelURI) at the specified id
        public async Task <HttpResponseMessage> Put(string id, DeviceRegistration deviceUpdate)
        {
            RegistrationDescription registration = null;

            switch (deviceUpdate.Platform)
            {
            case "mpns":
                var toastTemplate = "<?xml version=\"1.0\" encoding=\"utf-8\"?>" +
                                    "<wp:Notification xmlns:wp=\"WPNotification\">" +
                                    "<wp:Toast>" +
                                    "<wp:Text1>$(message)</wp:Text1>" +
                                    "</wp:Toast> " +
                                    "</wp:Notification>";
                registration = new MpnsTemplateRegistrationDescription(deviceUpdate.Handle, toastTemplate);
                break;

            case "wns":
                toastTemplate = @"<toast><visual><binding template=""ToastText01""><text id=""1"">$(message)</text></binding></visual></toast>";
                registration  = new WindowsTemplateRegistrationDescription(deviceUpdate.Handle, toastTemplate);
                break;

            case "apns":
                var alertTemplate = "{\"aps\":{\"alert\":\"$(nameandmsg)\",\"badge\":1}}";
                registration = new AppleTemplateRegistrationDescription(deviceUpdate.Handle, alertTemplate);
                break;

            case "gcm":
                var messageTemplate = "{\"data\":{\"msg\":\"$(message)\",\"senderid\":\"$(userid)\",\"sender\":\"$(username)\",\"icon\":\"$(usericon)\"}}";
                registration = new GcmTemplateRegistrationDescription(deviceUpdate.Handle, messageTemplate);
                break;

            default:
                throw new HttpResponseException(HttpStatusCode.BadRequest);
            }
            registration.RegistrationId = id;
            registration.Tags           = new HashSet <string>(deviceUpdate.Tags);
            try
            {
                await _hub.CreateOrUpdateRegistrationAsync(registration);
            }
            catch (MessagingException e)
            {
                ReturnGoneIfHubResponseIsGone(e);
            }

            return(Request.CreateResponse(HttpStatusCode.OK));
        }
        public async Task <string> RegisterWithHub(DeviceRegistration deviceUpdate)
        {
            string newRegistrationId = null;

            try
            {
                // make sure there are no existing registrations for this push handle (used for iOS and Android)
                if (deviceUpdate.Handle != null)
                {
                    //Azure likes to uppercase the iOS device handles for some reason - no worries tho, I only spent 2 hours tracking this down
                    if (deviceUpdate.Platform == "iOS")
                    {
                        deviceUpdate.Handle = deviceUpdate.Handle.ToUpper();
                    }

                    var registrations = await _hub.GetRegistrationsByChannelAsync(deviceUpdate.Handle, 100);

                    foreach (var reg in registrations)
                    {
                        if (newRegistrationId == null)
                        {
                            newRegistrationId = reg.RegistrationId;
                        }
                        else
                        {
                            await _hub.DeleteRegistrationAsync(reg);
                        }
                    }
                }

                if (newRegistrationId == null)
                {
                    newRegistrationId = await _hub.CreateRegistrationIdAsync();
                }

                RegistrationDescription registration = null;

                switch (deviceUpdate.Platform)
                {
                case "iOS":
                    var alertTemplate = "{\"aps\":{\"alert\":\"$(message)\",\"badge\":\"#(badge)\",\"payload\":\"$(payload)\"}}";
                    registration = new AppleTemplateRegistrationDescription(deviceUpdate.Handle, alertTemplate);
                    break;

                case "Android":
                    var messageTemplate = "{\"data\":{\"title\":\"Sport\",\"message\":\"$(message)\",\"payload\":\"$(payload)\"}}";
                    registration = new GcmTemplateRegistrationDescription(deviceUpdate.Handle, messageTemplate);
                    break;

                default:
                    throw new HttpResponseException(HttpStatusCode.BadRequest);
                }

                registration.RegistrationId = newRegistrationId;
                registration.Tags           = new HashSet <string>(deviceUpdate.Tags);
                await _hub.CreateOrUpdateRegistrationAsync(registration);
            }
            catch (Exception ex)
            {
                throw ex.GetBaseException().Message.ToException(Request);
            }

            return(newRegistrationId);
        }
Ejemplo n.º 8
0
		public async Task<string> RegisterWithHub(DeviceRegistration deviceUpdate)
		{
			string newRegistrationId = null;
			try
			{
				// make sure there are no existing registrations for this push handle (used for iOS and Android)
				if(deviceUpdate.Handle != null)
				{
					//Azure likes to uppercase the iOS device handles for some reason - no worries tho, I only spent 2 hours tracking this down
					if(deviceUpdate.Platform == "iOS")
						deviceUpdate.Handle = deviceUpdate.Handle.ToUpper();

					var registrations = await _hub.GetRegistrationsByChannelAsync(deviceUpdate.Handle, 100);

					foreach(var reg in registrations)
					{
						if(newRegistrationId == null)
						{
							newRegistrationId = reg.RegistrationId;
						}
						else
						{
							await _hub.DeleteRegistrationAsync(reg);
						}
					}
				}

				if(newRegistrationId == null)
					newRegistrationId = await _hub.CreateRegistrationIdAsync();

				RegistrationDescription registration = null;

				switch(deviceUpdate.Platform)
				{
					case "iOS":
						var alertTemplate = "{\"aps\":{\"alert\":\"$(message)\",\"badge\":\"#(badge)\",\"payload\":\"$(payload)\"}}";
						registration = new AppleTemplateRegistrationDescription(deviceUpdate.Handle, alertTemplate);
						break;
					case "Android":
						var messageTemplate = "{\"data\":{\"title\":\"Sport\",\"message\":\"$(message)\",\"payload\":\"$(payload)\"}}";
						registration = new GcmTemplateRegistrationDescription(deviceUpdate.Handle, messageTemplate);
						break;
					default:
						throw new HttpResponseException(HttpStatusCode.BadRequest);
				}

				registration.RegistrationId = newRegistrationId;
				registration.Tags = new HashSet<string>(deviceUpdate.Tags);
				await _hub.CreateOrUpdateRegistrationAsync(registration);
			}
			catch(Exception ex)
			{
				throw ex.GetBaseException().Message.ToException(Request);
			}
			
			return newRegistrationId;
		}
Ejemplo n.º 9
0
        // PUT api/register/5
        // This creates or updates a registration (with provided channelURI) at the specified id
        public async Task<HttpResponseMessage> Put(string id, DeviceRegistration deviceUpdate)
        {
            RegistrationDescription registration = null;
            switch (deviceUpdate.Platform)
            {
                case "mpns":
                    var toastTemplate = "<?xml version=\"1.0\" encoding=\"utf-8\"?>" +
                        "<wp:Notification xmlns:wp=\"WPNotification\">" +
                           "<wp:Toast>" +
                                "<wp:Text1>$(message)</wp:Text1>" +
                           "</wp:Toast> " +
                        "</wp:Notification>";
                    registration = new MpnsTemplateRegistrationDescription(deviceUpdate.Handle, toastTemplate);
                    break;
                case "wns":
                    toastTemplate = @"<toast><visual><binding template=""ToastText01""><text id=""1"">$(message)</text></binding></visual></toast>";
                    registration = new WindowsTemplateRegistrationDescription(deviceUpdate.Handle, toastTemplate);
                    break;
                case "apns":
                    var alertTemplate = "{\"aps\":{\"alert\":\"$(message)\"}}";
                    registration = new AppleTemplateRegistrationDescription(deviceUpdate.Handle, alertTemplate);
                    break;
                case "gcm":
                    var messageTemplate = "{\"data\":{\"msg\":\"$(message)\"}}";
                    registration = new GcmTemplateRegistrationDescription(deviceUpdate.Handle, messageTemplate);
                    break;
                default:
                    throw new HttpResponseException(HttpStatusCode.BadRequest);
            }
            registration.RegistrationId = id;
            registration.Tags = new HashSet<string>(deviceUpdate.Tags);
            try
            {
                await _hub.CreateOrUpdateRegistrationAsync(registration);
            }
            catch (MessagingException e)
            {
                ReturnGoneIfHubResponseIsGone(e);
            }

            return Request.CreateResponse(HttpStatusCode.OK);
        }
        public async Task <Response <string> > RegisterDeviceAsync(Platform platform, string deviceToken, string username, List <int> deviceIds)
        {
            Response <string> response = new Response <string>();

            try
            {
                var registrationIdResponse = await GetRegistrationIdForDeviceAsync(deviceToken);

                if (registrationIdResponse.IsSuccess == false)
                {
                    return(registrationIdResponse);
                }

                RegistrationDescription registration = null;
                switch (platform)
                {
                case Enums.Platform.apns:
                    var alertTemplate = string.Format("{{\"aps\":{{\"alert\":\"$({0})\" }}, \"base64Data\" : \"$({1})\", \"messageType\" : \"$({2})\"}}",
                                                      Constants.NOTIFICATION_MESSAGE_KEY, Constants.BASE64_MESSAGE_KEY, Constants.MESSAGE_TYPE);
                    registration = new AppleTemplateRegistrationDescription(deviceToken, alertTemplate);
                    break;

                case Enums.Platform.gcm:
                    var messageTemplate = string.Format("{{\"data\":{{\"message\":\"$({0})\", \"base64Data\" : \"$({1})\", \"messageType\" : \"$({2})\"}}}}",
                                                        Constants.NOTIFICATION_MESSAGE_KEY,
                                                        Constants.BASE64_MESSAGE_KEY, Constants.MESSAGE_TYPE);
                    registration = new GcmTemplateRegistrationDescription(deviceToken, messageTemplate);
                    break;

                default:
                    return(null);
                }

                registration.RegistrationId = registrationIdResponse.Value;
                // add check if user is allowed to add these tags
                if (deviceIds != null)
                {
                    registration.Tags = new HashSet <string>(deviceIds.Select(s => s.ToString()));
                }
                if (!string.IsNullOrEmpty(username))
                {
                    registration.Tags.Add(username);
                }

                var registrationResponse = await hub.CreateOrUpdateRegistrationAsync(registration);

                if (registrationResponse == null)
                {
                    response.IsSuccess = false;
                    response.Message   = Constants.GENERIC_ERROR_REGISTER_DEVICE;
                }
                response.Value = registrationIdResponse.Value;
            }
            catch (Exception ex)
            {
                response.IsSuccess = false;
                response.Message   = Constants.GENERIC_ERROR_MESSAGE + ex.Message;
            }

            return(response);
        }