Pushwoosh.cs 6.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221
  1. using UnityEngine;
  2. using System.Collections.Generic;
  3. #if UNITY_IPHONE && !UNITY_EDITOR
  4. using PushwooshInstanceType = PushNotificationsIOS;
  5. #elif UNITY_ANDROID && !UNITY_EDITOR
  6. using PushwooshInstanceType = PushNotificationsAndroid;
  7. #elif (UNITY_WP8 || UNITY_WP8_1 || UNITY_WSA || UNITY_WSA_8_0 || UNITY_WSA_8_1 || UNITY_WSA_10_0) && !UNITY_EDITOR
  8. using PushwooshInstanceType = PushNotificationsWindows;
  9. #else
  10. using PushwooshInstanceType = Pushwoosh;
  11. #endif
  12. public class Pushwoosh : MonoBehaviour
  13. {
  14. public static string ApplicationCode { get; set; }
  15. public static string GcmProjectNumber { get; set; }
  16. public delegate void RegistrationSuccessHandler(string token);
  17. public delegate void RegistrationErrorHandler(string error);
  18. public delegate void NotificationHandler(string payload);
  19. public event RegistrationSuccessHandler OnRegisteredForPushNotifications = delegate {};
  20. public event RegistrationErrorHandler OnFailedToRegisteredForPushNotifications = delegate {};
  21. public event NotificationHandler OnPushNotificationsReceived = delegate {};
  22. public virtual string HWID
  23. {
  24. get
  25. {
  26. Debug.Log ("[Pushwoosh] Error: HWID is not supported on this platform");
  27. return "Unsupported platform";
  28. }
  29. }
  30. public virtual string PushToken
  31. {
  32. get
  33. {
  34. Debug.Log ("[Pushwoosh] Error: PushToken is not supported on this platform");
  35. return "Unsupported platform";
  36. }
  37. }
  38. public virtual void RegisterForPushNotifications()
  39. {
  40. Debug.Log ("[Pushwoosh] Error: RegisterForPushNotifications is not supported on this platform");
  41. }
  42. public virtual void UnregisterForPushNotifications()
  43. {
  44. Debug.Log ("[Pushwoosh] Error: UnregisterForPushNotifications is not supported on this platform");
  45. }
  46. public virtual void StartTrackingGeoPushes()
  47. {
  48. Debug.Log ("[Pushwoosh] Error: StartTrackingGeoPushes is not supported on this platform");
  49. }
  50. public virtual void StopTrackingGeoPushes()
  51. {
  52. Debug.Log ("[Pushwoosh] Error: StopTrackingGeoPushes is not supported on this platform");
  53. }
  54. public virtual void SetIntTag(string tagName, int tagValue)
  55. {
  56. Debug.Log ("[Pushwoosh] Error: SetIntTag is not supported on this platform");
  57. }
  58. public virtual void SetStringTag(string tagName, string tagValue)
  59. {
  60. Debug.Log ("[Pushwoosh] Error: SetStringTag is not supported on this platform");
  61. }
  62. public virtual void SetListTag(string tagName, List<object> tagValues)
  63. {
  64. Debug.Log ("[Pushwoosh] Error: SetListTag is not supported on this platform");
  65. }
  66. public virtual void ClearNotificationCenter()
  67. {
  68. Debug.Log ("[Pushwoosh] Error: ClearNotificationCenter is not supported on this platform");
  69. }
  70. public virtual void SetBadgeNumber(int number)
  71. {
  72. Debug.Log ("[Pushwoosh] Error: SetBadgeNumber is not supported on this platform");
  73. }
  74. public virtual void AddBadgeNumber(int deltaBadge)
  75. {
  76. Debug.Log ("[Pushwoosh] Error: AddBadgeNumber is not supported on this platform");
  77. }
  78. public virtual void SetUserId(string userId)
  79. {
  80. Debug.Log ("[Pushwoosh] Error: SetUserId is not supported on this platform");
  81. }
  82. public virtual void PostEvent(string eventId, IDictionary<string, object> attributes)
  83. {
  84. var entries = new List<string>();
  85. if (attributes != null) {
  86. foreach (var attribute in attributes) {
  87. var key = attribute.Key;
  88. var value = attribute.Value;
  89. if (value is string) {
  90. entries.Add (string.Format("\"{0}\": \"{1}\"", key, value));
  91. }
  92. else {
  93. entries.Add (string.Format("\"{0}\": {1}", key, value));
  94. }
  95. }
  96. }
  97. string attributesStr = "{" + string.Join(",", entries.ToArray()) + "}";
  98. PostEventInternal(eventId, attributesStr);
  99. }
  100. public virtual void SendPurchase(string productId, double price, string currency)
  101. {
  102. Debug.Log ("[Pushwoosh] Error: SendPurchase is not supported on this platform");
  103. }
  104. protected virtual void PostEventInternal(string eventId, string attributes)
  105. {
  106. Debug.Log ("[Pushwoosh] Error: PostEvent is not supported on this platform");
  107. }
  108. protected void RegisteredForPushNotifications(string token)
  109. {
  110. OnRegisteredForPushNotifications(token);
  111. }
  112. protected void FailedToRegisteredForPushNotifications(string error)
  113. {
  114. OnFailedToRegisteredForPushNotifications(error);
  115. }
  116. protected void PushNotificationsReceived(string payload)
  117. {
  118. OnPushNotificationsReceived(payload);
  119. }
  120. // Singleton
  121. private static PushwooshInstanceType _instance;
  122. private static object _lock = new object();
  123. protected Pushwoosh() {}
  124. protected virtual void Initialize() {}
  125. public static PushwooshInstanceType Instance
  126. {
  127. get
  128. {
  129. if (applicationIsQuitting) {
  130. Debug.LogWarning("[Singleton] Instance '"+ typeof(PushwooshInstanceType) +
  131. "' already destroyed on application quit." +
  132. " Won't create again - returning null.");
  133. return null;
  134. }
  135. lock(_lock)
  136. {
  137. if (_instance == null)
  138. {
  139. _instance = (PushwooshInstanceType) FindObjectOfType(typeof(PushwooshInstanceType));
  140. if ( FindObjectsOfType(typeof(PushwooshInstanceType)).Length > 1 )
  141. {
  142. Debug.LogError("[Singleton] Something went really wrong " +
  143. " - there should never be more than 1 singleton!" +
  144. " Reopening the scene might fix it.");
  145. return _instance;
  146. }
  147. if (_instance == null)
  148. {
  149. GameObject singleton = new GameObject();
  150. singleton.name = "(singleton) "+ typeof(PushwooshInstanceType).ToString();
  151. _instance = singleton.AddComponent<PushwooshInstanceType>();
  152. _instance.Initialize ();
  153. DontDestroyOnLoad(singleton);
  154. Debug.Log("[Singleton] An instance of " + typeof(PushwooshInstanceType) +
  155. " has been created with DontDestroyOnLoad.");
  156. } else {
  157. Debug.Log("[Singleton] Using instance already created: " +
  158. _instance.gameObject.name);
  159. }
  160. }
  161. return _instance;
  162. }
  163. }
  164. }
  165. private static bool applicationIsQuitting = false;
  166. /// <summary>
  167. /// When Unity quits, it destroys objects in a random order.
  168. /// In principle, a Singleton is only destroyed when application quits.
  169. /// If any script calls Instance after it have been destroyed,
  170. /// it will create a buggy ghost object that will stay on the Editor scene
  171. /// even after stopping playing the Application. Really bad!
  172. /// So, this was made to be sure we're not creating that buggy ghost object.
  173. /// </summary>
  174. public void OnDestroy () {
  175. applicationIsQuitting = true;
  176. }
  177. }