use SimpleMethodPostJson to send the GCM

This commit is contained in:
2016-07-21 12:25:01 +02:00
parent c2e88aec17
commit b0c504347a
2 changed files with 58 additions and 46 deletions

View File

@ -148,11 +148,13 @@ namespace Yavsc.Controllers
if (pro.AcceptNotifications if (pro.AcceptNotifications
&& pro.AcceptPublicContact) && pro.AcceptPublicContact)
{ {
if (pro.Performer.Devices.Count > 0) if (pro.Performer.Devices.Count > 0) {
grep = await _GCMSender.NotifyAsync(_googleSettings, var regids = command.PerformerProfile.Performer
command.PerformerProfile.Performer.Devices.Select(d => d.GCMRegistrationId), .Devices.Select(d => d.GCMRegistrationId);
yaev 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 // TODO setup a profile choice to allow notifications
// both on mailbox and mobile // both on mailbox and mobile
// if (grep==null || grep.success<=0 || grep.failure>0) // if (grep==null || grep.success<=0 || grep.failure>0)

View File

@ -36,54 +36,64 @@ namespace Yavsc.Helpers
/// Google helpers. /// Google helpers.
/// </summary> /// </summary>
public static class GoogleHelpers public static class GoogleHelpers
{ {
/// <summary> /// <summary>
/// Notifies the event. /// Notifies the event.
/// </summary> /// </summary>
/// <returns>The event.</returns> /// <returns>The event.</returns>
/// <param name="evpub">Evpub.</param> /// <param name="evpub">Evpub.</param>
public static async Task<MessageWithPayloadResponse> NotifyEvent public static async Task<MessageWithPayloadResponse> NotifyEvent
(this HttpClient channel, GoogleAuthSettings googleSettings, CircleEvent evpub) { (this HttpClient channel, GoogleAuthSettings googleSettings, CircleEvent evpub)
// ASSERT ModelState.IsValid implies evpub.Circles != null {
//send a MessageWithPayload<YaEvent> to circle members // ASSERT ModelState.IsValid implies evpub.Circles != null
// receive MessageWithPayloadResponse //send a MessageWithPayload<YaEvent> to circle members
// "https://gcm-http.googleapis.com/gcm/send" // receive MessageWithPayloadResponse
// "https://gcm-http.googleapis.com/gcm/send"
var regids = new List<string> (); var regids = new List<string>();
foreach (var c in evpub.Circles) foreach (var c in evpub.Circles)
foreach (var u in c.Members) { foreach (var u in c.Members)
regids.AddRange (u.Member.Devices.Select(d=>d.GCMRegistrationId)); {
} regids.AddRange(u.Member.Devices.Select(d => d.GCMRegistrationId));
if (regids.Count>0) return null; }
var request = new HttpRequestMessage(HttpMethod.Get, Constants.GCMNotificationUrl); if (regids.Count > 0) return null;
request.Headers.Authorization = new AuthenticationHeaderValue("Key", googleSettings.ApiKey); var request = new HttpRequestMessage(HttpMethod.Get, Constants.GCMNotificationUrl);
var msg = new MessageWithPayload<YaEvent> () { request.Headers.Authorization = new AuthenticationHeaderValue("key", googleSettings.ApiKey);
var msg = new MessageWithPayload<YaEvent>()
{
notification = new Notification() { title = evpub.Title, body = evpub.Description, icon = "event" }, 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 response = await channel.SendAsync(request);
var payload = JObject.Parse(await response.Content.ReadAsStringAsync()); var payload = JObject.Parse(await response.Content.ReadAsStringAsync());
return payload.Value<MessageWithPayloadResponse>(); return payload.Value<MessageWithPayloadResponse>();
} }
public static async Task<MessageWithPayloadResponse> NotifyEvent<Event> public static MessageWithPayloadResponse NotifyEvent<Event>
(this HttpClient channel, GoogleAuthSettings googleSettings, IEnumerable<string> regids, Event ev) (this GoogleAuthSettings googleSettings, IEnumerable<string> regids, Event ev)
where Event : YaEvent where Event : YaEvent
{ {
if (regids == null) var msg = new MessageWithPayload<Event>()
throw new NotImplementedException ("Notify & No GCM reg ids"); {
var request = new HttpRequestMessage(HttpMethod.Get, Constants.GCMNotificationUrl); notification = new Notification()
request.Headers.Authorization = new AuthenticationHeaderValue("Key", googleSettings.ApiKey); {
var msg = new MessageWithPayload<Event> () { title = ev.Title,
notification = new Notification() { title = ev.Title, body = ev.Description + ev.Comment == null ?
body = ev.Description + ev.Comment==null? "" : "(" + ev.Comment + ")",
"":"("+ev.Comment+")", icon = "icon" }, icon = "icon"
data = ev, registration_ids = regids.ToArray() }; },
data = ev,
registration_ids = regids.ToArray()
};
var response = await channel.SendAsync(request); if (regids == null)
var payload = JObject.Parse(await response.Content.ReadAsStringAsync()); throw new NotImplementedException("Notify & No GCM reg ids");
return payload.Value<MessageWithPayloadResponse>(); using (var m = new SimpleJsonPostMethod("https://gcm-http.googleapis.com/gcm/send",$"key={googleSettings.ApiKey}")) {
} return m.Invoke<MessageWithPayloadResponse>(msg);
}
}
public static async Task<UserCredential> GetCredentialForGoogleApiAsync(this UserManager<ApplicationUser> userManager, ApplicationDbContext context, string uid) public static async Task<UserCredential> GetCredentialForGoogleApiAsync(this UserManager<ApplicationUser> userManager, ApplicationDbContext context, string uid)
{ {
var user = await userManager.FindByIdAsync(uid); var user = await userManager.FindByIdAsync(uid);
@ -91,11 +101,11 @@ namespace Yavsc.Helpers
x => x.UserId == uid x => x.UserId == uid
).ProviderKey; ).ProviderKey;
if (string.IsNullOrEmpty(googleId)) if (string.IsNullOrEmpty(googleId))
throw new InvalidOperationException("No Google login"); throw new InvalidOperationException("No Google login");
var token = await context.GetTokensAsync(googleId); var token = await context.GetTokensAsync(googleId);
return new UserCredential(uid, token); return new UserCredential(uid, token);
} }
} }
} }