Эх сурвалжийг харах

Fixed the isTriggered, and getTriggeredIds functions on android

Known Quirks are documented in the README.md
PKnittel 11 жил өмнө
parent
commit
5b863fbbde

+ 2 - 0
README.md

@@ -499,6 +499,8 @@ The launch mode for the main activity has to be set to `singleInstance`
 <activity ... android:launchMode="singleInstance" ... />
 ```
 
+### A notification cleared by the User is still shown as Triggered on Android
+It's not possible on android, to get informed about a User clearing an applications notifications. Currently the only way to prevent that sort of behavior is, to set the "ongoing" parameter to "true" and cancel the notification during the onclick event.
 
 ## Contributing
 

+ 90 - 20
src/android/LocalNotification.java

@@ -72,11 +72,10 @@ public class LocalNotification extends CordovaPlugin {
     public boolean execute (String action, final JSONArray args, final CallbackContext command) throws JSONException {
         if (action.equalsIgnoreCase("add")) {
             cordova.getThreadPool().execute( new Runnable() {
-                public void run() {
-                    JSONObject arguments = args.optJSONObject(0);
+                public void run() {               	
+                    JSONObject arguments = setInitDate(args).optJSONObject(0);
                     Options options      = new Options(context).parse(arguments);
 
-                    persist(options.getId(), args);
                     add(options, true);
                     command.success();
                 }
@@ -167,6 +166,8 @@ public class LocalNotification extends CordovaPlugin {
      */
     public static void add (Options options, boolean doFireEvent) {
         long triggerTime = options.getDate();
+        
+        persist(options.getId(), options.getJSONObject());
 
         Intent intent = new Intent(context, Receiver.class)
             .setAction("" + options.getId())
@@ -244,7 +245,23 @@ public class LocalNotification extends CordovaPlugin {
         SharedPreferences settings = getSharedPreferences();
         Map<String, ?> alarms      = settings.getAll();
         boolean isScheduled        = alarms.containsKey(id);
-        PluginResult result        = new PluginResult(PluginResult.Status.OK, isScheduled);
+        boolean isNotTriggered	   = false;
+        
+        if (isScheduled) {
+        	JSONObject arguments;
+			try {
+				arguments = new JSONObject(alarms.get(id).toString());
+	        	Options options      = new Options(context).parse(arguments);
+	        	Date fireDate        = new Date(options.getDate());
+	        	isNotTriggered = new Date().before(fireDate);
+			} catch (JSONException e) {
+				isNotTriggered = false;
+				e.printStackTrace();
+			}
+        	
+        }
+        
+        PluginResult result        = new PluginResult(PluginResult.Status.OK, (isScheduled && isNotTriggered));
 
         command.sendPluginResult(result);
     }
@@ -258,8 +275,27 @@ public class LocalNotification extends CordovaPlugin {
         SharedPreferences settings = getSharedPreferences();
         Map<String, ?> alarms      = settings.getAll();
         Set<String> alarmIds       = alarms.keySet();
-        JSONArray scheduledIds     = new JSONArray(alarmIds);
+        JSONArray scheduledIds     = new JSONArray();
+        
+        for (String id : alarmIds) {
+        	boolean isScheduled;
+        	JSONObject arguments;
+ 			try {
+ 				arguments = new JSONObject(alarms.get(id).toString());
+ 	        	Options options      = new Options(context).parse(arguments);
+ 	        	Date fireDate        = new Date(options.getDate());
+ 	        	isScheduled = new Date().before(fireDate);
+ 			} catch (JSONException e) {
+ 				isScheduled = false;
+ 				e.printStackTrace();
+ 			}
+ 			if (isScheduled){
+ 				scheduledIds.put(id);
+ 			}
+        }
 
+        
+        
         command.success(scheduledIds);
     }
 
@@ -277,13 +313,19 @@ public class LocalNotification extends CordovaPlugin {
         boolean isTriggered        = isScheduled;
 
         if (isScheduled) {
-            JSONObject arguments = (JSONObject) alarms.get(id);
-            Options options      = new Options(context).parse(arguments);
-            Date fireDate        = new Date(options.getDate());
-
-            isTriggered = new Date().after(fireDate);
+        	JSONObject arguments;
+			try {
+				arguments = new JSONObject(alarms.get(id).toString());
+	        	Options options      = new Options(context).parse(arguments);
+	        	Date fireDate        = new Date(options.getInitialDate());
+	        	isTriggered = new Date().after(fireDate);
+			} catch (JSONException e) {
+				isTriggered = false;
+				e.printStackTrace();
+			}
+        	
         }
-
+        	
         PluginResult result = new PluginResult(PluginResult.Status.OK, isTriggered);
 
         command.sendPluginResult(result);
@@ -298,21 +340,32 @@ public class LocalNotification extends CordovaPlugin {
         SharedPreferences settings = getSharedPreferences();
         Map<String, ?> alarms      = settings.getAll();
         Set<String> alarmIds       = alarms.keySet();
-        JSONArray scheduledIds     = new JSONArray();
+        JSONArray triggeredIds     = new JSONArray();
         Date now                   = new Date();
 
         for (String id : alarmIds) {
-            JSONObject arguments = (JSONObject) alarms.get(id);
-            Options options      = new Options(context).parse(arguments);
-            Date fireDate        = new Date(options.getDate());
-            boolean isTriggered  = now.after(fireDate);
+        	boolean isTriggered;
+        	JSONObject arguments;
+        	try{
+        		arguments = new JSONObject(alarms.get(id).toString());
+        		Options options      = new Options(context).parse(arguments);
+        		Date fireDate        = new Date(options.getInitialDate());
+        		isTriggered  = now.after(fireDate);
+            } catch(ClassCastException cce) {
+            	cce.printStackTrace();
+            	isTriggered = false;
+            }
+        	catch(JSONException jse) {
+        		jse.printStackTrace();
+            	isTriggered = false;
+            }
 
             if (isTriggered == true) {
-                scheduledIds.put(id);
+                triggeredIds.put(id);
             }
         }
 
-        command.success(scheduledIds);
+        command.success(triggeredIds);
     }
 
     /**
@@ -325,7 +378,7 @@ public class LocalNotification extends CordovaPlugin {
      * @param args
      *            The assumption is that parse has been called already.
      */
-    public static void persist (String alarmId, JSONArray args) {
+    public static void persist (String alarmId, JSONObject args) {
         Editor editor = getSharedPreferences().edit();
 
         if (alarmId != null) {
@@ -431,4 +484,21 @@ public class LocalNotification extends CordovaPlugin {
     protected static NotificationManager getNotificationManager () {
         return (NotificationManager) context.getSystemService(Context.NOTIFICATION_SERVICE);
     }
-}
+    
+    /**
+     * Function to set the value of "initialDate" in the JSONArray
+     * @param args The given JSONArray
+     * @return A new JSONArray with the parameter "initialDate" set.
+     */
+    private static JSONArray setInitDate(JSONArray args){
+    	long initialDate = args.optJSONObject(0).optLong("date", 0) * 1000;
+    	try {
+			args.optJSONObject(0).put("initialDate", initialDate);
+		} catch (JSONException e) {
+			e.printStackTrace();
+		}
+    	return args;
+    }
+    
+  
+}

+ 8 - 1
src/android/Options.java

@@ -115,6 +115,13 @@ public class Options {
     public long getDate() {
         return options.optLong("date", 0) * 1000;
     }
+    
+    /**
+     * Returns time in milliseconds when the notification was scheduled first
+     */
+    public long getInitialDate() {
+    	return options.optLong("initialDate", 0);
+    }
 
     /**
      * Returns time as calender
@@ -359,4 +366,4 @@ public class Options {
 
         return bmp;
     }
-}
+}

+ 0 - 1
src/android/Receiver.java

@@ -74,7 +74,6 @@ public class Receiver extends BroadcastReceiver {
         fireTriggerEvent();
 
         if (options.getInterval() == 0) {
-            LocalNotification.unpersist(options.getId());
         } else if (isFirstAlarmInFuture()) {
             return;
         } else {