PushNotificationsAndroid.cs 5.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256
  1. using UnityEngine;
  2. using System;
  3. using System.Collections;
  4. using System.Collections.Generic;
  5. public class PushNotificationsAndroid : Pushwoosh
  6. {
  7. #if UNITY_ANDROID && !UNITY_EDITOR
  8. private static AndroidJavaObject pushwoosh = null;
  9. protected override void Initialize()
  10. {
  11. if(pushwoosh != null)
  12. return;
  13. using(var pluginClass = new AndroidJavaClass("com.pushwoosh.unityplugin.PushwooshProxy")) {
  14. pluginClass.CallStatic("initialize", Pushwoosh.ApplicationCode, Pushwoosh.GcmProjectNumber);
  15. pushwoosh = pluginClass.CallStatic<AndroidJavaObject>("instance");
  16. }
  17. pushwoosh.Call("setListenerName", this.gameObject.name);
  18. }
  19. public override void RegisterForPushNotifications()
  20. {
  21. pushwoosh.Call("registerForPushNotifications");
  22. }
  23. public override void UnregisterForPushNotifications()
  24. {
  25. pushwoosh.Call("unregisterFromPushNotifications");
  26. }
  27. public override void SetIntTag(string tagName, int tagValue)
  28. {
  29. pushwoosh.Call("setIntTag", tagName, tagValue);
  30. }
  31. public override void SetStringTag(string tagName, string tagValue)
  32. {
  33. pushwoosh.Call("setStringTag", tagName, tagValue);
  34. }
  35. public override void SetListTag(string tagName, List<object> tagValues)
  36. {
  37. AndroidJavaObject tags = new AndroidJavaObject ("com.pushwoosh.unityplugin.TagValues");
  38. foreach( var tagValue in tagValues )
  39. {
  40. tags.Call ("addValue", tagValue);
  41. }
  42. pushwoosh.Call ("setListTag", tagName, tags);
  43. }
  44. public string GetLaunchNotification()
  45. {
  46. return pushwoosh.Call<string>("getLaunchNotification");
  47. }
  48. public void ClearLaunchNotification()
  49. {
  50. pushwoosh.Call("clearLaunchNotification");
  51. }
  52. public String[] GetPushHistory()
  53. {
  54. AndroidJavaObject history = pushwoosh.Call<AndroidJavaObject>("getPushHistory");
  55. if (history.GetRawObject().ToInt32() == 0)
  56. {
  57. return new String[0];
  58. }
  59. String[] result = AndroidJNIHelper.ConvertFromJNIArray<String[]>(history.GetRawObject());
  60. history.Dispose();
  61. return result;
  62. }
  63. public void ClearPushHistory()
  64. {
  65. pushwoosh.Call("clearPushHistory");
  66. }
  67. public override void StartTrackingGeoPushes()
  68. {
  69. pushwoosh.Call("startTrackingGeoPushes");
  70. }
  71. public override void StopTrackingGeoPushes()
  72. {
  73. pushwoosh.Call("stopTrackingGeoPushes");
  74. }
  75. public void StartTrackingBeaconPushes()
  76. {
  77. pushwoosh.Call("startTrackingBeaconPushes");
  78. }
  79. public void StopTrackingBeaconPushes()
  80. {
  81. pushwoosh.Call("stopTrackingBeaconPushes");
  82. }
  83. public void SetBeaconBackgroundMode(bool backgroundMode)
  84. {
  85. pushwoosh.Call("setBeaconBackgroundMode", backgroundMode);
  86. }
  87. public void ClearLocalNotifications()
  88. {
  89. pushwoosh.Call("clearLocalNotifications");
  90. }
  91. public override void ClearNotificationCenter()
  92. {
  93. pushwoosh.Call("clearNotificationCenter");
  94. }
  95. public int ScheduleLocalNotification(string message, int seconds)
  96. {
  97. return pushwoosh.Call<int>("scheduleLocalNotification", message, seconds);
  98. }
  99. public int ScheduleLocalNotification(string message, int seconds, string userdata)
  100. {
  101. IDictionary<string,string> parameters = new Dictionary<string, string>();
  102. parameters.Add("u", userdata);
  103. return ScheduleLocalNotification(message, seconds, parameters);
  104. }
  105. public int ScheduleLocalNotification(string message, int seconds, IDictionary<string, string> parameters)
  106. {
  107. var extras = new AndroidJavaObject("android.os.Bundle");
  108. foreach (var item in parameters)
  109. {
  110. extras.Call("putString", item.Key, item.Value);
  111. }
  112. return pushwoosh.Call<int>("scheduleLocalNotification", message, seconds, extras);
  113. }
  114. public void ClearLocalNotification(int id)
  115. {
  116. pushwoosh.Call("clearLocalNotification", id);
  117. }
  118. public void SetMultiNotificationMode()
  119. {
  120. pushwoosh.Call("setMultiNotificationMode");
  121. }
  122. public void SetSimpleNotificationMode()
  123. {
  124. pushwoosh.Call("setSimpleNotificationMode");
  125. }
  126. /*
  127. * Sound notification types:
  128. * 0 - default mode
  129. * 1 - no sound
  130. * 2 - always
  131. */
  132. public void SetSoundNotificationType(int soundNotificationType)
  133. {
  134. pushwoosh.Call("setSoundNotificationType", soundNotificationType);
  135. }
  136. /*
  137. * Vibrate notification types:
  138. * 0 - default mode
  139. * 1 - no vibrate
  140. * 2 - always
  141. */
  142. public void SetVibrateNotificationType(int vibrateNotificationType)
  143. {
  144. pushwoosh.Call("setVibrateNotificationType", vibrateNotificationType);
  145. }
  146. public void SetLightScreenOnNotification(bool lightsOn)
  147. {
  148. pushwoosh.Call("setLightScreenOnNotification", lightsOn);
  149. }
  150. public void SetEnableLED(bool ledOn)
  151. {
  152. pushwoosh.Call("setEnableLED", ledOn);
  153. }
  154. public override void SetBadgeNumber(int number)
  155. {
  156. pushwoosh.Call("setBadgeNumber", number);
  157. }
  158. public override void AddBadgeNumber(int deltaBadge)
  159. {
  160. pushwoosh.Call("addBadgeNumber", deltaBadge);
  161. }
  162. public override string HWID
  163. {
  164. get { return pushwoosh.Call<string>("getPushwooshHWID"); }
  165. }
  166. public override string PushToken
  167. {
  168. get { return pushwoosh.Call<string>("getPushToken"); }
  169. }
  170. public override void SetUserId(string userId)
  171. {
  172. pushwoosh.Call("setUserId", userId);
  173. }
  174. protected override void PostEventInternal(string eventId, string attributes)
  175. {
  176. pushwoosh.Call("postEvent", eventId, attributes);
  177. }
  178. public override void SendPurchase(string productId, double price, string currency)
  179. {
  180. pushwoosh.Call("sendPurchase", productId, price, currency);
  181. }
  182. void onRegisteredForPushNotifications(string token)
  183. {
  184. RegisteredForPushNotifications (token);
  185. }
  186. void onFailedToRegisteredForPushNotifications(string error)
  187. {
  188. FailedToRegisteredForPushNotifications (error);
  189. }
  190. void onPushNotificationsReceived(string payload)
  191. {
  192. PushNotificationsReceived (payload);
  193. }
  194. void OnApplicationPause(bool paused)
  195. {
  196. //make sure everything runs smoothly even if pushwoosh is not initialized yet
  197. if (pushwoosh == null)
  198. Initialize();
  199. if(paused)
  200. {
  201. pushwoosh.Call("onPause");
  202. }
  203. else
  204. {
  205. pushwoosh.Call("onResume");
  206. }
  207. }
  208. #endif
  209. }