/PushSharp.Apple/ApplePushService.cs

https://github.com/mustafagenc/PushSharp · C# · 55 lines · 45 code · 8 blank · 2 comment · 1 complexity · 7aa542540cdea2ffed0ae8d6d2e8809a MD5 · raw file

  1. using System;
  2. using System.Collections.Generic;
  3. using System.Collections.Concurrent;
  4. using System.Linq;
  5. using System.Text;
  6. using System.Threading;
  7. using System.Threading.Tasks;
  8. using PushSharp.Common;
  9. namespace PushSharp.Apple
  10. {
  11. public class ApplePushService : Common.PushServiceBase<ApplePushChannelSettings>, IDisposable
  12. {
  13. FeedbackService feedbackService;
  14. CancellationTokenSource cancelTokenSource;
  15. Timer timerFeedback;
  16. public ApplePushService(ApplePushChannelSettings channelSettings, PushServiceSettings serviceSettings = null)
  17. : base(channelSettings, serviceSettings)
  18. {
  19. var appleChannelSettings = channelSettings as ApplePushChannelSettings;
  20. cancelTokenSource = new CancellationTokenSource();
  21. feedbackService = new FeedbackService();
  22. feedbackService.OnFeedbackReceived += new FeedbackService.FeedbackReceivedDelegate(feedbackService_OnFeedbackReceived);
  23. //allow control over feedback call interval, if set to zero, don't make feedback calls automatically
  24. if (appleChannelSettings.FeedbackIntervalMinutes > 0)
  25. {
  26. timerFeedback = new Timer(new TimerCallback((state) =>
  27. {
  28. try { feedbackService.Run(channelSettings as ApplePushChannelSettings, this.cancelTokenSource.Token); }
  29. catch (Exception ex) { this.Events.RaiseChannelException(ex, PlatformType.Apple); }
  30. //Timer will run first after 10 seconds, then every 10 minutes to get feedback!
  31. }), null, TimeSpan.FromSeconds(10), TimeSpan.FromMinutes(appleChannelSettings.FeedbackIntervalMinutes));
  32. }
  33. }
  34. void feedbackService_OnFeedbackReceived(string deviceToken, DateTime timestamp)
  35. {
  36. this.Events.RaiseDeviceSubscriptionExpired(PlatformType.Apple, deviceToken);
  37. }
  38. protected override Common.PushChannelBase CreateChannel(Common.PushChannelSettings channelSettings)
  39. {
  40. return new ApplePushChannel(channelSettings as ApplePushChannelSettings);
  41. }
  42. public override PlatformType Platform
  43. {
  44. get { return PlatformType.Apple; }
  45. }
  46. }
  47. }