LocalNotification.java 6.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198
  1. /**
  2. * LocalNotification.java
  3. * Cordova LocalNotification Plugin
  4. *
  5. * Created by Sebastian Katzer (github.com/katzer) on 31/08/2013.
  6. * Copyright 2013 Sebastian Katzer. All rights reserved.
  7. * GPL v2 licensed
  8. */
  9. package de.appplant.cordova.plugin;
  10. import java.util.Map;
  11. import java.util.Set;
  12. import org.json.JSONArray;
  13. import org.json.JSONException;
  14. import org.json.JSONObject;
  15. import android.content.Intent;
  16. import android.content.Context;
  17. import android.app.AlarmManager;
  18. import android.app.PendingIntent;
  19. import android.content.SharedPreferences;
  20. import android.content.SharedPreferences.Editor;
  21. import org.apache.cordova.CordovaInterface;
  22. import org.apache.cordova.CordovaPlugin;
  23. import org.apache.cordova.CallbackContext;
  24. import org.apache.cordova.CordovaWebView;
  25. /**
  26. * This plugin utilizes the Android AlarmManager in combination with StatusBar
  27. * notifications. When a local notification is scheduled the alarm manager takes
  28. * care of firing the event. When the event is processed, a notification is put
  29. * in the Android status bar.
  30. */
  31. public class LocalNotification extends CordovaPlugin {
  32. public static final String PLUGIN_NAME = "LocalNotification";
  33. public static CordovaInterface cordova = null;
  34. public static CordovaWebView webView = null;
  35. @Override
  36. public boolean execute (String action, JSONArray args, CallbackContext callbackContext) throws JSONException {
  37. JSONObject arguments = args.getJSONObject(0);
  38. LocalNotificationOptions options = new LocalNotificationOptions(arguments);
  39. String alarmId = options.getId();
  40. rememberCordovaVarsForStaticUse();
  41. if (action.equalsIgnoreCase("add")) {
  42. persist(alarmId, args);
  43. add(options);
  44. return true;
  45. }
  46. if (action.equalsIgnoreCase("cancel")) {
  47. cancel(alarmId);
  48. unpersist(alarmId);
  49. return true;
  50. }
  51. if (action.equalsIgnoreCase("cancelAll")) {
  52. cancelAll();
  53. unpersistAll();
  54. return true;
  55. }
  56. // Returning false results in a "MethodNotFound" error.
  57. return false;
  58. }
  59. /**
  60. * Set an alarm
  61. *
  62. * @param options
  63. * The options that can be specified per alarm.
  64. */
  65. public static void add (LocalNotificationOptions options) {
  66. long triggerTime = options.getDate();
  67. Intent intent = new Intent(cordova.getActivity(), LocalNotificationReceiver.class);
  68. intent.setAction("" + options.getId());
  69. intent.putExtra(LocalNotificationReceiver.OPTIONS, options.getJSONObject().toString());
  70. AlarmManager am = getAlarmManager();
  71. PendingIntent sender = PendingIntent.getBroadcast(cordova.getActivity(), 0, intent, PendingIntent.FLAG_CANCEL_CURRENT);
  72. if (options.getInterval() > 0) {
  73. am.setRepeating(AlarmManager.RTC_WAKEUP, triggerTime, AlarmManager.INTERVAL_DAY, sender);
  74. } else {
  75. am.set(AlarmManager.RTC_WAKEUP, triggerTime, sender);
  76. }
  77. }
  78. /**
  79. * Cancel a specific notification that was previously registered.
  80. *
  81. * @param notificationId
  82. * The original ID of the notification that was used when it was
  83. * registered using add()
  84. */
  85. public static void cancel (String notificationId) {
  86. /*
  87. * Create an intent that looks similar, to the one that was registered
  88. * using add. Making sure the notification id in the action is the same.
  89. * Now we can search for such an intent using the 'getService' method
  90. * and cancel it.
  91. */
  92. Intent intent = new Intent(cordova.getActivity(), LocalNotificationReceiver.class);
  93. intent.setAction("" + notificationId);
  94. PendingIntent pi = PendingIntent.getBroadcast(cordova.getActivity(), 0, intent, PendingIntent.FLAG_CANCEL_CURRENT);
  95. AlarmManager am = getAlarmManager();
  96. try {
  97. am.cancel(pi);
  98. } catch (Exception e) {}
  99. }
  100. /**
  101. * Cancel all notifications that were created by this plugin.
  102. *
  103. * Android can only unregister a specific alarm. There is no such thing
  104. * as cancelAll. Therefore we rely on the Shared Preferences which holds
  105. * all our alarms to loop through these alarms and unregister them one
  106. * by one.
  107. */
  108. public static void cancelAll() {
  109. SharedPreferences settings = cordova.getActivity().getSharedPreferences(PLUGIN_NAME, Context.MODE_PRIVATE);
  110. Map<String, ?> alarms = settings.getAll();
  111. Set<String> alarmIds = alarms.keySet();
  112. for (String alarmId : alarmIds) {
  113. cancel(alarmId);
  114. }
  115. }
  116. /**
  117. * Persist the information of this alarm to the Android Shared Preferences.
  118. * This will allow the application to restore the alarm upon device reboot.
  119. * Also this is used by the cancelAll method.
  120. *
  121. * @param args
  122. * The assumption is that parse has been called already.
  123. */
  124. public static void persist (String alarmId, JSONArray args) {
  125. Editor editor = cordova.getActivity().getSharedPreferences(PLUGIN_NAME, Context.MODE_PRIVATE).edit();
  126. if (alarmId != null) {
  127. editor.putString(alarmId, args.toString());
  128. editor.commit();
  129. }
  130. }
  131. /**
  132. * Remove a specific alarm from the Android shared Preferences.
  133. *
  134. * @param alarmId
  135. * The Id of the notification that must be removed.
  136. */
  137. public static void unpersist (String alarmId) {
  138. Editor editor = cordova.getActivity().getSharedPreferences(PLUGIN_NAME, Context.MODE_PRIVATE).edit();
  139. if (alarmId != null) {
  140. editor.remove(alarmId);
  141. editor.commit();
  142. }
  143. }
  144. /**
  145. * Clear all alarms from the Android shared Preferences.
  146. */
  147. public static void unpersistAll () {
  148. Editor editor = cordova.getActivity().getSharedPreferences(PLUGIN_NAME, Context.MODE_PRIVATE).edit();
  149. editor.clear();
  150. editor.commit();
  151. }
  152. private static AlarmManager getAlarmManager () {
  153. return (AlarmManager) cordova.getActivity().getSystemService(Context.ALARM_SERVICE);
  154. }
  155. /**
  156. * Save required Cordova specific variables for later use.
  157. */
  158. private void rememberCordovaVarsForStaticUse () {
  159. LocalNotification.cordova = super.cordova;
  160. LocalNotification.webView = super.webView;
  161. }
  162. }