一个通知的多个回调 Pushsharp

Posted

技术标签:

【中文标题】一个通知的多个回调 Pushsharp【英文标题】:Multiple callback for one notification Pushsharp 【发布时间】: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);
        

【问题讨论】:

【参考方案1】:

在 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的主要内容,如果未能解决你的问题,请参考以下文章

Django信号

正确使用element中Notification的回调方法close、onClick、onClose

无法在 Apple Watch 上获得可操作通知回调

ios不点击通知栏如何触发通知回调

避免 AVAudioEngineConfigurationChange 通知回调中的死锁

java接收回调通知实现方式