一次通知多次回调 Pushsharp
Multiple callback for one notification Pushsharp
为了在 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);
}
在 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
}
为了在 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);
}
在 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
}