From b0c504347a2b14ff851df825a4348e2214e324e1 Mon Sep 17 00:00:00 2001 From: Paul Schneider Date: Thu, 21 Jul 2016 12:25:01 +0200 Subject: [PATCH] use SimpleMethodPostJson to send the GCM --- Yavsc/Controllers/CommandController.cs | 12 ++-- Yavsc/Helpers/GoogleHelpers.cs | 92 ++++++++++++++------------ 2 files changed, 58 insertions(+), 46 deletions(-) diff --git a/Yavsc/Controllers/CommandController.cs b/Yavsc/Controllers/CommandController.cs index e14801ac..7e926124 100644 --- a/Yavsc/Controllers/CommandController.cs +++ b/Yavsc/Controllers/CommandController.cs @@ -148,11 +148,13 @@ namespace Yavsc.Controllers if (pro.AcceptNotifications && pro.AcceptPublicContact) { - if (pro.Performer.Devices.Count > 0) - grep = await _GCMSender.NotifyAsync(_googleSettings, - command.PerformerProfile.Performer.Devices.Select(d => d.GCMRegistrationId), - yaev - ); + if (pro.Performer.Devices.Count > 0) { + var regids = command.PerformerProfile.Performer + .Devices.Select(d => d.GCMRegistrationId); + var sregids = string.Join(",",regids); + _logger.LogWarning($"ApiKey: {_googleSettings.ApiKey} {sregids}"); + grep = await _GCMSender.NotifyAsync(_googleSettings,regids,yaev); + } // TODO setup a profile choice to allow notifications // both on mailbox and mobile // if (grep==null || grep.success<=0 || grep.failure>0) diff --git a/Yavsc/Helpers/GoogleHelpers.cs b/Yavsc/Helpers/GoogleHelpers.cs index eca1e91b..e2fc82fc 100644 --- a/Yavsc/Helpers/GoogleHelpers.cs +++ b/Yavsc/Helpers/GoogleHelpers.cs @@ -36,54 +36,64 @@ namespace Yavsc.Helpers /// Google helpers. /// public static class GoogleHelpers - { + { - /// - /// Notifies the event. - /// - /// The event. - /// Evpub. - public static async Task NotifyEvent - (this HttpClient channel, GoogleAuthSettings googleSettings, CircleEvent evpub) { - // ASSERT ModelState.IsValid implies evpub.Circles != null - //send a MessageWithPayload to circle members - // receive MessageWithPayloadResponse - // "https://gcm-http.googleapis.com/gcm/send" + /// + /// Notifies the event. + /// + /// The event. + /// Evpub. + public static async Task NotifyEvent + (this HttpClient channel, GoogleAuthSettings googleSettings, CircleEvent evpub) + { + // ASSERT ModelState.IsValid implies evpub.Circles != null + //send a MessageWithPayload to circle members + // receive MessageWithPayloadResponse + // "https://gcm-http.googleapis.com/gcm/send" - var regids = new List (); + var regids = new List(); foreach (var c in evpub.Circles) - foreach (var u in c.Members) { - regids.AddRange (u.Member.Devices.Select(d=>d.GCMRegistrationId)); - } - if (regids.Count>0) return null; - var request = new HttpRequestMessage(HttpMethod.Get, Constants.GCMNotificationUrl); - request.Headers.Authorization = new AuthenticationHeaderValue("Key", googleSettings.ApiKey); - var msg = new MessageWithPayload () { + foreach (var u in c.Members) + { + regids.AddRange(u.Member.Devices.Select(d => d.GCMRegistrationId)); + } + if (regids.Count > 0) return null; + var request = new HttpRequestMessage(HttpMethod.Get, Constants.GCMNotificationUrl); + request.Headers.Authorization = new AuthenticationHeaderValue("key", googleSettings.ApiKey); + var msg = new MessageWithPayload() + { notification = new Notification() { title = evpub.Title, body = evpub.Description, icon = "event" }, - data = evpub , registration_ids = regids.ToArray() }; + data = evpub, + registration_ids = regids.ToArray() + }; var response = await channel.SendAsync(request); var payload = JObject.Parse(await response.Content.ReadAsStringAsync()); - return payload.Value(); - } + return payload.Value(); + } - public static async Task NotifyEvent - (this HttpClient channel, GoogleAuthSettings googleSettings, IEnumerable regids, Event ev) + public static MessageWithPayloadResponse NotifyEvent + (this GoogleAuthSettings googleSettings, IEnumerable regids, Event ev) where Event : YaEvent - { - if (regids == null) - throw new NotImplementedException ("Notify & No GCM reg ids"); - var request = new HttpRequestMessage(HttpMethod.Get, Constants.GCMNotificationUrl); - request.Headers.Authorization = new AuthenticationHeaderValue("Key", googleSettings.ApiKey); - var msg = new MessageWithPayload () { - notification = new Notification() { title = ev.Title, - body = ev.Description + ev.Comment==null? - "":"("+ev.Comment+")", icon = "icon" }, - data = ev, registration_ids = regids.ToArray() }; + { + var msg = new MessageWithPayload() + { + notification = new Notification() + { + title = ev.Title, + body = ev.Description + ev.Comment == null ? + "" : "(" + ev.Comment + ")", + icon = "icon" + }, + data = ev, + registration_ids = regids.ToArray() + }; - var response = await channel.SendAsync(request); - var payload = JObject.Parse(await response.Content.ReadAsStringAsync()); - return payload.Value(); - } + if (regids == null) + throw new NotImplementedException("Notify & No GCM reg ids"); + using (var m = new SimpleJsonPostMethod("https://gcm-http.googleapis.com/gcm/send",$"key={googleSettings.ApiKey}")) { + return m.Invoke(msg); + } + } public static async Task GetCredentialForGoogleApiAsync(this UserManager userManager, ApplicationDbContext context, string uid) { var user = await userManager.FindByIdAsync(uid); @@ -91,11 +101,11 @@ namespace Yavsc.Helpers x => x.UserId == uid ).ProviderKey; if (string.IsNullOrEmpty(googleId)) - throw new InvalidOperationException("No Google login"); + throw new InvalidOperationException("No Google login"); var token = await context.GetTokensAsync(googleId); return new UserCredential(uid, token); } - } + } }