|
|
@@ -8,16 +8,23 @@
|
|
|
|
|
|
package de.appplant.cordova.plugin.localnotification;
|
|
|
|
|
|
+import java.util.Map;
|
|
|
+import java.util.Set;
|
|
|
+
|
|
|
+import org.apache.cordova.CallbackContext;
|
|
|
+import org.apache.cordova.CordovaPlugin;
|
|
|
+import org.apache.cordova.CordovaWebView;
|
|
|
import org.json.JSONArray;
|
|
|
import org.json.JSONException;
|
|
|
import org.json.JSONObject;
|
|
|
|
|
|
+import android.app.AlarmManager;
|
|
|
+import android.app.NotificationManager;
|
|
|
+import android.app.PendingIntent;
|
|
|
import android.content.Context;
|
|
|
+import android.content.Intent;
|
|
|
import android.content.SharedPreferences;
|
|
|
-
|
|
|
-import org.apache.cordova.CordovaPlugin;
|
|
|
-import org.apache.cordova.CallbackContext;
|
|
|
-import org.apache.cordova.CordovaWebView;
|
|
|
+import android.content.SharedPreferences.Editor;
|
|
|
|
|
|
/**
|
|
|
* This plugin utilizes the Android AlarmManager in combination with StatusBar
|
|
|
@@ -34,7 +41,8 @@ public class LocalNotification extends CordovaPlugin {
|
|
|
|
|
|
@Override
|
|
|
public boolean execute (String action, JSONArray args, CallbackContext callbackContext) throws JSONException {
|
|
|
- rememberCordovaVarsForStaticUse();
|
|
|
+ LocalNotification.webView = super.webView;
|
|
|
+ LocalNotification.context = super.cordova.getActivity().getApplicationContext();
|
|
|
|
|
|
if (action.equalsIgnoreCase("add")) {
|
|
|
JSONObject arguments = args.optJSONObject(0);
|
|
|
@@ -78,7 +86,20 @@ public class LocalNotification extends CordovaPlugin {
|
|
|
* The options that can be specified per alarm.
|
|
|
*/
|
|
|
public static void add (Options options) {
|
|
|
- getHelper().add(options);
|
|
|
+ long triggerTime = options.getDate();
|
|
|
+
|
|
|
+ Intent intent = new Intent(context, Receiver.class)
|
|
|
+ .setAction("" + options.getId())
|
|
|
+ .putExtra(Receiver.OPTIONS, options.getJSONObject().toString());
|
|
|
+
|
|
|
+ AlarmManager am = getAlarmManager();
|
|
|
+ PendingIntent pi = PendingIntent.getBroadcast(context, 0, intent, PendingIntent.FLAG_CANCEL_CURRENT);
|
|
|
+
|
|
|
+ if (options.getInterval() > 0) {
|
|
|
+ am.setRepeating(AlarmManager.RTC_WAKEUP, triggerTime, options.getInterval(), pi);
|
|
|
+ } else {
|
|
|
+ am.set(AlarmManager.RTC_WAKEUP, triggerTime, pi);
|
|
|
+ }
|
|
|
}
|
|
|
|
|
|
/**
|
|
|
@@ -89,7 +110,22 @@ public class LocalNotification extends CordovaPlugin {
|
|
|
* registered using add()
|
|
|
*/
|
|
|
public static void cancel (String notificationId) {
|
|
|
- getHelper().cancel(notificationId);
|
|
|
+ /*
|
|
|
+ * Create an intent that looks similar, to the one that was registered
|
|
|
+ * using add. Making sure the notification id in the action is the same.
|
|
|
+ * Now we can search for such an intent using the 'getService' method
|
|
|
+ * and cancel it.
|
|
|
+ */
|
|
|
+
|
|
|
+ Intent intent = new Intent(context, Receiver.class)
|
|
|
+ .setAction("" + notificationId);
|
|
|
+
|
|
|
+ PendingIntent pi = PendingIntent.getBroadcast(context, 0, intent, PendingIntent.FLAG_CANCEL_CURRENT);
|
|
|
+ AlarmManager am = getAlarmManager();
|
|
|
+ NotificationManager nc = getNotificationManager();
|
|
|
+
|
|
|
+ am.cancel(pi);
|
|
|
+ nc.cancel(Integer.parseInt(notificationId));
|
|
|
}
|
|
|
|
|
|
/**
|
|
|
@@ -101,7 +137,16 @@ public class LocalNotification extends CordovaPlugin {
|
|
|
* by one.
|
|
|
*/
|
|
|
public static void cancelAll() {
|
|
|
- getHelper().cancelAll();
|
|
|
+ SharedPreferences settings = getSharedPreferences();
|
|
|
+ NotificationManager nc = getNotificationManager();
|
|
|
+ Map<String, ?> alarms = settings.getAll();
|
|
|
+ Set<String> alarmIds = alarms.keySet();
|
|
|
+
|
|
|
+ for (String alarmId : alarmIds) {
|
|
|
+ cancel(alarmId);
|
|
|
+ }
|
|
|
+
|
|
|
+ nc.cancelAll();
|
|
|
}
|
|
|
|
|
|
/**
|
|
|
@@ -115,7 +160,12 @@ public class LocalNotification extends CordovaPlugin {
|
|
|
* The assumption is that parse has been called already.
|
|
|
*/
|
|
|
public static void persist (String alarmId, JSONArray args) {
|
|
|
- getHelper().persist(alarmId, args);
|
|
|
+ Editor editor = getSharedPreferences().edit();
|
|
|
+
|
|
|
+ if (alarmId != null) {
|
|
|
+ editor.putString(alarmId, args.toString());
|
|
|
+ editor.commit();
|
|
|
+ }
|
|
|
}
|
|
|
|
|
|
/**
|
|
|
@@ -125,32 +175,42 @@ public class LocalNotification extends CordovaPlugin {
|
|
|
* The Id of the notification that must be removed.
|
|
|
*/
|
|
|
public static void unpersist (String alarmId) {
|
|
|
- getHelper().unpersist(alarmId);
|
|
|
+ Editor editor = getSharedPreferences().edit();
|
|
|
+
|
|
|
+ if (alarmId != null) {
|
|
|
+ editor.remove(alarmId);
|
|
|
+ editor.commit();
|
|
|
+ }
|
|
|
}
|
|
|
|
|
|
/**
|
|
|
* Clear all alarms from the Android shared Preferences.
|
|
|
*/
|
|
|
public static void unpersistAll () {
|
|
|
- getHelper().unpersistAll();
|
|
|
+ Editor editor = LocalNotification.getSharedPreferences().edit();
|
|
|
+
|
|
|
+ editor.clear();
|
|
|
+ editor.commit();
|
|
|
}
|
|
|
|
|
|
/**
|
|
|
- * Local storage of the application.
|
|
|
+ * The Local storage for the application.
|
|
|
*/
|
|
|
public static SharedPreferences getSharedPreferences () {
|
|
|
return context.getSharedPreferences(PLUGIN_NAME, Context.MODE_PRIVATE);
|
|
|
}
|
|
|
|
|
|
- private final static Helper getHelper () {
|
|
|
- return new Helper(context);
|
|
|
+ /**
|
|
|
+ * The alarm manager for the application.
|
|
|
+ */
|
|
|
+ private static AlarmManager getAlarmManager () {
|
|
|
+ return (AlarmManager) context.getSystemService(Context.ALARM_SERVICE);
|
|
|
}
|
|
|
|
|
|
/**
|
|
|
- * Save required Cordova specific variables for later use.
|
|
|
+ * The notification manager for the application.
|
|
|
*/
|
|
|
- private void rememberCordovaVarsForStaticUse () {
|
|
|
- LocalNotification.webView = super.webView;
|
|
|
- LocalNotification.context = super.cordova.getActivity().getApplicationContext();
|
|
|
+ private static NotificationManager getNotificationManager () {
|
|
|
+ return (NotificationManager) context.getSystemService(Context.NOTIFICATION_SERVICE);
|
|
|
}
|
|
|
}
|