LocalNotification.java 14 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434
  1. /*
  2. Copyright 2013-2014 appPlant UG
  3. Licensed to the Apache Software Foundation (ASF) under one
  4. or more contributor license agreements. See the NOTICE file
  5. distributed with this work for additional information
  6. regarding copyright ownership. The ASF licenses this file
  7. to you under the Apache License, Version 2.0 (the
  8. "License"); you may not use this file except in compliance
  9. with the License. You may obtain a copy of the License at
  10. http://www.apache.org/licenses/LICENSE-2.0
  11. Unless required by applicable law or agreed to in writing,
  12. software distributed under the License is distributed on an
  13. "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
  14. KIND, either express or implied. See the License for the
  15. specific language governing permissions and limitations
  16. under the License.
  17. */
  18. package de.appplant.cordova.plugin.localnotification;
  19. import java.util.ArrayList;
  20. import java.util.Date;
  21. import java.util.Map;
  22. import java.util.Set;
  23. import org.apache.cordova.CallbackContext;
  24. import org.apache.cordova.CordovaInterface;
  25. import org.apache.cordova.CordovaPlugin;
  26. import org.apache.cordova.CordovaWebView;
  27. import org.apache.cordova.PluginResult;
  28. import org.json.JSONArray;
  29. import org.json.JSONException;
  30. import org.json.JSONObject;
  31. import android.app.AlarmManager;
  32. import android.app.NotificationManager;
  33. import android.app.PendingIntent;
  34. import android.content.Context;
  35. import android.content.Intent;
  36. import android.content.SharedPreferences;
  37. import android.content.SharedPreferences.Editor;
  38. import android.os.Build;
  39. /**
  40. * This plugin utilizes the Android AlarmManager in combination with StatusBar
  41. * notifications. When a local notification is scheduled the alarm manager takes
  42. * care of firing the event. When the event is processed, a notification is put
  43. * in the Android status bar.
  44. */
  45. public class LocalNotification extends CordovaPlugin {
  46. protected final static String PLUGIN_NAME = "LocalNotification";
  47. private static CordovaWebView webView = null;
  48. private static Boolean deviceready = false;
  49. protected static Context context = null;
  50. protected static Boolean isInBackground = true;
  51. private static ArrayList<String> eventQueue = new ArrayList<String>();
  52. @Override
  53. public void initialize (CordovaInterface cordova, CordovaWebView webView) {
  54. super.initialize(cordova, webView);
  55. LocalNotification.webView = super.webView;
  56. LocalNotification.context = super.cordova.getActivity().getApplicationContext();
  57. }
  58. @Override
  59. public boolean execute (String action, final JSONArray args, final CallbackContext command) throws JSONException {
  60. if (action.equalsIgnoreCase("add")) {
  61. cordova.getThreadPool().execute( new Runnable() {
  62. public void run() {
  63. JSONObject arguments = args.optJSONObject(0);
  64. Options options = new Options(context).parse(arguments);
  65. persist(options.getId(), args);
  66. add(options, true);
  67. command.success();
  68. }
  69. });
  70. }
  71. if (action.equalsIgnoreCase("cancel")) {
  72. cordova.getThreadPool().execute( new Runnable() {
  73. public void run() {
  74. String id = args.optString(0);
  75. cancel(id);
  76. unpersist(id);
  77. command.success();
  78. }
  79. });
  80. }
  81. if (action.equalsIgnoreCase("cancelAll")) {
  82. cordova.getThreadPool().execute( new Runnable() {
  83. public void run() {
  84. cancelAll();
  85. unpersistAll();
  86. command.success();
  87. }
  88. });
  89. }
  90. if (action.equalsIgnoreCase("isScheduled")) {
  91. String id = args.optString(0);
  92. isScheduled(id, command);
  93. }
  94. if (action.equalsIgnoreCase("getScheduledIds")) {
  95. getScheduledIds(command);
  96. }
  97. if (action.equalsIgnoreCase("isTriggered")) {
  98. String id = args.optString(0);
  99. isTriggered(id, command);
  100. }
  101. if (action.equalsIgnoreCase("getTriggeredIds")) {
  102. getTriggeredIds(command);
  103. }
  104. if (action.equalsIgnoreCase("deviceready")) {
  105. cordova.getThreadPool().execute( new Runnable() {
  106. public void run() {
  107. deviceready();
  108. }
  109. });
  110. }
  111. if (action.equalsIgnoreCase("pause")) {
  112. isInBackground = true;
  113. }
  114. if (action.equalsIgnoreCase("resume")) {
  115. isInBackground = false;
  116. }
  117. return true;
  118. }
  119. /**
  120. * Calls all pending callbacks after the deviceready event has been fired.
  121. */
  122. private static void deviceready () {
  123. deviceready = true;
  124. for (String js : eventQueue) {
  125. webView.sendJavascript(js);
  126. }
  127. eventQueue.clear();
  128. }
  129. /**
  130. * Set an alarm.
  131. *
  132. * @param options
  133. * The options that can be specified per alarm.
  134. * @param doFireEvent
  135. * If the onadd callback shall be called.
  136. */
  137. public static void add (Options options, boolean doFireEvent) {
  138. long triggerTime = options.getDate();
  139. Intent intent = new Intent(context, Receiver.class)
  140. .setAction("" + options.getId())
  141. .putExtra(Receiver.OPTIONS, options.getJSONObject().toString());
  142. AlarmManager am = getAlarmManager();
  143. PendingIntent pi = PendingIntent.getBroadcast(context, 0, intent, PendingIntent.FLAG_CANCEL_CURRENT);
  144. if (doFireEvent) {
  145. fireEvent("add", options.getId(), options.getJSON());
  146. }
  147. am.set(AlarmManager.RTC_WAKEUP, triggerTime, pi);
  148. }
  149. /**
  150. * Cancel a specific notification that was previously registered.
  151. *
  152. * @param notificationId
  153. * The original ID of the notification that was used when it was
  154. * registered using add()
  155. */
  156. public static void cancel (String notificationId) {
  157. /*
  158. * Create an intent that looks similar, to the one that was registered
  159. * using add. Making sure the notification id in the action is the same.
  160. * Now we can search for such an intent using the 'getService' method
  161. * and cancel it.
  162. */
  163. Intent intent = new Intent(context, Receiver.class)
  164. .setAction("" + notificationId);
  165. PendingIntent pi = PendingIntent.getBroadcast(context, 0, intent, 0);
  166. AlarmManager am = getAlarmManager();
  167. NotificationManager nc = getNotificationManager();
  168. am.cancel(pi);
  169. try {
  170. nc.cancel(Integer.parseInt(notificationId));
  171. } catch (Exception e) {}
  172. fireEvent("cancel", notificationId, "");
  173. }
  174. /**
  175. * Cancel all notifications that were created by this plugin.
  176. *
  177. * Android can only unregister a specific alarm. There is no such thing
  178. * as cancelAll. Therefore we rely on the Shared Preferences which holds
  179. * all our alarms to loop through these alarms and unregister them one
  180. * by one.
  181. */
  182. public static void cancelAll() {
  183. SharedPreferences settings = getSharedPreferences();
  184. NotificationManager nc = getNotificationManager();
  185. Map<String, ?> alarms = settings.getAll();
  186. Set<String> alarmIds = alarms.keySet();
  187. for (String alarmId : alarmIds) {
  188. cancel(alarmId);
  189. }
  190. nc.cancelAll();
  191. }
  192. /**
  193. * Checks if a notification with an ID is scheduled.
  194. *
  195. * @param id
  196. * The notification ID to be check.
  197. * @param callbackContext
  198. */
  199. public static void isScheduled (String id, CallbackContext command) {
  200. SharedPreferences settings = getSharedPreferences();
  201. Map<String, ?> alarms = settings.getAll();
  202. boolean isScheduled = alarms.containsKey(id);
  203. PluginResult result = new PluginResult(PluginResult.Status.OK, isScheduled);
  204. command.sendPluginResult(result);
  205. }
  206. /**
  207. * Retrieves a list with all currently pending notifications.
  208. *
  209. * @param callbackContext
  210. */
  211. public static void getScheduledIds (CallbackContext command) {
  212. SharedPreferences settings = getSharedPreferences();
  213. Map<String, ?> alarms = settings.getAll();
  214. Set<String> alarmIds = alarms.keySet();
  215. JSONArray scheduledIds = new JSONArray(alarmIds);
  216. command.success(scheduledIds);
  217. }
  218. /**
  219. * Checks if a notification with an ID was triggered.
  220. *
  221. * @param id
  222. * The notification ID to be check.
  223. * @param callbackContext
  224. */
  225. public static void isTriggered (String id, CallbackContext command) {
  226. SharedPreferences settings = getSharedPreferences();
  227. Map<String, ?> alarms = settings.getAll();
  228. boolean isScheduled = alarms.containsKey(id);
  229. boolean isTriggered = isScheduled;
  230. if (isScheduled) {
  231. JSONObject arguments = (JSONObject) alarms.get(id);
  232. Options options = new Options(context).parse(arguments);
  233. Date fireDate = new Date(options.getDate());
  234. isTriggered = new Date().after(fireDate);
  235. }
  236. PluginResult result = new PluginResult(PluginResult.Status.OK, isTriggered);
  237. command.sendPluginResult(result);
  238. }
  239. /**
  240. * Retrieves a list with all currently triggered notifications.
  241. *
  242. * @param callbackContext
  243. */
  244. public static void getTriggeredIds (CallbackContext command) {
  245. SharedPreferences settings = getSharedPreferences();
  246. Map<String, ?> alarms = settings.getAll();
  247. Set<String> alarmIds = alarms.keySet();
  248. JSONArray scheduledIds = new JSONArray();
  249. Date now = new Date();
  250. for (String id : alarmIds) {
  251. JSONObject arguments = (JSONObject) alarms.get(id);
  252. Options options = new Options(context).parse(arguments);
  253. Date fireDate = new Date(options.getDate());
  254. boolean isTriggered = now.after(fireDate);
  255. if (isTriggered == true) {
  256. scheduledIds.put(id);
  257. }
  258. }
  259. command.success(scheduledIds);
  260. }
  261. /**
  262. * Persist the information of this alarm to the Android Shared Preferences.
  263. * This will allow the application to restore the alarm upon device reboot.
  264. * Also this is used by the cancelAll method.
  265. *
  266. * @param alarmId
  267. * The Id of the notification that must be persisted.
  268. * @param args
  269. * The assumption is that parse has been called already.
  270. */
  271. public static void persist (String alarmId, JSONArray args) {
  272. Editor editor = getSharedPreferences().edit();
  273. if (alarmId != null) {
  274. editor.putString(alarmId, args.toString());
  275. if (Build.VERSION.SDK_INT<9) {
  276. editor.commit();
  277. } else {
  278. editor.apply();
  279. }
  280. }
  281. }
  282. /**
  283. * Remove a specific alarm from the Android shared Preferences.
  284. *
  285. * @param alarmId
  286. * The Id of the notification that must be removed.
  287. */
  288. public static void unpersist (String alarmId) {
  289. Editor editor = getSharedPreferences().edit();
  290. if (alarmId != null) {
  291. editor.remove(alarmId);
  292. if (Build.VERSION.SDK_INT<9) {
  293. editor.commit();
  294. } else {
  295. editor.apply();
  296. }
  297. }
  298. }
  299. /**
  300. * Clear all alarms from the Android shared Preferences.
  301. */
  302. public static void unpersistAll () {
  303. Editor editor = getSharedPreferences().edit();
  304. editor.clear();
  305. if (Build.VERSION.SDK_INT<9) {
  306. editor.commit();
  307. } else {
  308. editor.apply();
  309. }
  310. }
  311. /**
  312. * Fires the given event.
  313. *
  314. * @param {String} event The Name of the event
  315. * @param {String} id The ID of the notification
  316. * @param {String} json A custom (JSON) string
  317. */
  318. public static void fireEvent (String event, String id, String json) {
  319. String state = getApplicationState();
  320. String params = "\"" + id + "\",\"" + state + "\",\\'" + JSONObject.quote(json) + "\\'.replace(/(^\"|\"$)/g, \\'\\')";
  321. String js = "setTimeout('plugin.notification.local.on" + event + "(" + params + ")',0)";
  322. // webview may available, but callbacks needs to be executed
  323. // after deviceready
  324. if (deviceready == false) {
  325. eventQueue.add(js);
  326. } else {
  327. webView.sendJavascript(js);
  328. }
  329. }
  330. /**
  331. * Retrieves the application state
  332. *
  333. * @return {String}
  334. * Either "background" or "foreground"
  335. */
  336. protected static String getApplicationState () {
  337. return isInBackground ? "background" : "foreground";
  338. }
  339. /**
  340. * Set the application context if not already set.
  341. */
  342. protected static void setContext (Context context) {
  343. if (LocalNotification.context == null) {
  344. LocalNotification.context = context;
  345. }
  346. }
  347. /**
  348. * The Local storage for the application.
  349. */
  350. protected static SharedPreferences getSharedPreferences () {
  351. return context.getSharedPreferences(PLUGIN_NAME, Context.MODE_PRIVATE);
  352. }
  353. /**
  354. * The alarm manager for the application.
  355. */
  356. protected static AlarmManager getAlarmManager () {
  357. return (AlarmManager) context.getSystemService(Context.ALARM_SERVICE);
  358. }
  359. /**
  360. * The notification manager for the application.
  361. */
  362. protected static NotificationManager getNotificationManager () {
  363. return (NotificationManager) context.getSystemService(Context.NOTIFICATION_SERVICE);
  364. }
  365. }