2

I have used PushSharp v 2 before but want to update to v 4. I wonder if singleton really is best practise in my case.

Current workflow: I have a job that starts every minute and checks if there are new notifications to send.

PushSharp v.4 has introduced GcmServiceBroker.Start() and GcmServiceBroker.Stop() and I wonder if singleton really is best practice since you can't use Stop(). At least I haven't figured that out. The reason that I can't use Stop() is because when I call Stop() it set the flag IsAddingCompleted which prevent more notification to be added to the queue and I don't know how to release that lock.

My solution is therefore to not use Stop() but what I understand Stop() ensures that all notifications has been sent before exiting and I don't want to drop that improvement. So, is it even possible to use PushSharp v4 "the correct way" as a singleton or is it better to create a new object every time I start the job.

This is my code as a singleton:

public class GcmPushService
{
    static GcmPushService()
    {
        Push = PushBrokerSingleton.Instance;
    }

    internal static GcmServiceBroker Push { get; }

    public static void QueueNotification(string deviceToken, GcmNotification notification)
    {            
        notification.RegistrationIds.Add(deviceToken);
        Push.QueueNotification(notification);           
    }

    private class PushBrokerSingleton
    {            
        static PushBrokerSingleton()
        {
            var gcmServiceBroker = new GcmServiceBroker(...gcmConfiguration);

            //Exception handling

            gcmServiceBroker.Start();

            Instance = gcmServiceBroker;
        }

        internal static readonly GcmServiceBroker Instance;
    }  
}
Henke
  • 99
  • 8

0 Answers0