Browse Source

Added back update method for Android

Sebastián Katzer 8 years ago
parent
commit
9a6d89b5b1

+ 24 - 24
src/android/LocalNotification.java

@@ -157,10 +157,10 @@ public class LocalNotification extends CordovaPlugin {
                     schedule(args);
                     command.success();
                 } else
-                // if (action.equals("update")) {
-                //     update(args);
-                //     command.success();
-                // } else
+                if (action.equals("update")) {
+                    update(args);
+                    command.success();
+                } else
                 if (action.equals("cancel")) {
                     cancel(args);
                     command.success();
@@ -290,26 +290,26 @@ public class LocalNotification extends CordovaPlugin {
         }
     }
 
-    // /**
-    //  * Update multiple local notifications.
-    //  *
-    //  * @param updates
-    //  *      Notification properties including their IDs
-    //  */
-    // private void update (JSONArray updates) {
-    //     for (int i = 0; i < updates.length(); i++) {
-    //         JSONObject update = updates.optJSONObject(i);
-    //         int id = update.optInt("id", 0);
-
-    //         Notification notification =
-    //                 getNotMgr().update(id, update, TriggerReceiver.class);
-
-    //         if (notification == null)
-    //             continue;
-
-    //         fireEvent("update", notification);
-    //     }
-    // }
+    /**
+     * Update multiple local notifications.
+     *
+     * @param updates
+     *      Notification properties including their IDs
+     */
+    private void update (JSONArray updates) {
+        for (int i = 0; i < updates.length(); i++) {
+            JSONObject update = updates.optJSONObject(i);
+            int id            = update.optInt("id", 0);
+
+            Notification notification =
+                    getNotMgr().update(id, update, TriggerReceiver.class);
+
+            if (notification == null)
+                continue;
+
+            fireEvent("update", notification);
+        }
+    }
 
     /**
      * Cancel multiple local notifications.

+ 6 - 2
src/android/TriggerReceiver.java

@@ -45,14 +45,18 @@ public class TriggerReceiver extends AbstractTriggerReceiver {
      */
     @Override
     public void onTrigger (Notification notification, Bundle bundle) {
-        int badge = notification.getOptions().getBadgeNumber();
+        boolean isUpdate = bundle.getBoolean(Notification.EXTRA_UPDATE, false);
+        int badge        = notification.getOptions().getBadgeNumber();
 
         if (badge > 0) {
             Manager.getInstance(notification.getContext()).setBadge(badge);
         }
 
         notification.show();
-        LocalNotification.fireEvent("trigger", notification);
+
+        if (!isUpdate) {
+            LocalNotification.fireEvent("trigger", notification);
+        }
     }
 
     /**

+ 18 - 52
src/android/notification/Manager.java

@@ -120,33 +120,23 @@ public final class Manager {
         mgr.createNotificationChannel(channel);
     }
 
-    // /**
-    //  * Clear local notification specified by ID.
-    //  *
-    //  * @param id
-    //  *      The notification ID
-    //  * @param updates
-    //  *      JSON object with notification options
-    //  * @param receiver
-    //  *      Receiver to handle the trigger event
-    //  */
-    // public Notification update (int id, JSONObject updates, Class<?> receiver) {
-    //     Notification notification = get(id);
-
-    //     if (notification == null)
-    //         return null;
-
-    //     notification.cancel();
-
-    //     JSONObject options = mergeJSONObjects(
-    //             notification.getOptions().getDict(), updates);
-
-    //     try {
-    //         options.put("updated", true);
-    //     } catch (JSONException ignore) {}
-
-    //     return schedule(options, receiver);
-    // }
+    /**
+     * Update local notification specified by ID.
+     *
+     * @param id       The notification ID.
+     * @param updates  JSON object with notification options.
+     * @param receiver Receiver to handle the trigger event.
+     */
+    public Notification update (int id, JSONObject updates, Class<?> receiver) {
+        Notification notification = get(id);
+
+        if (notification == null)
+            return null;
+
+        notification.update(updates, receiver);
+
+        return notification;
+    }
 
     /**
      * Clear local notification specified by ID.
@@ -386,30 +376,6 @@ public final class Manager {
         return new Notification(context, options);
     }
 
-    // /**
-    //  * Merge two JSON objects.
-    //  *
-    //  * @param obj1
-    //  *      JSON object
-    //  * @param obj2
-    //  *      JSON object with new options
-    //  */
-    // private JSONObject mergeJSONObjects (JSONObject obj1, JSONObject obj2) {
-    //     Iterator it = obj2.keys();
-
-    //     while (it.hasNext()) {
-    //         try {
-    //             String key = (String)it.next();
-
-    //             obj1.put(key, obj2.opt(key));
-    //         } catch (JSONException e) {
-    //             e.printStackTrace();
-    //         }
-    //     }
-
-    //     return obj1;
-    // }
-
     /**
      * Set the badge number of the app icon.
      *
@@ -444,7 +410,7 @@ public final class Manager {
     private NotificationManagerCompat getNotCompMgr() {
         return NotificationManagerCompat.from(context);
     }
-    
+
     /**
      * Notification manager compat for the application.
      */

+ 45 - 0
src/android/notification/Notification.java

@@ -39,6 +39,7 @@ import org.json.JSONObject;
 
 import java.util.ArrayList;
 import java.util.Date;
+import java.util.Iterator;
 import java.util.List;
 import java.util.Set;
 
@@ -62,6 +63,9 @@ public final class Notification {
     // Extra key for the id
     public static final String EXTRA_ID = "NOTIFICATION_ID";
 
+    // Extra key for the update flag
+    public static final String EXTRA_UPDATE = "NOTIFICATION_UPDATE";
+
     // Key for private preferences
     static final String PREF_KEY_ID = "NOTIFICATION_ID";
 
@@ -287,6 +291,27 @@ public final class Notification {
         getNotMgr().notify(getId(), builder.build());
     }
 
+    /**
+     * Update the notification properties.
+     *
+     * @param updates  The properties to update.
+     * @param receiver Receiver to handle the trigger event.
+     */
+    void update (JSONObject updates, Class<?> receiver) {
+        mergeJSONObjects(updates);
+        persist(null);
+
+        if (getType() != Type.TRIGGERED)
+            return;
+
+        Intent intent = new Intent(context, receiver)
+                .setAction(PREF_KEY_ID + options.getId())
+                .putExtra(Notification.EXTRA_ID, options.getId())
+                .putExtra(Notification.EXTRA_UPDATE, true);
+
+        trigger(intent, receiver);
+    }
+
     /**
      * Encode options to JSON.
      */
@@ -318,6 +343,9 @@ public final class Notification {
         editor.putString(id, options.toString());
         editor.apply();
 
+        if (ids == null)
+            return;
+
         editor = getPrefs(PREF_KEY_PID).edit();
         editor.putStringSet(id, ids);
         editor.apply();
@@ -354,6 +382,23 @@ public final class Notification {
                 Intent.FLAG_GRANT_READ_URI_PERMISSION);
     }
 
+    /**
+     * Merge two JSON objects.
+     */
+    private void mergeJSONObjects (JSONObject updates) {
+        JSONObject dict = options.getDict();
+        Iterator it     = updates.keys();
+
+        while (it.hasNext()) {
+            try {
+                String key = (String)it.next();
+                dict.put(key, updates.opt(key));
+            } catch (JSONException e) {
+                e.printStackTrace();
+            }
+        }
+    }
+
     /**
      * Shared private preferences for the application.
      */

+ 0 - 1
src/android/notification/Options.java

@@ -31,7 +31,6 @@ import android.support.v4.media.session.MediaSessionCompat;
 
 import org.json.JSONArray;
 import org.json.JSONObject;
-import org.json.JSONArray;
 
 import java.io.IOException;
 import java.util.ArrayList;

+ 7 - 2
www/local-notification-util.js

@@ -164,12 +164,11 @@ exports.convertProperties = function (options) {
  * @return [ Map ] Interaction object with category & actions.
  */
 exports.convertActions = function (options) {
+    var actions = [];
 
     if (!options.actions)
         return null;
 
-    var actions = [];
-
     for (var action of options.actions) {
 
         if (!action.id) {
@@ -200,6 +199,9 @@ exports.convertTrigger = function (options) {
     var trigger  = options.trigger || {},
         date     = this.getValueFor(trigger, 'at', 'firstAt', 'date');
 
+    if (!options.trigger)
+        return;
+
     if (!trigger.type) {
         trigger.type = trigger.center ? 'location' : 'calendar';
     }
@@ -262,6 +264,9 @@ exports.convertProgressBar = function (options) {
     var isAndroid = device.platform == 'Android',
         cfg       = options.progressBar;
 
+    if (cfg === undefined)
+        return;
+
     if (typeof cfg === 'boolean') {
         cfg = options.progressBar = { enabled: cfg };
     }