PageRenderTime 39ms CodeModel.GetById 16ms RepoModel.GetById 0ms app.codeStats 0ms

/PushSharp.Android/Gcm/GcmPushChannel.cs

https://github.com/mustafagenc/PushSharp
C# | 112 lines | 84 code | 20 blank | 8 comment | 13 complexity | 319abbcc80f77bed6159718a5c311dd6 MD5 | raw file
Possible License(s): Apache-2.0
  1. using System;
  2. using System.Collections.Generic;
  3. using System.Collections.Specialized;
  4. using System.Linq;
  5. using System.Net;
  6. using System.Text;
  7. using System.Threading;
  8. using PushSharp.Common;
  9. namespace PushSharp.Android
  10. {
  11. public class GcmPushChannel : PushChannelBase
  12. {
  13. GcmPushChannelSettings gcmSettings = null;
  14. GcmMessageTransportAsync transport;
  15. long waitCounter = 0;
  16. public GcmPushChannel(GcmPushChannelSettings channelSettings, PushServiceSettings serviceSettings = null) : base(channelSettings, serviceSettings)
  17. {
  18. gcmSettings = channelSettings;
  19. transport = new GcmMessageTransportAsync();
  20. transport.MessageResponseReceived += new Action<GcmMessageTransportResponse>(transport_MessageResponseReceived);
  21. transport.UnhandledException += new Action<GcmNotification, Exception>(transport_UnhandledException);
  22. }
  23. public override PlatformType PlatformType
  24. {
  25. get { return PlatformType.AndroidGcm; }
  26. }
  27. void transport_UnhandledException(GcmNotification notification, Exception exception)
  28. {
  29. //Raise individual failures for each registration id for the notification
  30. foreach (var r in notification.RegistrationIds)
  31. {
  32. this.Events.RaiseNotificationSendFailure(GcmNotification.ForSingleRegistrationId(notification, r),
  33. exception);
  34. }
  35. this.Events.RaiseChannelException(exception, PlatformType.AndroidGcm, notification);
  36. Interlocked.Decrement(ref waitCounter);
  37. }
  38. void transport_MessageResponseReceived(GcmMessageTransportResponse response)
  39. {
  40. int index = 0;
  41. //Loop through every result in the response
  42. // We will raise events for each individual result so that the consumer of the library
  43. // can deal with individual registrationid's for the notification
  44. foreach (var r in response.Results)
  45. {
  46. var singleResultNotification = GcmNotification.ForSingleResult(response, index);
  47. if (r.ResponseStatus == GcmMessageTransportResponseStatus.Ok)
  48. {
  49. //It worked! Raise success
  50. this.Events.RaiseNotificationSent(singleResultNotification);
  51. }
  52. else if (r.ResponseStatus == GcmMessageTransportResponseStatus.CanonicalRegistrationId)
  53. {
  54. //Swap Registrations Id's
  55. var newRegistrationId = r.CanonicalRegistrationId;
  56. this.Events.RaiseDeviceSubscriptionIdChanged(PlatformType.AndroidGcm, singleResultNotification.RegistrationIds[0], newRegistrationId, singleResultNotification);
  57. }
  58. else if (r.ResponseStatus == GcmMessageTransportResponseStatus.Unavailable)
  59. {
  60. this.QueueNotification(singleResultNotification);
  61. }
  62. else if (r.ResponseStatus == GcmMessageTransportResponseStatus.NotRegistered)
  63. {
  64. //Raise failure and device expired
  65. this.Events.RaiseDeviceSubscriptionExpired(PlatformType.AndroidGcm, singleResultNotification.RegistrationIds[0], singleResultNotification);
  66. }
  67. else
  68. {
  69. //Raise failure, for unknown reason
  70. this.Events.RaiseNotificationSendFailure(singleResultNotification, new GcmMessageTransportException(r.ResponseStatus.ToString(), response));
  71. }
  72. index++;
  73. }
  74. Interlocked.Decrement(ref waitCounter);
  75. }
  76. protected override void SendNotification(Notification notification)
  77. {
  78. Interlocked.Increment(ref waitCounter);
  79. transport.Send(notification as GcmNotification, gcmSettings.SenderAuthToken, gcmSettings.SenderID, gcmSettings.ApplicationIdPackageName);
  80. }
  81. public override void Stop(bool waitForQueueToDrain)
  82. {
  83. base.Stop(waitForQueueToDrain);
  84. var slept = 0;
  85. while (Interlocked.Read(ref waitCounter) > 0 && slept <= 30000)
  86. {
  87. slept += 100;
  88. Thread.Sleep(100);
  89. }
  90. }
  91. }
  92. }