Correction de l'envoi GCM depuis multi device

main
Paul Schneider 10 years ago
parent 591e6c5f46
commit 8343abd2b4
5 changed files with 15 additions and 25 deletions

@ -133,9 +133,9 @@ namespace Yavsc.Controllers
_context.SaveChanges(); _context.SaveChanges();
if (command.PerformerProfile.AcceptNotifications if (command.PerformerProfile.AcceptNotifications
&& command.PerformerProfile.AcceptPublicContact && command.PerformerProfile.AcceptPublicContact
&& command.PerformerProfile.Performer.GoogleRegId!=null) { && command.PerformerProfile.Performer.Devices.Select(d=>d.RegistrationId)!=null) {
grep = await _GCMSender.NotifyAsync(_googleSettings, grep = await _GCMSender.NotifyAsync(_googleSettings,
command.PerformerProfile.Performer.GoogleRegId, command.PerformerProfile.Performer.Devices.Select(d=>d.RegistrationId),
yaev yaev
); );
} }

@ -4,7 +4,6 @@ using System.Linq;
using System.Threading.Tasks; using System.Threading.Tasks;
using Microsoft.AspNet.Authorization; using Microsoft.AspNet.Authorization;
using Microsoft.AspNet.Hosting; using Microsoft.AspNet.Hosting;
using Microsoft.AspNet.Http;
using Microsoft.AspNet.Mvc; using Microsoft.AspNet.Mvc;
using Microsoft.Extensions.Logging; using Microsoft.Extensions.Logging;
using Microsoft.Extensions.OptionsModel; using Microsoft.Extensions.OptionsModel;

@ -49,45 +49,36 @@ namespace Yavsc.Helpers
//send a MessageWithPayload<YaEvent> to circle members //send a MessageWithPayload<YaEvent> to circle members
// receive MessageWithPayloadResponse // receive MessageWithPayloadResponse
// "https://gcm-http.googleapis.com/gcm/send" // "https://gcm-http.googleapis.com/gcm/send"
var request = new HttpRequestMessage(HttpMethod.Get, Constants.GCMNotificationUrl);
request.Headers.Authorization = new AuthenticationHeaderValue("Key", googleSettings.ApiKey);
var regids = new List<string> (); var regids = new List<string> ();
var to = 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) {
if (u.Member.GoogleRegId == null) regids.AddRange (u.Member.Devices.Select(d=>d.RegistrationId));
to.Add (u.Member.Email);
else
regids.Add ((string)u.Member.GoogleRegId);
} }
if (regids.Count>0) return null;
if (regids.Count == 0) var request = new HttpRequestMessage(HttpMethod.Get, Constants.GCMNotificationUrl);
throw new InvalidOperationException request.Headers.Authorization = new AuthenticationHeaderValue("Key", googleSettings.ApiKey);
("No recipient where found for this circle list");
var msg = new MessageWithPayload<YaEvent> () { 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 async Task<MessageWithPayloadResponse> NotifyEvent<Event>
(this HttpClient channel, GoogleAuthSettings googleSettings, string regid, Event ev) (this HttpClient channel, GoogleAuthSettings googleSettings, IEnumerable<string> regids, Event ev)
where Event : YaEvent where Event : YaEvent
{ {
if (regid == null) if (regids == null)
throw new NotImplementedException ("Notify & No GCM reg id"); throw new NotImplementedException ("Notify & No GCM reg ids");
var request = new HttpRequestMessage(HttpMethod.Get, Constants.GCMNotificationUrl); var request = new HttpRequestMessage(HttpMethod.Get, Constants.GCMNotificationUrl);
request.Headers.Authorization = new AuthenticationHeaderValue("Key", googleSettings.ApiKey); request.Headers.Authorization = new AuthenticationHeaderValue("Key", googleSettings.ApiKey);
var msg = new MessageWithPayload<Event> () { var msg = new MessageWithPayload<Event> () {
notification = new Notification() { title = ev.Title, notification = new Notification() { title = ev.Title,
body = ev.Description + ev.Comment==null? body = ev.Description + ev.Comment==null?
"":"("+ev.Comment+")", icon = "icon" }, "":"("+ev.Comment+")", icon = "icon" },
data = ev, registration_ids = new string[] { regid } }; data = ev, 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());

@ -10,7 +10,6 @@ namespace Yavsc.Models
// Add profile data for application users by adding properties to the ApplicationUser class // Add profile data for application users by adding properties to the ApplicationUser class
public class ApplicationUser : IdentityUser public class ApplicationUser : IdentityUser
{ {
public string GoogleRegId { get; set; }
[Display(Name="AccountBalance")] [Display(Name="AccountBalance")]
public virtual AccountBalance AccountBalance { get; set; } public virtual AccountBalance AccountBalance { get; set; }

@ -1,4 +1,5 @@
using System.Collections.Generic;
using System.Threading.Tasks; using System.Threading.Tasks;
using Yavsc.Models.Google.Messaging; using Yavsc.Models.Google.Messaging;
using Yavsc.Models.Messaging; using Yavsc.Models.Messaging;
@ -7,6 +8,6 @@ namespace Yavsc.Services
{ {
public interface IGoogleCloudMessageSender public interface IGoogleCloudMessageSender
{ {
Task<MessageWithPayloadResponse> NotifyAsync(GoogleAuthSettings googlesettings, string registrationId, YaEvent ev); Task<MessageWithPayloadResponse> NotifyAsync(GoogleAuthSettings googlesettings, IEnumerable<string> registrationId, YaEvent ev);
} }
} }

Loading…