Search code examples
c#pushsharp

Multiple callback for one notification Pushsharp


For sending bulk notification in pushsharp, i am using foreach loop. I am getting multiple call back for same notification.

suppose i have send notification to 3 devices , i am getting callback 10 times . It repeats callback notification for all 3 devices.

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);
        }

Solution

  • In C# adding the same callback to a delegate more than once results in that callback being invoked as many times as it was added. What you probably want is to move the parts of the code which don't depend on recipient outside of the loop. That way you'll registed each callback method only once, irrespective of the count of 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
    }