PageRenderTime 49ms CodeModel.GetById 23ms RepoModel.GetById 0ms app.codeStats 0ms

/PushSharp/PushService.cs

https://github.com/mustafagenc/PushSharp
C# | 167 lines | 140 code | 27 blank | 0 comment | 33 complexity | c4171249a8b2cef30935af5809cc9052 MD5 | raw file
Possible License(s): Apache-2.0
  1. using System;
  2. using System.Collections.Generic;
  3. using System.Linq;
  4. using System.Text;
  5. using System.Threading.Tasks;
  6. using PushSharp.Common;
  7. namespace PushSharp
  8. {
  9. public class PushService : IDisposable
  10. {
  11. public ChannelEvents Events;
  12. Apple.ApplePushService appleService = null;
  13. Android.C2dmPushService androidService = null;
  14. WindowsPhone.WindowsPhonePushService wpService = null;
  15. Windows.WindowsPushService winService = null;
  16. Blackberry.BlackberryPushService bbService = null;
  17. Android.GcmPushService gcmService = null;
  18. static PushService instance = null;
  19. public static PushService Instance
  20. {
  21. get
  22. {
  23. if (instance == null)
  24. instance = new PushService();
  25. return instance;
  26. }
  27. }
  28. public PushService()
  29. {
  30. this.Events = new ChannelEvents();
  31. }
  32. public void StartApplePushService(Apple.ApplePushChannelSettings channelSettings, PushServiceSettings serviceSettings = null)
  33. {
  34. appleService = new Apple.ApplePushService(channelSettings, serviceSettings);
  35. appleService.Events.RegisterProxyHandler(this.Events);
  36. }
  37. public void StopApplePushService(bool waitForQueueToFinish = true)
  38. {
  39. if (appleService != null)
  40. appleService.Stop(waitForQueueToFinish);
  41. }
  42. [Obsolete("Google has Deprecated C2DM, and you should now use GCM Instead. See the StartGoogleCloudMessagingPushService(...) method!")]
  43. public void StartAndroidPushService(Android.C2dmPushChannelSettings channelSettings, PushServiceSettings serviceSettings = null)
  44. {
  45. androidService = new Android.C2dmPushService(channelSettings, serviceSettings);
  46. androidService.Events.RegisterProxyHandler(this.Events);
  47. }
  48. [Obsolete("Google has Deprecated C2DM, and you should now use GCM Instead. See the StopGoogleCloudMessagingPushService() method!")]
  49. public void StopAndroidPushService(bool waitForQueueToFinish = true)
  50. {
  51. if (androidService != null)
  52. androidService.Stop(waitForQueueToFinish);
  53. }
  54. public void StartGoogleCloudMessagingPushService(Android.GcmPushChannelSettings channelSettings, PushServiceSettings serviceSettings = null)
  55. {
  56. gcmService = new Android.GcmPushService(channelSettings, serviceSettings);
  57. gcmService.Events.RegisterProxyHandler(this.Events);
  58. }
  59. public void StopGoogleCloudMessagingPushService(bool waitForQueueToFinish = true)
  60. {
  61. if (gcmService != null)
  62. gcmService.Stop(waitForQueueToFinish);
  63. }
  64. public void StartWindowsPhonePushService(WindowsPhone.WindowsPhonePushChannelSettings channelSettings = null, PushServiceSettings serviceSettings = null)
  65. {
  66. wpService = new WindowsPhone.WindowsPhonePushService(channelSettings, serviceSettings);
  67. wpService.Events.RegisterProxyHandler(this.Events);
  68. }
  69. public void StopWindowsPhonePushService(bool waitForQueueToFinish = true)
  70. {
  71. if (wpService != null)
  72. wpService.Stop(waitForQueueToFinish);
  73. }
  74. public void StartWindowsPushService(Windows.WindowsPushChannelSettings channelSettings, PushServiceSettings serviceSettings = null)
  75. {
  76. winService = new Windows.WindowsPushService(channelSettings, serviceSettings);
  77. winService.Events.RegisterProxyHandler(this.Events);
  78. }
  79. public void StopWindowsPushService(bool waitForQueueToFinish = true)
  80. {
  81. if (winService != null)
  82. winService.Stop(waitForQueueToFinish);
  83. }
  84. public void StartBlackberryPushService(Blackberry.BlackberryPushChannelSettings channelSettings, PushServiceSettings serviceSettings = null)
  85. {
  86. bbService = new Blackberry.BlackberryPushService(channelSettings, serviceSettings);
  87. bbService.Events.RegisterProxyHandler(this.Events);
  88. }
  89. public void StopBlackberryPushService(bool waitForQueueToFinish = true)
  90. {
  91. if (bbService != null)
  92. bbService.Stop(waitForQueueToFinish);
  93. }
  94. public void QueueNotification(Notification notification)
  95. {
  96. switch (notification.Platform)
  97. {
  98. case PlatformType.Apple:
  99. appleService.QueueNotification(notification);
  100. break;
  101. case PlatformType.AndroidC2dm:
  102. androidService.QueueNotification(notification);
  103. break;
  104. case PlatformType.AndroidGcm:
  105. gcmService.QueueNotification(notification);
  106. break;
  107. case PlatformType.WindowsPhone:
  108. wpService.QueueNotification(notification);
  109. break;
  110. case PlatformType.Windows:
  111. winService.QueueNotification(notification);
  112. break;
  113. case PlatformType.Blackberry:
  114. bbService.QueueNotification(notification);
  115. break;
  116. }
  117. }
  118. public void StopAllServices(bool waitForQueuesToFinish = true)
  119. {
  120. var tasks = new List<Task>();
  121. if (appleService != null && !appleService.IsStopping)
  122. tasks.Add(Task.Factory.StartNew(() => appleService.Stop(waitForQueuesToFinish)));
  123. if (androidService != null && !androidService.IsStopping)
  124. tasks.Add(Task.Factory.StartNew(() => androidService.Stop(waitForQueuesToFinish)));
  125. if (gcmService != null && !gcmService.IsStopping)
  126. tasks.Add(Task.Factory.StartNew(() => gcmService.Stop(waitForQueuesToFinish)));
  127. if (wpService != null && !wpService.IsStopping)
  128. tasks.Add(Task.Factory.StartNew(() => wpService.Stop(waitForQueuesToFinish)));
  129. if (winService != null && !winService.IsStopping)
  130. tasks.Add(Task.Factory.StartNew(() => winService.Stop(waitForQueuesToFinish)));
  131. if (bbService != null && !bbService.IsStopping)
  132. tasks.Add(Task.Factory.StartNew(() => bbService.Stop(waitForQueuesToFinish)));
  133. Task.WaitAll(tasks.ToArray());
  134. }
  135. void IDisposable.Dispose()
  136. {
  137. StopAllServices(false);
  138. }
  139. }
  140. }