Пример #1
0
        public async Task CreateRegistrationAsync_PassValidGcmTemplateRegistration_GetCreatedRegistrationBack()
        {
            LoadMockData();
            await DeleteAllRegistrationsAndInstallations();

            var registration = new FcmTemplateRegistrationDescription(_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.FcmRegistrationId, createdRegistration.FcmRegistrationId);
            Assert.Equal(registration.BodyTemplate.Value, createdRegistration.BodyTemplate.Value);
            Assert.Equal(registration.TemplateName, createdRegistration.TemplateName);
            RecordTestResults();
        }
Пример #2
0
        public async Task <HttpResponseMessage> Put(string id, DeviceRegistration deviceUpdate)
        {
            // IMPORTANT: add logic to make sure that caller is allowed to register for the provided tags
            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 FcmTemplateRegistrationDescription(deviceUpdate.Handle, messageTemplate);
                break;

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

            registration.RegistrationId = id;

            var username = HttpContext.Current.User.Identity.Name;

            // add check if user is allowed to add these tags
            registration.Tags = new HashSet <string>(deviceUpdate.Tags);
            if (!registration.Tags.Contains("all"))
            {
                registration.Tags.Add("all");
            }
            registration.Tags.Add("username:" + username);

            try
            {
                await hub.CreateOrUpdateRegistrationAsync(registration);
            }
            catch (MessagingException e)
            {
                ReturnGoneIfHubResponseIsGone(e);
            }

            return(Request.CreateResponse(HttpStatusCode.OK));
        }