소스 검색

Added back events such add, trigger, clear and click and action events

Sebastián Katzer 8 년 전
부모
커밋
69914bc6da

+ 20 - 8
src/android/ClickActivity.java

@@ -21,6 +21,12 @@
 
 package de.appplant.cordova.plugin.localnotification;
 
+import android.os.Bundle;
+import android.support.v4.app.RemoteInput;
+
+import org.json.JSONException;
+import org.json.JSONObject;
+
 import de.appplant.cordova.plugin.notification.Notification;
 
 /**
@@ -33,19 +39,25 @@ public class ClickActivity extends de.appplant.cordova.plugin.notification.activ
     /**
      * Called when local notification was clicked by the user.
      *
-     * @param notification Wrapper around the local notification
+     * @param action       The name of the action.
+     * @param notification Wrapper around the local notification.
      */
     @Override
-    public void onClick(Notification notification) {
-        LocalNotification.fireEvent("click", notification);
+    public void onClick(String action, Notification notification) {
+        JSONObject data = new JSONObject();
+        Bundle input    = RemoteInput.getResultsFromIntent(getIntent());
 
-        super.onClick(notification);
+        if (input != null) {
+            try {
+                data.put("text", input.getString(action, ""));
+            } catch (JSONException e) {
+                e.printStackTrace();
+            }
+        }
 
-        if (notification.getOptions().isSticky())
-            return;
+        LocalNotification.fireEvent(action, notification, data);
 
-        String event = notification.isRepeating() ? "clear" : "cancel";
-        LocalNotification.fireEvent(event, notification);
+        super.onClick(action, notification);
     }
 
 }

+ 38 - 24
src/android/LocalNotification.java

@@ -61,7 +61,7 @@ public class LocalNotification extends CordovaPlugin {
     private static Boolean deviceready = false;
 
     // To inform the user about the state of the app in callbacks
-    protected static Boolean isInBackground = true;
+    private static Boolean isInBackground = true;
 
     // Queues all events before deviceready
     private static ArrayList<String> eventQueue = new ArrayList<String>();
@@ -70,8 +70,6 @@ public class LocalNotification extends CordovaPlugin {
      * Called after plugin construction and fields have been initialized.
      * Prefer to use pluginInitialize instead since there is no value in
      * having parameters on the initialize() function.
-     *
-     * pluginInitialize is not available for cordova 3.0-3.5 !
      */
     @Override
     public void initialize (CordovaInterface cordova, CordovaWebView webView) {
@@ -223,7 +221,7 @@ public class LocalNotification extends CordovaPlugin {
      *                JavaScript.
      */
     private void check (CallbackContext command) {
-        boolean allowed = getNotificationMgr().hasPermission();
+        boolean allowed     = getNotificationMgr().hasPermission();
         PluginResult result = new PluginResult(PluginResult.Status.OK, allowed);
 
         command.sendPluginResult(result);
@@ -268,7 +266,7 @@ public class LocalNotification extends CordovaPlugin {
             Notification notification =
                     mgr.schedule(request, TriggerReceiver.class);
 
-            fireEvent("schedule", notification);
+            fireEvent("add", notification);
         }
     }
 
@@ -583,31 +581,56 @@ public class LocalNotification extends CordovaPlugin {
     /**
      * Fire given event on JS side. Does inform all event listeners.
      *
-     * @param event
-     *      The event name
+     * @param event The event name.
      */
     private void fireEvent (String event) {
-        fireEvent(event, null);
+        fireEvent(event, null, new JSONObject());
     }
 
     /**
      * Fire given event on JS side. Does inform all event listeners.
      *
-     * @param event The event name
-     * @param notification Optional notification to pass the id and properties.
+     * @param event        The event name.
+     * @param notification Optional notification to pass with.
      */
     static void fireEvent (String event, Notification notification) {
-        String state = getApplicationState();
-        String params = "\"" + state + "\"";
+        fireEvent(event, notification, new JSONObject());
+    }
+
+    /**
+     * Fire given event on JS side. Does inform all event listeners.
+     *
+     * @param event        The event name.
+     * @param notification Optional notification to pass with.
+     * @param data         Event object with additional data.
+     */
+    static void fireEvent (String event, Notification notification,
+                           JSONObject data) {
+
+        String params, js;
+
+        try {
+            data.put("event", event);
+            data.put("foreground", !isInBackground);
+            data.put("queued", !deviceready);
+
+            if (notification != null) {
+                data.put("notification", notification.getId());
+            }
+        } catch (JSONException e) {
+            e.printStackTrace();
+        }
 
         if (notification != null) {
-            params = notification.toString() + "," + params;
+            params = notification.toString() + "," + data.toString();
+        } else {
+            params = data.toString();
         }
 
-        String js = "cordova.plugins.notification.local.core.fireEvent(" +
+        js = "cordova.plugins.notification.local.core.fireEvent(" +
                 "\"" + event + "\"," + params + ")";
 
-        // sendJavascript(js);
+        sendJavascript(js);
     }
 
     /**
@@ -651,15 +674,6 @@ public class LocalNotification extends CordovaPlugin {
     //     return list;
     // }
 
-    /**
-     * Current application state.
-     *
-     * @return "background" or "foreground"
-     */
-    static String getApplicationState() {
-        return isInBackground ? "background" : "foreground";
-    }
-
     /**
      * Notification manager instance.
      */

+ 5 - 2
src/android/notification/Action.java

@@ -35,10 +35,13 @@ import de.appplant.cordova.plugin.notification.util.AssetUtil;
  * that it may be generated each time the notification is built. Necessary to
  * compensate for missing functionality in the support library.
  */
-final class Action {
+public final class Action {
 
     // Key name for bundled extras
-    static final String EXTRA = "NOTIFICATION_ACTION_ID";
+    public static final String EXTRA = "NOTIFICATION_ACTION_ID";
+
+    // The id for the click action
+    public static final String CLICK_ACTION_ID = "click";
 
     // The application context
     private final Context context;

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

@@ -279,6 +279,7 @@ public final class Builder {
 
         Intent intent = new Intent(context, clickActivity)
                 .putExtra(Options.EXTRA, options.toString())
+                .putExtra(Action.EXTRA, Action.CLICK_ACTION_ID)
                 .setFlags(Intent.FLAG_ACTIVITY_NO_HISTORY);
 
         int reqCode = new Random().nextInt();

+ 8 - 3
src/android/notification/activity/AbstractClickActivity.java

@@ -29,9 +29,12 @@ import android.os.Bundle;
 import org.json.JSONException;
 import org.json.JSONObject;
 
+import de.appplant.cordova.plugin.notification.Action;
 import de.appplant.cordova.plugin.notification.Notification;
 import de.appplant.cordova.plugin.notification.Options;
 
+import static de.appplant.cordova.plugin.notification.Action.CLICK_ACTION_ID;
+
 /**
  * Abstract content receiver activity for local notifications. Creates the
  * local notification and calls the event functions for further proceeding.
@@ -52,6 +55,7 @@ abstract public class AbstractClickActivity extends Activity {
         Context context = getApplicationContext();
 
         try {
+            String action   = bundle.getString(Action.EXTRA, CLICK_ACTION_ID);
             String data     = bundle.getString(Options.EXTRA);
             JSONObject dict = new JSONObject(data);
             Options options = new Options(context, dict);
@@ -59,7 +63,7 @@ abstract public class AbstractClickActivity extends Activity {
             Notification notification =
                     new Notification(context, options);
 
-            onClick(notification);
+            onClick(action, notification);
         } catch (JSONException e) {
             e.printStackTrace();
         }
@@ -78,9 +82,10 @@ abstract public class AbstractClickActivity extends Activity {
     /**
      * Called when local notification was clicked by the user.
      *
-     * @param notification Wrapper around the local notification
+     * @param action       The name of the action.
+     * @param notification Wrapper around the local notification.
      */
-    abstract public void onClick (Notification notification);
+    abstract public void onClick (String action, Notification notification);
 
     /**
      * Launch main intent from package.

+ 6 - 2
src/android/notification/activity/ClickActivity.java

@@ -34,12 +34,16 @@ public class ClickActivity extends AbstractClickActivity {
      * Called when local notification was clicked by the user. Will
      * move the app to foreground.
      *
-     * @param notification Wrapper around the local notification
+     * @param action       The name of the action.
+     * @param notification Wrapper around the local notification.
      */
     @Override
-    public void onClick(Notification notification) {
+    public void onClick(String action, Notification notification) {
         launchApp();
 
+        if (notification.getOptions().isSticky())
+            return;
+
         if (notification.isRepeating()) {
             notification.clear();
         } else {