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 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 static async Task RegisterDevice(DeviceInstallation deviceUpdate) { Installation installation = new Installation(); installation.InstallationId = deviceUpdate.InstallationId; installation.PushChannel = deviceUpdate.PushChannel; switch (deviceUpdate.Platform) { case "apns": installation.Platform = NotificationPlatform.Apns; break; case "gcm": installation.Platform = NotificationPlatform.Gcm; break; default: throw new Exception("Invalid Channel"); } installation.Tags = new List <string>(); await notitifcationHubClient.CreateOrUpdateInstallationAsync(installation); }
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 RegisterAsync(RegistrationRequest request) { Installation installation = new Installation { InstallationId = request.trackingNumber, PushChannel = request.trackingNumber, Tags = new List <string>() { request.trackingNumber }, Platform = NotificationPlatform.Fcm }; await _hub.CreateOrUpdateInstallationAsync(installation); return; }
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; } } }
private async void InitNotificationsAsync() { var channel = await PushNotificationChannelManager.CreatePushNotificationChannelForApplicationAsync(); var hub = new NotificationHubClient(Secrets.HubConnectionString, Secrets.HubName); await hub.CreateOrUpdateInstallationAsync(new Installation { Platform = NotificationPlatform.Wns, InstallationId = "myid", PushChannel = channel.Uri }); var dialog = new MessageDialog("Registration successful"); dialog.Commands.Add(new UICommand("OK")); await dialog.ShowAsync(); }
public async Task <bool> CreateOrUpdateInstallationAsync(DeviceInstallation deviceInstallation, CancellationToken token) { if (string.IsNullOrWhiteSpace(deviceInstallation?.InstallationId) || string.IsNullOrWhiteSpace(deviceInstallation?.Platform) || string.IsNullOrWhiteSpace(deviceInstallation?.PushChannel)) { return(false); } var templates = deviceInstallation.Templates .ToDictionary( i => i.Key, i => new InstallationTemplate { Body = i.Value.Body }); var installation = new Installation() { InstallationId = deviceInstallation.InstallationId, PushChannel = deviceInstallation.PushChannel, Tags = deviceInstallation.Tags, Templates = templates }; if (_installationPlatform.TryGetValue(deviceInstallation.Platform, out var platform)) { installation.Platform = platform; } else { return(false); } try { await _hub.CreateOrUpdateInstallationAsync(installation, token); } catch { return(false); } return(true); }
public async Task <Boolean> RegisterMobileDevice(MobileDeviceInstallationNotificationHub deviceUpdate) { Dictionary <string, InstallationTemplate> templates = new Dictionary <string, InstallationTemplate>(); foreach (var t in deviceUpdate.Templates) { templates.Add(t.Key, new InstallationTemplate { Body = t.Value.Body }); } Installation installation = new Installation() { InstallationId = deviceUpdate.InstallationId, PushChannel = deviceUpdate.PushChannel, Tags = deviceUpdate.Tags, Templates = templates }; switch (deviceUpdate.Platform) { case "apns": installation.Platform = NotificationPlatform.Apns; break; case "gcm": installation.Platform = NotificationPlatform.Gcm; break; default: throw new HttpResponseException(HttpStatusCode.BadRequest); } installation.Tags = new List <string>(deviceUpdate.Tags); try { await _hubClient.CreateOrUpdateInstallationAsync(installation); return(true); } catch (Exception ex) { throw ex; } }
public async Task <HttpResponseMessage> CreateOrUpdateInstallation([FromBody] DeviceInstallation deviceUpdate) { //await CleanupInstallations(); var installation = new Installation { InstallationId = deviceUpdate.InstallationId, PushChannel = deviceUpdate.PushChannel, Tags = deviceUpdate.Tags, }; switch (deviceUpdate.Platform) { case "Mpns": installation.Platform = NotificationPlatform.Mpns; break; case "Wns": installation.Platform = NotificationPlatform.Wns; break; case "Apns": installation.Platform = NotificationPlatform.Apns; break; case "Fcm": installation.Platform = NotificationPlatform.Fcm; break; default: throw new Exception("Invalid platform request"); } // In the backend we can control if a user is allowed to add tags //installation.Tags = new List<string>(deviceUpdate.Tags); //installation.Tags.Add("username:" + username); await _hub.CreateOrUpdateInstallationAsync(installation); return(new HttpResponseMessage(HttpStatusCode.OK)); }
public async Task <bool> CreateOrUpdateInstallationAsync(DeviceInstallation deviceInstallation, CancellationToken token) { if (string.IsNullOrWhiteSpace(deviceInstallation?.InstallationId) || string.IsNullOrWhiteSpace(deviceInstallation?.Platform) || string.IsNullOrWhiteSpace(deviceInstallation?.PushChannel) || string.IsNullOrWhiteSpace(deviceInstallation?.UserId)) { return(false); } var installation = new Installation() { InstallationId = deviceInstallation.InstallationId, PushChannel = deviceInstallation.PushChannel, Tags = deviceInstallation.Tags, UserId = deviceInstallation.UserId }; if (_installationPlatform.TryGetValue(deviceInstallation.Platform, out var platform)) { installation.Platform = platform; } else { return(false); } try { await _hub.CreateOrUpdateInstallationAsync(installation, token); _logger.LogInformation($"Device Id {installation.InstallationId} User Id {installation.UserId} registered"); } catch (Exception e) { _logger.LogError(e, "Unexpected error device installation"); return(false); } return(true); }
public static async Task <bool> Register(Guid userId, PushNotificationPlatform platform, string pnsToken, string template, ILogger logger) { string connectionString = Environment.GetEnvironmentVariable("SOLENE_NOTIFICATION_CONNECTION_STRING", EnvironmentVariableTarget.Process); NotificationHubClient hub = NotificationHubClient.CreateClientFromConnectionString(connectionString, "solene-mobile-app"); try { await hub.CreateOrUpdateInstallationAsync(new Installation { InstallationId = ToInstallId(userId, platform), Platform = (NotificationPlatform)platform, PushChannel = pnsToken, Tags = new[] { userId.ToString("N") }, Templates = new Dictionary <string, InstallationTemplate> { { "questionTemplate", new InstallationTemplate { Body = template, Headers = platform == PushNotificationPlatform.Windows ? new Dictionary <string, string> { { "X-WNS-Type", "wns/toast" } } : null, } } } }); logger.LogInformation($"Registered user {ToInstallId(userId, platform)} for push notifications."); return(true); } catch (Exception ex) { logger.LogError(ex, $"Failed to register push notifications for {userId}.", null); return(false); } }
public async Task <bool> CreateOrUpdateInstallationAsync(DeviceInstallation deviceInstallation, CancellationToken cancellationToken) { if (string.IsNullOrWhiteSpace(deviceInstallation?.InstallationId) || string.IsNullOrWhiteSpace(deviceInstallation?.Platform) || string.IsNullOrWhiteSpace(deviceInstallation?.PushChannel)) { return(false); } var installation = new Installation() { InstallationId = deviceInstallation.InstallationId, PushChannel = deviceInstallation.PushChannel, Tags = deviceInstallation.Tags }; if (_installationPlatform.TryGetValue(deviceInstallation.Platform, out var platform)) { installation.Platform = platform; } else { return(false); } try { await _hub.CreateOrUpdateInstallationAsync(installation, cancellationToken); } catch { return(false); } return(true); }
public async Task CreateOrUpdateRegistrationAsync(Device device) { if (string.IsNullOrWhiteSpace(device.PushToken)) { return; } var installation = new Installation { InstallationId = device.Id.ToString(), PushChannel = device.PushToken, Templates = new Dictionary <string, InstallationTemplate>() }; installation.Tags = new List <string> { $"userId:{device.UserId}" }; if (!string.IsNullOrWhiteSpace(device.Identifier)) { installation.Tags.Add("deviceIdentifier:" + device.Identifier); } string payloadTemplate = null, messageTemplate = null, badgeMessageTemplate = null; switch (device.Type) { case Enums.DeviceType.Android: payloadTemplate = "{\"data\":{\"type\":\"#(type)\",\"payload\":\"$(payload)\"}}"; messageTemplate = "{\"data\":{\"type\":\"#(type)\"}," + "\"notification\":{\"title\":\"$(title)\",\"body\":\"$(message)\"}}"; installation.Platform = NotificationPlatform.Gcm; break; case Enums.DeviceType.iOS: payloadTemplate = "{\"data\":{\"type\":\"#(type)\",\"payload\":\"$(payload)\"}," + "\"aps\":{\"alert\":null,\"badge\":null,\"content-available\":1}}"; messageTemplate = "{\"data\":{\"type\":\"#(type)\"}," + "\"aps\":{\"alert\":\"$(message)\",\"badge\":null,\"content-available\":1}}"; badgeMessageTemplate = "{\"data\":{\"type\":\"#(type)\"}," + "\"aps\":{\"alert\":\"$(message)\",\"badge\":\"#(badge)\",\"content-available\":1}}"; installation.Platform = NotificationPlatform.Apns; break; case Enums.DeviceType.AndroidAmazon: payloadTemplate = "{\"data\":{\"type\":\"#(type)\",\"payload\":\"$(payload)\"}}"; messageTemplate = "{\"data\":{\"type\":\"#(type)\",\"message\":\"$(message)\"}}"; installation.Platform = NotificationPlatform.Adm; break; default: break; } BuildInstallationTemplate(installation, "payload", payloadTemplate, device.UserId, device.Identifier); BuildInstallationTemplate(installation, "message", messageTemplate, device.UserId, device.Identifier); BuildInstallationTemplate(installation, "badgeMessage", badgeMessageTemplate ?? messageTemplate, device.UserId, device.Identifier); await _client.CreateOrUpdateInstallationAsync(installation); }
public async Task CreateOrUpdateRegistrationAsync(string pushToken, string deviceId, string userId, string identifier, DeviceType type) { if (string.IsNullOrWhiteSpace(pushToken)) { return; } var installation = new Installation { InstallationId = deviceId, PushChannel = pushToken, Templates = new Dictionary <string, InstallationTemplate>() }; installation.Tags = new List <string> { $"userId:{userId}" }; if (!string.IsNullOrWhiteSpace(identifier)) { installation.Tags.Add("deviceIdentifier:" + identifier); } string payloadTemplate = null, messageTemplate = null, badgeMessageTemplate = null; switch (type) { case DeviceType.Android: payloadTemplate = "{\"data\":{\"data\":{\"type\":\"#(type)\",\"payload\":\"$(payload)\"}}}"; messageTemplate = "{\"data\":{\"data\":{\"type\":\"#(type)\"}," + "\"notification\":{\"title\":\"$(title)\",\"body\":\"$(message)\"}}}"; installation.Platform = NotificationPlatform.Fcm; break; case DeviceType.iOS: payloadTemplate = "{\"data\":{\"type\":\"#(type)\",\"payload\":\"$(payload)\"}," + "\"aps\":{\"alert\":null,\"badge\":null,\"content-available\":1}}"; messageTemplate = "{\"data\":{\"type\":\"#(type)\"}," + "\"aps\":{\"alert\":\"$(message)\",\"badge\":null,\"content-available\":1}}"; badgeMessageTemplate = "{\"data\":{\"type\":\"#(type)\"}," + "\"aps\":{\"alert\":\"$(message)\",\"badge\":\"#(badge)\",\"content-available\":1}}"; installation.Platform = NotificationPlatform.Apns; break; case DeviceType.AndroidAmazon: payloadTemplate = "{\"data\":{\"type\":\"#(type)\",\"payload\":\"$(payload)\"}}"; messageTemplate = "{\"data\":{\"type\":\"#(type)\",\"message\":\"$(message)\"}}"; installation.Platform = NotificationPlatform.Adm; break; default: break; } BuildInstallationTemplate(installation, "payload", payloadTemplate, userId, identifier); BuildInstallationTemplate(installation, "message", messageTemplate, userId, identifier); BuildInstallationTemplate(installation, "badgeMessage", badgeMessageTemplate ?? messageTemplate, userId, identifier); await _client.CreateOrUpdateInstallationAsync(installation); if (InstallationDeviceEntity.IsInstallationDeviceId(deviceId)) { await _installationDeviceRepository.UpsertAsync(new InstallationDeviceEntity(deviceId)); } }
private static async Task RegisterAsync(string[] args) { IDictionary <Guid, List <OrganizationUser> > orgUsersDict; using (var connection = new SqlConnection(SqlConnectionString)) { var results = await connection.QueryAsync <OrganizationUser>( "SELECT * FROM [dbo].[OrganizationUser] WHERE [Status] = 2 AND [UserId] IS NOT NULL", commandType : CommandType.Text); orgUsersDict = results .GroupBy(ou => ou.UserId.Value) .ToDictionary(ou => ou.Key, ou => ou.ToList()); } IEnumerable <Device> devices; using (var connection = new SqlConnection(SqlConnectionString)) { devices = await connection.QueryAsync <Device>( "SELECT * FROM [dbo].[Device] WHERE [PushToken] IS NOT NULL AND [RevisionDate] > @StartDate", new { StartDate = _startDate }, commandType : CommandType.Text); } var i = 0; foreach (var device in devices) { i++; var installation = new Installation { InstallationId = device.Id.ToString(), PushChannel = device.PushToken, Templates = new Dictionary <string, InstallationTemplate>() }; installation.Tags = new List <string> { $"userId:{device.UserId}" }; if (!string.IsNullOrWhiteSpace(device.Identifier)) { installation.Tags.Add("deviceIdentifier:" + device.Identifier); } if (orgUsersDict.ContainsKey(device.UserId)) { foreach (var orgUser in orgUsersDict[device.UserId]) { installation.Tags.Add("organizationId:" + orgUser.OrganizationId); } } string payloadTemplate = null, messageTemplate = null, badgeMessageTemplate = null; switch (device.Type) { case DeviceType.Android: payloadTemplate = "{\"data\":{\"type\":\"#(type)\",\"payload\":\"$(payload)\"}}"; messageTemplate = "{\"data\":{\"type\":\"#(type)\"}," + "\"notification\":{\"title\":\"$(title)\",\"body\":\"$(message)\"}}"; installation.Platform = NotificationPlatform.Gcm; break; case DeviceType.iOS: payloadTemplate = "{\"data\":{\"type\":\"#(type)\",\"payload\":\"$(payload)\"}," + "\"aps\":{\"alert\":null,\"badge\":null,\"content-available\":1}}"; messageTemplate = "{\"data\":{\"type\":\"#(type)\"}," + "\"aps\":{\"alert\":\"$(message)\",\"badge\":null,\"content-available\":1}}"; badgeMessageTemplate = "{\"data\":{\"type\":\"#(type)\"}," + "\"aps\":{\"alert\":\"$(message)\",\"badge\":\"#(badge)\",\"content-available\":1}}"; installation.Platform = NotificationPlatform.Apns; break; case DeviceType.AndroidAmazon: payloadTemplate = "{\"data\":{\"type\":\"#(type)\",\"payload\":\"$(payload)\"}}"; messageTemplate = "{\"data\":{\"type\":\"#(type)\",\"message\":\"$(message)\"}}"; installation.Platform = NotificationPlatform.Adm; break; default: break; } BuildInstallationTemplate(installation, "payload", payloadTemplate, device.UserId, device.Identifier); BuildInstallationTemplate(installation, "message", messageTemplate, device.UserId, device.Identifier); BuildInstallationTemplate(installation, "badgeMessage", badgeMessageTemplate ?? messageTemplate, device.UserId, device.Identifier); await _client.CreateOrUpdateInstallationAsync(installation); Console.WriteLine("Added install #" + i + " (" + installation.InstallationId + ")"); } }