public async Task <HttpResponseMessage> Register(Registration registration) { // You'd also want to verify that the user in the registration is allowed to register for that tag // Get notification hubs registrations for that "username" tag. // Usually, you should get only 1, if we get more than one (for example, during development you make mistakes), delete all others // The "100" parameter just represents how many registrations to return var registrationsForUser = await hub.GetRegistrationsByTagAsync(registration.Username, 100); bool firstRegistration = true; bool existingRegistration = false; foreach (var reg in registrationsForUser) { if (firstRegistration) { // Update the registration with the incoming channel var winReg = reg as WindowsRegistrationDescription; winReg.ChannelUri = new Uri(registration.Channel); winReg.Tags = new HashSet <string> { registration.Username }; await hub.UpdateRegistrationAsync(winReg); firstRegistration = false; existingRegistration = true; } else { // Delete other registrations for that user await hub.DeleteRegistrationAsync(reg); } } // Register with the notification hub and listen to the "username" tag, if this is a new registration if (!existingRegistration) { switch (registration.Platform) { case "WindowsStore": await hub.CreateWindowsNativeRegistrationAsync(registration.Channel, new List <string> { registration.Username }); break; default: // If the platform isn't supported, return Bad Request return(Request.CreateErrorResponse(HttpStatusCode.BadRequest, registration.Platform + " platform is not supported.")); } } return(Request.CreateResponse(HttpStatusCode.OK)); }
// POST api/RegisterDevice public void Post([FromBody] string value) { var connectionString = ConfigurationManager.AppSettings["NotificationHubConnectionString"]; var notificationHubName = ConfigurationManager.AppSettings["NotificationHubName"]; NotificationHubClient hub = NotificationHubClient.CreateClientFromConnectionString(connectionString, notificationHubName); // Decode from Base64 string Base64DecodedChannelUri = Encoding.UTF8.GetString(Convert.FromBase64String(value)); hub.DeleteRegistrationsByChannelAsync(Base64DecodedChannelUri).Wait(); hub.CreateWindowsNativeRegistrationAsync(Base64DecodedChannelUri); }
private dynamic GetNativeRegistration(DeviceRegistration registration) { switch (registration.Type) { case NotificationType.FCM: return(_hub.CreateFcmNativeRegistrationAsync(registration.Handle).Result); case NotificationType.WND: return(_hub.CreateWindowsNativeRegistrationAsync(registration.Handle).Result); default: return(_hub.CreateFcmNativeRegistrationAsync(registration.Handle).Result); } }
private async Task <string> createWindowsRegistration(Registration clientRegistration, ISet <string> tags) { string newId = string.Empty; // Register either a standard toast notification using templates or a raw noative notification // depending on what is requested buy the client. switch (clientRegistration.Type) { case "toast": var template = @"<toast> <visual> <binding template=""ToastText04""> <text id=""1"">{'Northwind data was ' + $(operation) + '...'}</text> <text id=""2"">{'Feed: ' + $(feed)}</text> <text id=""3"">{'Entity: ' + $(entity)}</text> </binding> </visual> </toast>"; // Register for the templated toast notification, and return the ID. newId = clientRegistration.RegistrationId = (await hubClient.CreateWindowsTemplateRegistrationAsync( clientRegistration.ChannelUri, template, tags)).RegistrationId; break; case "auto-update": // Register for the raw notification used by auto-updates, and return the ID. newId = clientRegistration.RegistrationId = (await hubClient.CreateWindowsNativeRegistrationAsync( clientRegistration.ChannelUri, tags)).RegistrationId; break; default: throw new DataServiceException("Unexpected notification type."); } return(newId); }
public async Task Register(string email, int platformId, string pushRegistrationId) { try { var registrationsCollection = await _hubClient.GetRegistrationsByTagAsync(email, 100); var registrations = registrationsCollection.ToList(); if (registrations != null && registrations.Count > 0) { foreach (var register in registrations) { await _hubClient.DeleteRegistrationAsync(register); } } switch ((EPlatform)platformId) { case EPlatform.ANDROID: await _hubClient.CreateGcmNativeRegistrationAsync(pushRegistrationId, new string[] { email }); break; case EPlatform.WP: await _hubClient.CreateWindowsNativeRegistrationAsync(pushRegistrationId, new string[] { email }); break; } } catch (Exception exception) { ElmahUtils.LogToElmah(exception); } }
public async Task <RegistrationDescription> Post([FromBody] JObject registrationCall) { // Get the registration info that we need from the request. var platform = registrationCall["platform"].ToString(); var installationId = registrationCall["instId"].ToString(); var channelUri = registrationCall["channelUri"] != null ? registrationCall["channelUri"].ToString() : null; var deviceToken = registrationCall["deviceToken"] != null ? registrationCall["deviceToken"].ToString() : null; var registrationId = registrationCall["registrationId"] != null ? registrationCall["registrationId"].ToString() : null; var userName = HttpContext.Current.User.Identity.Name; // Get registrations for the current installation ID. var regsForInstId = await _hubClient.GetRegistrationsByTagAsync(installationId, 100); bool updated = false; bool firstRegistration = true; RegistrationDescription registration = null; // Check for existing registrations. foreach (var registrationDescription in regsForInstId) { if (firstRegistration) { // Update the tags. registrationDescription.Tags = new HashSet <string> { installationId, userName }; // We need to handle each platform separately. switch (platform) { case "windows": var winReg = registrationDescription as WindowsRegistrationDescription; if (winReg != null) { if (channelUri != null) { winReg.ChannelUri = new Uri(channelUri); } registration = await _hubClient.UpdateRegistrationAsync(winReg); } break; case "android": var gcmReg = registrationDescription as GcmRegistrationDescription; if (gcmReg != null) { gcmReg.GcmRegistrationId = registrationId; registration = await _hubClient.UpdateRegistrationAsync(gcmReg); } break; case "ios": var iosReg = registrationDescription as AppleRegistrationDescription; if (iosReg != null) { iosReg.DeviceToken = deviceToken; registration = await _hubClient.UpdateRegistrationAsync(iosReg); } break; } updated = true; firstRegistration = false; } else { // We shouldn't have any extra registrations; delete if we do. await _hubClient.DeleteRegistrationAsync(registrationDescription); } } // Create a new registration. if (!updated) { switch (platform) { case "windows": registration = await _hubClient.CreateWindowsNativeRegistrationAsync(channelUri, new[] { installationId, userName }); break; case "android": registration = await _hubClient.CreateGcmNativeRegistrationAsync(registrationId, new[] { installationId, userName }); break; case "ios": registration = await _hubClient.CreateAppleNativeRegistrationAsync(deviceToken, new[] { installationId, userName }); break; } } // Send out a test notification. SendNotification(string.Format("Test notification for {0}", userName), userName); return(registration); }
/// <summary> /// Create a registration for an app instance /// </summary> /// <param name="hubClient">Notification hub client for this app</param> /// <param name="clientRegistrationId">Client registration id from Windows for this app instance</param> /// <param name="tags">Registration tags that describe the user and the app</param> /// <returns>Hub registration id</returns> protected override async Task <RegistrationDescription> CreateRegistrationAsync(NotificationHubClient hubClient, string clientRegistrationId, IEnumerable <string> tags) { return(await hubClient.CreateWindowsNativeRegistrationAsync(clientRegistrationId, tags)); }