要在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);
}
答案 0 :(得分:0)
在C#中,多次向委托添加相同的回调会导致回调被调用的次数与添加的次数相同。您可能想要的是移动代码中不依赖于recipient
的部分代码。这样,您只会对每个回调方法进行一次注册,而不管recipients
的数量。
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;
// not sure about this one
push.RegisterGcmService(new GcmPushChannelSettings(ConfigurationManager.AppSettings["GCM_Development_ServerKey"].ToString()));
foreach(var recipient in recipients)
{
// do other things here
}