【发布时间】:2015-09-04 07:02:25
【问题描述】:
为了在 pushsharp 中发送批量通知,我正在使用 foreach 循环。 我收到了多次回电以获得相同的通知。
假设我已向 3 台设备发送通知,我会收到 10 次回调。 它为所有 3 台设备重复回调通知。
foreach (var recipient in recipients)
{
//Wire up the events for all the services that the broker registers
push.OnChannelCreated += push_OnChannelCreated;
push.OnChannelDestroyed += push_OnChannelDestroyed;
push.OnChannelException += push_OnChannelException;
push.OnDeviceSubscriptionChanged += push_OnDeviceSubscriptionChanged;
push.OnDeviceSubscriptionExpired += push_OnDeviceSubscriptionExpired;
push.OnNotificationFailed += push_OnNotificationFailed;
push.OnNotificationRequeue += push_OnNotificationRequeue;
push.OnNotificationSent += push_OnNotificationSent;
push.OnServiceException += push_OnServiceException;
var gcmMessage = new GCMMessage
{
message = TemplateUtility.GetNotificationBodyGcm(TemplateName, recipient),
badge=7,
sound="sound.caf"
};
string jsonGcmMessage = Newtonsoft.Json.JsonConvert.SerializeObject(gcmMessage);
push.RegisterGcmService(new GcmPushChannelSettings(ConfigurationManager.AppSettings["GCM_Development_ServerKey"].ToString()));
//push.RegisterGcmService(new GcmPushChannelSettings(ConfigurationManager.AppSettings["GCM_Production_ServerKey"].ToString()));
push.QueueNotification(new GcmNotification().ForDeviceRegistrationId(recipient.DeviceRegistrationToken)
//.WithJson("{\"message\":\"Hi PushNoti\",\"badge\":7,\"sound\":\"sound.caf\"}"));
.WithJson(jsonGcmMessage));
//Stop and wait for the queues to drains before it dispose
push.StopAllServices(waitForQueuesToFinish: true);
}
【问题讨论】: