示例#1
0
        private static async Task CreateAndDeleteInstallationAsync(NotificationHubClient nhClient)
        {
            // Register some fake devices
            //var fcmDeviceId = Guid.NewGuid().ToString();
            var apnsDeviceId = Guid.NewGuid().ToString();

            Console.WriteLine($"The APNS device ID is {apnsDeviceId}");
            //var fcmInstallation = new Installation
            var apnsInstallation = new Installation
            {
                //InstallationId = fcmDeviceId,
                InstallationId = apnsDeviceId,
                Platform       = NotificationPlatform.Fcm,
                //PushChannel = fcmDeviceId,
                PushChannel        = apnsDeviceId,
                PushChannelExpired = false,
                //Tags = new[] { "fcm" }
                Tags = new[] { "apns" }
            };
            //await nhClient.CreateOrUpdateInstallationAsync(fcmInstallation);
            await nhClient.CreateOrUpdateInstallationAsync(apnsInstallation);

            while (true)
            {
                try
                {
                    //var installationFromServer = await nhClient.GetInstallationAsync(fcmInstallation.InstallationId);
                    var installationFromServer = await nhClient.GetInstallationAsync(apnsInstallation.InstallationId);

                    break;
                }
                catch (MessagingEntityNotFoundException)
                {
                    // Wait for installation to be created
                    await Task.Delay(1000);
                }
            }
            //Console.WriteLine($"Created FCM installation {fcmInstallation.InstallationId}");
            Console.WriteLine($"Created APNS installation {apnsInstallation.InstallationId}");
            //await nhClient.DeleteInstallationAsync(fcmInstallation.InstallationId);
            await nhClient.DeleteInstallationAsync(apnsInstallation.InstallationId);

            while (true)
            {
                try
                {
                    //var installationFromServer = await nhClient.GetInstallationAsync(fcmInstallation.InstallationId);
                    var installationFromServer = await nhClient.GetInstallationAsync(apnsInstallation.InstallationId);

                    await Task.Delay(1000);
                }
                catch (MessagingEntityNotFoundException)
                {
                    //Console.WriteLine($"Deleted FCM installation {fcmInstallation.InstallationId}");
                    Console.WriteLine($"Deleted APNS installation {apnsInstallation.InstallationId}");
                    break;
                }
            }
        }
        public async Task <DeviceInstallation> GetInstallation(string installationId)
        {
            var receivedInstallation = await _hub.GetInstallationAsync(installationId);

            return(new DeviceInstallation
            {
                InstallationId = receivedInstallation.InstallationId,
                Platform = receivedInstallation.Platform.ToString(),
                PushChannel = receivedInstallation.PushChannel,
                Tags = receivedInstallation.Tags.ToList()
            });
        }
示例#3
0
        private async Task CreateOrUpdateInstallationAsync_CreateInstallationWithExpiryInTemplate_GetCreatedInstallationWithExpiryInTemplateBack()
        {
            LoadMockData();

            await DeleteAllRegistrationsAndInstallations();

            var installationId = _testServer.NewGuid().ToString();

            var installation = new Installation
            {
                InstallationId = installationId,
                Platform       = NotificationPlatform.Apns,
                PushChannel    = _configuration["AppleDeviceToken"],
                PushVariables  = new Dictionary <string, string> {
                    { "var1", "value1" }
                },
                Tags = new List <string> {
                    "tag1"
                },
                Templates = new Dictionary <string, InstallationTemplate>
                {
                    {
                        "Template Name", new InstallationTemplate
                        {
                            Body   = "{\"aps\":{\"alert\":\"alert!\"}}",
                            Expiry = DateTime.Now.AddDays(1).ToString("o")
                        }
                    }
                }
            };

            await _hubClient.CreateOrUpdateInstallationAsync(installation);

            await Sleep(TimeSpan.FromSeconds(1));

            var createdInstallation = await _hubClient.GetInstallationAsync(installationId);

            Assert.Equal(installation.InstallationId, createdInstallation.InstallationId);
            Assert.Equal(installation.Platform, createdInstallation.Platform);
            Assert.Equal(installation.PushChannel, createdInstallation.PushChannel);
            Assert.Contains(new KeyValuePair <string, string>("var1", "value1"), createdInstallation.PushVariables);
            Assert.Contains("tag1", createdInstallation.Tags);
            Assert.Contains("Template Name", createdInstallation.Templates.Keys);
            Assert.Equal(installation.Templates["Template Name"].Body, createdInstallation.Templates["Template Name"].Body);

            RecordTestResults();
        }
        private static async Task CreateAndDeleteInstallationAsync(NotificationHubClient nhClient)
        {
            // Register some fake devices
            var gcmDeviceId     = Guid.NewGuid().ToString();
            var gcmInstallation = new Installation
            {
                InstallationId     = gcmDeviceId,
                Platform           = NotificationPlatform.Gcm,
                PushChannel        = gcmDeviceId,
                PushChannelExpired = false,
                Tags = new[] { "gcm" }
            };
            await nhClient.CreateOrUpdateInstallationAsync(gcmInstallation);

            while (true)
            {
                try
                {
                    var installationFromServer = await nhClient.GetInstallationAsync(gcmInstallation.InstallationId);

                    break;
                }
                catch (MessagingEntityNotFoundException)
                {
                    // Wait for installation to be created
                    await Task.Delay(1000);
                }
            }
            Console.WriteLine($"Created GCM installation {gcmInstallation.InstallationId}");
            await nhClient.DeleteInstallationAsync(gcmInstallation.InstallationId);

            while (true)
            {
                try
                {
                    var installationFromServer = await nhClient.GetInstallationAsync(gcmInstallation.InstallationId);

                    await Task.Delay(1000);
                }
                catch (MessagingEntityNotFoundException)
                {
                    Console.WriteLine($"Deleted GCM installation {gcmInstallation.InstallationId}");
                    break;
                }
            }
        }
示例#5
0
文件: Program.cs 项目: robisys/core
        private static async Task ViewAsync(string[] args)
        {
            var install = await _client.GetInstallationAsync(_viewId);

            var json = JsonConvert.SerializeObject(install, Formatting.Indented);

            Console.WriteLine(json);
        }
        public static async Task AddTag(string installationId, string newTag)
        {
            Installation installation = await notitifcationHubClient.GetInstallationAsync(installationId);

            if (installation.Tags == null)
            {
                installation.Tags = new List <string>();
            }
            installation.Tags.Add($"groupid:{newTag}");
            await notitifcationHubClient.CreateOrUpdateInstallationAsync(installation);
        }
示例#7
0
        private static async Task CreateInstallationAsync(NotificationHubClient nhClient)
        {
            // Register some fake devices
            //var fcmDeviceId = Guid.NewGuid().ToString();
            var InstallationId = Guid.NewGuid().ToString();
            //var apnsDeviceId = "djPUqVGjd9I:APA91bE7UyqaMr0IAICU7Wz5eV5qUEaZ2nEA7YkFcz6UqCHv-nIkUNDBAE4cZdjjG5bw8WnT2Psu7-eV4GMURVXtMGzI_yju2VRAzaTShW-9xwW2qy9D_SPIRsD8xVLalVklQgy_xara";
            //var apnsDeviceId = "96d6bb5d-412881d3-0046f5bf-5616bead-df6aa4ab-e57987bc-cc4b5c23-209dbb0c";
            //var apnsDeviceId = "96d6bb5d412881d30046f5bf5616beaddf6aa4abe57987bccc4b5c23209dbb0c";
            var apnsDeviceId = "c6a0cef6e29d7b38040c843d2296b7ecdf3d66f5d1b62e3cffb64a2edadd6d05";

            //var apnsDeviceId = "37303132443544373636453344373635444530313746363541353845304446423436314136423737394335374236433636303034304636343935454132304142";
            //c6a0cef6 e29d7b38 040c843d 2296b7ec df3d66f5 d1b62e3c ffb64a2e dadd6d05
            //c6a0cef6e29d7b38040c843d2296b7ecdf3d66f5d1b62e3cffb64a2edadd6d05
            //var apnsDeviceId = "c61cd9eea3d2def2";
            Console.WriteLine($"The Installation ID is {InstallationId}");
            Console.WriteLine($"The APNS device ID is {apnsDeviceId}");
            //var fcmInstallation = new Installation
            var apnsInstallation = new Installation
            {
                //InstallationId = fcmDeviceId,
                //InstallationId = apnsDeviceId,
                InstallationId = InstallationId,
                //Platform = NotificationPlatform.Fcm,
                Platform = NotificationPlatform.Apns,
                //PushChannel = fcmDeviceId,
                PushChannel        = apnsDeviceId,
                PushChannelExpired = false,
                //Tags = new[] { "fcm" }
                Tags = new[] { "apns" }
            };
            //await nhClient.CreateOrUpdateInstallationAsync(fcmInstallation);
            await nhClient.CreateOrUpdateInstallationAsync(apnsInstallation);

            while (true)
            {
                try
                {
                    //var installationFromServer = await nhClient.GetInstallationAsync(fcmInstallation.InstallationId);
                    var installationFromServer = await nhClient.GetInstallationAsync(apnsInstallation.InstallationId);

                    break;
                }
                catch (MessagingEntityNotFoundException)
                {
                    // Wait for installation to be created
                    await Task.Delay(1000);
                }
            }
            //Console.WriteLine($"Created FCM installation {fcmInstallation.InstallationId}");
            Console.WriteLine($"Created APNS installation {apnsInstallation.InstallationId}");
        }
        public async Task <List <string> > GetTagsByInstallationId(string Id)
        {
            try
            {
                // Return the installation for the specific ID.
                var installation = await hubClient.GetInstallationAsync(Id);

                return(installation.Tags as List <string>);
            }
            catch (MessagingException ex)
            {
                throw ex;
            }
        }
示例#9
0
        public async Task <IHttpActionResult> GetTagsByInstallationId(string id)
        {
            try
            {
                // Return the installation for the specific ID.
                var installation = await hubClient.GetInstallationAsync(id);

                return(Ok(installation.Tags));
            }
            catch
            {
                return(BadRequest());
            }
        }
        public async Task <HttpResponseMessage> Post(string InstallationId)
        {
            //Retrieve current user's email
            var customerEmail = ClaimsPrincipal.Current.FindFirst("emails").Value;

            // Create the notification hub client.
            NotificationHubClient hubClient = NotificationHubClient
                                              .CreateClientFromConnectionString(Settings.MS_NotificationHubConnectionString,
                                                                                Settings.MS_NotificationHubName);

            // Return the installation for the specific ID.
            var installation = await hubClient.GetInstallationAsync(InstallationId);

            if (installation.Tags == null || installation.Tags.Where(i => i == "uid:" + customerEmail).Count() == 0)
            {
                // Verify that the tags are a valid JSON array.
                var tags = JArray.Parse("[\"uid:" + customerEmail + "\"]");

                // Define a collection of PartialUpdateOperations. Note that
                // only one '/tags' path is permitted in a given collection.
                var updates = new List <PartialUpdateOperation>();

                // Add a update operation for the tag.
                updates.Add(new PartialUpdateOperation
                {
                    Operation = UpdateOperationType.Add,
                    Path      = "/tags",
                    Value     = tags.ToString()
                });

                try
                {
                    // Add the requested tag to the installation.
                    await hubClient.PatchInstallationAsync(InstallationId, updates);
                }
                catch (MessagingException)
                {
                    // When an error occurs, return a failure status.
                    return(new HttpResponseMessage(HttpStatusCode.InternalServerError));
                }
            }

            // Return success status.
            return(new HttpResponseMessage(HttpStatusCode.OK));
        }
示例#11
0
        public async Task <HttpResponseMessage> Delete(string InstallationId)
        {
            // Create the notification hub client.
            NotificationHubClient hubClient = NotificationHubClient
                                              .CreateClientFromConnectionString(Settings.MS_NotificationHubConnectionString,
                                                                                Settings.MS_NotificationHubName);

            // Return the installation for the specific ID.
            var installation = await hubClient.GetInstallationAsync(InstallationId);

            // Define a collection of PartialUpdateOperations. Note that
            // only one '/tags' path is permitted in a given collection.
            var updates = new List <PartialUpdateOperation>();

            if (installation.Tags != null)
            {
                foreach (var removeTag in installation.Tags)
                {
                    // Add a update operation for the tag.
                    updates.Add(new PartialUpdateOperation
                    {
                        Operation = UpdateOperationType.Remove,
                        Path      = "/tags/" + removeTag
                    });
                }

                try
                {
                    // Add the requested tag to the installation.
                    await hubClient.PatchInstallationAsync(InstallationId, updates);
                }
                catch (MessagingException)
                {
                    // When an error occurs, return a failure status.
                    return(new HttpResponseMessage(HttpStatusCode.InternalServerError));
                }
            }

            // Return success status.
            return(new HttpResponseMessage(HttpStatusCode.OK));
        }
示例#12
0
 public async Task <Installation> GetRegistration(string installationId)
 {
     return(await hub.GetInstallationAsync(installationId));
 }