PageRenderTime 40ms CodeModel.GetById 13ms RepoModel.GetById 1ms app.codeStats 0ms

/PushSharp.Android/Gcm/GcmNotification.cs

https://github.com/mustafagenc/PushSharp
C# | 124 lines | 91 code | 18 blank | 15 comment | 6 complexity | e03024c74ec01ce8ce6c033d4b4cfd64 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.Text;
  6. using System.Web;
  7. using Newtonsoft.Json.Linq;
  8. using PushSharp.Common;
  9. namespace PushSharp.Android
  10. {
  11. public class GcmNotification : Notification
  12. {
  13. public static GcmNotification ForSingleResult(GcmMessageTransportResponse response, int resultIndex)
  14. {
  15. var result = new GcmNotification();
  16. result.Platform = PlatformType.AndroidGcm;
  17. result.Tag = response.Message.Tag;
  18. result.RegistrationIds.Add(response.Message.RegistrationIds[resultIndex]);
  19. result.CollapseKey = response.Message.CollapseKey;
  20. result.JsonData = response.Message.JsonData;
  21. result.DelayWhileIdle = response.Message.DelayWhileIdle;
  22. return result;
  23. }
  24. public static GcmNotification ForSingleRegistrationId(GcmNotification msg, string registrationId)
  25. {
  26. var result = new GcmNotification();
  27. result.Platform = PlatformType.AndroidGcm;
  28. result.Tag = msg.Tag;
  29. result.RegistrationIds.Add(registrationId);
  30. result.CollapseKey = msg.CollapseKey;
  31. result.JsonData = msg.JsonData;
  32. result.DelayWhileIdle = msg.DelayWhileIdle;
  33. return result;
  34. }
  35. public GcmNotification()
  36. {
  37. this.Platform = PlatformType.AndroidGcm;
  38. this.RegistrationIds = new List<string>();
  39. this.CollapseKey = string.Empty;
  40. this.JsonData = string.Empty;
  41. this.DelayWhileIdle = null;
  42. }
  43. /// <summary>
  44. /// Registration ID of the Device
  45. /// </summary>
  46. public List<string> RegistrationIds
  47. {
  48. get;
  49. set;
  50. }
  51. /// <summary>
  52. /// Only the latest message with the same collapse key will be delivered
  53. /// </summary>
  54. public string CollapseKey
  55. {
  56. get;
  57. set;
  58. }
  59. /// <summary>
  60. /// JSON Payload to be sent in the message
  61. /// </summary>
  62. public string JsonData
  63. {
  64. get;
  65. set;
  66. }
  67. /// <summary>
  68. /// If true, C2DM will only be delivered once the device's screen is on
  69. /// </summary>
  70. public bool? DelayWhileIdle
  71. {
  72. get;
  73. set;
  74. }
  75. /// <summary>
  76. /// Time in seconds that a message should be kept on the server if the device is offline. Default is 4 weeks.
  77. /// </summary>
  78. public int? TimeToLive
  79. {
  80. get;
  81. set;
  82. }
  83. internal string GetJson()
  84. {
  85. var json = new JObject();
  86. if (!string.IsNullOrEmpty(this.CollapseKey))
  87. json["collapse_key"] = this.CollapseKey;
  88. if (this.TimeToLive.HasValue)
  89. json["time_to_live"] = this.TimeToLive.Value;
  90. json["registration_ids"] = new JArray(this.RegistrationIds.ToArray());
  91. if (this.DelayWhileIdle.HasValue)
  92. json["delay_while_idle"] = this.DelayWhileIdle.Value;
  93. if (!string.IsNullOrEmpty(this.JsonData))
  94. {
  95. var jsonData = JObject.Parse(this.JsonData);
  96. if (jsonData != null)
  97. json["data"] = jsonData;
  98. }
  99. return json.ToString();
  100. }
  101. public override string ToString()
  102. {
  103. return GetJson();
  104. }
  105. }
  106. }