private static async Task CreateAndDeleteRegistrationAsync(NotificationHubClient nhClient) { var registrationId = await nhClient.CreateRegistrationIdAsync(); var registrationDescr = await nhClient.CreateGcmNativeRegistrationAsync(registrationId); Console.WriteLine($"Created GCM registration {registrationDescr.GcmRegistrationId}"); var allRegistrations = await nhClient.GetAllRegistrationsAsync(1000); foreach (var regFromServer in allRegistrations) { if (regFromServer.RegistrationId == registrationDescr.RegistrationId) { Console.WriteLine($"Found GCM registration {registrationDescr.GcmRegistrationId}"); break; } } //registrationDescr = await nhClient.GetRegistrationAsync<GcmRegistrationDescription>(registrationId); //Console.WriteLine($"Retrieved GCM registration {registrationDescr.GcmRegistrationId}"); await nhClient.DeleteRegistrationAsync(registrationDescr); Console.WriteLine($"Deleted GCM registration {registrationDescr.GcmRegistrationId}"); }
public static async Task <bool> SendNotificationAsync(string message, string idNotificacao, bool broadcast = false) { string[] userTag = new string[1]; // Obs importantíssima: O username tá na primeira parte do id da // notificação antes do ":"(dois pontos). Para mandar broadcast é só //enviar o username em branco userTag[0] = string.Empty; string defaultFullSharedAccessSignature = util.configTools.getConfig("hubnotificacao"); string hubName = util.configTools.getConfig("hubname"); string handle = idNotificacao;//hubName; NotificationHubClient _hub = NotificationHubClient.CreateClientFromConnectionString(defaultFullSharedAccessSignature, hubName); //await _hub.DeleteRegistrationAsync(idNotificacao); //Excluindo registros var registrations = await _hub.GetRegistrationsByChannelAsync(handle, 100); foreach (RegistrationDescription xregistration in registrations) { try { await _hub.DeleteRegistrationAsync(xregistration); } catch (Exception ex) { string sm = ex.Message; } } if (!broadcast) { string to_tag = idNotificacao.Split(':')[0]; userTag[0] = "username:"******"{ \"data\" : {\"message\":\"" + message + "\"}}"; outcome = await _hub.SendGcmNativeNotificationAsync(notif, userTag); if (outcome != null) { if (!((outcome.State == NotificationOutcomeState.Abandoned) || (outcome.State == NotificationOutcomeState.Unknown))) { return(true); } } return(false); }
public async Task <RegistrationDescription> Register(string platform, string registerId, string key) { if (!string.IsNullOrEmpty(key)) { await _notificationHubClient.DeleteRegistrationAsync(key); } switch (platform) { case "android": return(await _notificationHubClient.CreateGcmNativeRegistrationAsync(registerId)); case "ios": return(await _notificationHubClient.CreateAppleNativeRegistrationAsync(registerId)); default: throw new Exception("Not supported platform."); } }
public Task RegisterGoogle(Guid account_id, string deviceToken) { return(Task.Run(async delegate() { try { NotificationHubClient hubClient = this.HubClient; Account account = this.API.Direct.Accounts.GetById(account_id); string previousRegistrationID = account.push_google; if (!string.IsNullOrEmpty(previousRegistrationID)) { if (await hubClient.RegistrationExistsAsync(previousRegistrationID)) { await hubClient.DeleteRegistrationAsync(previousRegistrationID); } } string accountTag = string.Format(PushAssumptions.TARGET_ACCOUNT_FORMAT, account_id.ToString().ToLower()); var registrations = await hubClient.GetRegistrationsByTagAsync(accountTag, 100); foreach (RegistrationDescription registration in registrations) { if (registration.Tags.Contains("droid")) { await hubClient.DeleteRegistrationAsync(registration); } } List <string> tags = new List <string>(); tags.Add(accountTag); tags.Add("droid"); GcmRegistrationDescription newRegistration = await hubClient.CreateGcmNativeRegistrationAsync(deviceToken, tags); if (newRegistration != null) { this.API.Direct.Accounts.UpdatePushTokenGoogle(account.account_id, newRegistration.RegistrationId); } } catch (Exception ex) { base.IFoundation.LogError(ex); } })); }
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 Android 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.CreateGcmNativeRegistrationAsync(clientRegistrationId, tags)); }