Commit 69914bc6 by Sebastián Katzer

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

parent 519c452b
...@@ -21,6 +21,12 @@ ...@@ -21,6 +21,12 @@
package de.appplant.cordova.plugin.localnotification; 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; import de.appplant.cordova.plugin.notification.Notification;
/** /**
...@@ -33,19 +39,25 @@ public class ClickActivity extends de.appplant.cordova.plugin.notification.activ ...@@ -33,19 +39,25 @@ public class ClickActivity extends de.appplant.cordova.plugin.notification.activ
/** /**
* Called when local notification was clicked by the user. * 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 @Override
public void onClick(Notification notification) { public void onClick(String action, Notification notification) {
LocalNotification.fireEvent("click", 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()) LocalNotification.fireEvent(action, notification, data);
return;
String event = notification.isRepeating() ? "clear" : "cancel"; super.onClick(action, notification);
LocalNotification.fireEvent(event, notification);
} }
} }
...@@ -61,7 +61,7 @@ public class LocalNotification extends CordovaPlugin { ...@@ -61,7 +61,7 @@ public class LocalNotification extends CordovaPlugin {
private static Boolean deviceready = false; private static Boolean deviceready = false;
// To inform the user about the state of the app in callbacks // 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 // Queues all events before deviceready
private static ArrayList<String> eventQueue = new ArrayList<String>(); private static ArrayList<String> eventQueue = new ArrayList<String>();
...@@ -70,8 +70,6 @@ public class LocalNotification extends CordovaPlugin { ...@@ -70,8 +70,6 @@ public class LocalNotification extends CordovaPlugin {
* Called after plugin construction and fields have been initialized. * Called after plugin construction and fields have been initialized.
* Prefer to use pluginInitialize instead since there is no value in * Prefer to use pluginInitialize instead since there is no value in
* having parameters on the initialize() function. * having parameters on the initialize() function.
*
* pluginInitialize is not available for cordova 3.0-3.5 !
*/ */
@Override @Override
public void initialize (CordovaInterface cordova, CordovaWebView webView) { public void initialize (CordovaInterface cordova, CordovaWebView webView) {
...@@ -223,7 +221,7 @@ public class LocalNotification extends CordovaPlugin { ...@@ -223,7 +221,7 @@ public class LocalNotification extends CordovaPlugin {
* JavaScript. * JavaScript.
*/ */
private void check (CallbackContext command) { private void check (CallbackContext command) {
boolean allowed = getNotificationMgr().hasPermission(); boolean allowed = getNotificationMgr().hasPermission();
PluginResult result = new PluginResult(PluginResult.Status.OK, allowed); PluginResult result = new PluginResult(PluginResult.Status.OK, allowed);
command.sendPluginResult(result); command.sendPluginResult(result);
...@@ -268,7 +266,7 @@ public class LocalNotification extends CordovaPlugin { ...@@ -268,7 +266,7 @@ public class LocalNotification extends CordovaPlugin {
Notification notification = Notification notification =
mgr.schedule(request, TriggerReceiver.class); mgr.schedule(request, TriggerReceiver.class);
fireEvent("schedule", notification); fireEvent("add", notification);
} }
} }
...@@ -583,31 +581,56 @@ public class LocalNotification extends CordovaPlugin { ...@@ -583,31 +581,56 @@ public class LocalNotification extends CordovaPlugin {
/** /**
* Fire given event on JS side. Does inform all event listeners. * Fire given event on JS side. Does inform all event listeners.
* *
* @param event * @param event The event name.
* The event name
*/ */
private void fireEvent (String event) { private void fireEvent (String event) {
fireEvent(event, null); fireEvent(event, null, new JSONObject());
} }
/** /**
* Fire given event on JS side. Does inform all event listeners. * Fire given event on JS side. Does inform all event listeners.
* *
* @param event The event name * @param event The event name.
* @param notification Optional notification to pass the id and properties. * @param notification Optional notification to pass with.
*/ */
static void fireEvent (String event, Notification notification) { static void fireEvent (String event, Notification notification) {
String state = getApplicationState(); fireEvent(event, notification, new JSONObject());
String params = "\"" + state + "\""; }
/**
* 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) { 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 + ")"; "\"" + event + "\"," + params + ")";
// sendJavascript(js); sendJavascript(js);
} }
/** /**
...@@ -652,15 +675,6 @@ public class LocalNotification extends CordovaPlugin { ...@@ -652,15 +675,6 @@ public class LocalNotification extends CordovaPlugin {
// } // }
/** /**
* Current application state.
*
* @return "background" or "foreground"
*/
static String getApplicationState() {
return isInBackground ? "background" : "foreground";
}
/**
* Notification manager instance. * Notification manager instance.
*/ */
private Manager getNotificationMgr() { private Manager getNotificationMgr() {
......
...@@ -35,10 +35,13 @@ import de.appplant.cordova.plugin.notification.util.AssetUtil; ...@@ -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 * that it may be generated each time the notification is built. Necessary to
* compensate for missing functionality in the support library. * compensate for missing functionality in the support library.
*/ */
final class Action { public final class Action {
// Key name for bundled extras // 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 // The application context
private final Context context; private final Context context;
......
...@@ -279,6 +279,7 @@ public final class Builder { ...@@ -279,6 +279,7 @@ public final class Builder {
Intent intent = new Intent(context, clickActivity) Intent intent = new Intent(context, clickActivity)
.putExtra(Options.EXTRA, options.toString()) .putExtra(Options.EXTRA, options.toString())
.putExtra(Action.EXTRA, Action.CLICK_ACTION_ID)
.setFlags(Intent.FLAG_ACTIVITY_NO_HISTORY); .setFlags(Intent.FLAG_ACTIVITY_NO_HISTORY);
int reqCode = new Random().nextInt(); int reqCode = new Random().nextInt();
......
...@@ -29,9 +29,12 @@ import android.os.Bundle; ...@@ -29,9 +29,12 @@ import android.os.Bundle;
import org.json.JSONException; import org.json.JSONException;
import org.json.JSONObject; import org.json.JSONObject;
import de.appplant.cordova.plugin.notification.Action;
import de.appplant.cordova.plugin.notification.Notification; import de.appplant.cordova.plugin.notification.Notification;
import de.appplant.cordova.plugin.notification.Options; 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 * Abstract content receiver activity for local notifications. Creates the
* local notification and calls the event functions for further proceeding. * local notification and calls the event functions for further proceeding.
...@@ -52,6 +55,7 @@ abstract public class AbstractClickActivity extends Activity { ...@@ -52,6 +55,7 @@ abstract public class AbstractClickActivity extends Activity {
Context context = getApplicationContext(); Context context = getApplicationContext();
try { try {
String action = bundle.getString(Action.EXTRA, CLICK_ACTION_ID);
String data = bundle.getString(Options.EXTRA); String data = bundle.getString(Options.EXTRA);
JSONObject dict = new JSONObject(data); JSONObject dict = new JSONObject(data);
Options options = new Options(context, dict); Options options = new Options(context, dict);
...@@ -59,7 +63,7 @@ abstract public class AbstractClickActivity extends Activity { ...@@ -59,7 +63,7 @@ abstract public class AbstractClickActivity extends Activity {
Notification notification = Notification notification =
new Notification(context, options); new Notification(context, options);
onClick(notification); onClick(action, notification);
} catch (JSONException e) { } catch (JSONException e) {
e.printStackTrace(); e.printStackTrace();
} }
...@@ -78,9 +82,10 @@ abstract public class AbstractClickActivity extends Activity { ...@@ -78,9 +82,10 @@ abstract public class AbstractClickActivity extends Activity {
/** /**
* Called when local notification was clicked by the user. * 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. * Launch main intent from package.
......
...@@ -34,12 +34,16 @@ public class ClickActivity extends AbstractClickActivity { ...@@ -34,12 +34,16 @@ public class ClickActivity extends AbstractClickActivity {
* Called when local notification was clicked by the user. Will * Called when local notification was clicked by the user. Will
* move the app to foreground. * 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 @Override
public void onClick(Notification notification) { public void onClick(String action, Notification notification) {
launchApp(); launchApp();
if (notification.getOptions().isSticky())
return;
if (notification.isRepeating()) { if (notification.isRepeating()) {
notification.clear(); notification.clear();
} else { } else {
......
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment