Commit 99abac4a by Sebastián Katzer

`cancel` && `cancelAll` now removes the notification(s) from the notification…

`cancel` && `cancelAll` now removes the notification(s) from the notification center as well, + some code refactoring.
parent 0c0b1079
...@@ -14,6 +14,7 @@ import java.util.Set; ...@@ -14,6 +14,7 @@ import java.util.Set;
import org.json.JSONArray; import org.json.JSONArray;
import android.app.AlarmManager; import android.app.AlarmManager;
import android.app.NotificationManager;
import android.app.PendingIntent; import android.app.PendingIntent;
import android.content.Context; import android.content.Context;
import android.content.Intent; import android.content.Intent;
...@@ -36,10 +37,10 @@ public class Helper { ...@@ -36,10 +37,10 @@ public class Helper {
*/ */
public void add (Options options) { public void add (Options options) {
long triggerTime = options.getDate(); long triggerTime = options.getDate();
Intent intent = new Intent(context, Receiver.class);
intent.setAction("" + options.getId()); Intent intent = new Intent(context, Receiver.class)
intent.putExtra(Receiver.OPTIONS, options.getJSONObject().toString()); .setAction("" + options.getId())
.putExtra(Receiver.OPTIONS, options.getJSONObject().toString());
AlarmManager am = getAlarmManager(); AlarmManager am = getAlarmManager();
PendingIntent pi = PendingIntent.getBroadcast(context, 0, intent, PendingIntent.FLAG_CANCEL_CURRENT); PendingIntent pi = PendingIntent.getBroadcast(context, 0, intent, PendingIntent.FLAG_CANCEL_CURRENT);
...@@ -66,15 +67,15 @@ public class Helper { ...@@ -66,15 +67,15 @@ public class Helper {
* and cancel it. * and cancel it.
*/ */
Intent intent = new Intent(context, Receiver.class); Intent intent = new Intent(context, Receiver.class)
intent.setAction("" + notificationId); .setAction("" + notificationId);
PendingIntent pi = PendingIntent.getBroadcast(context, 0, intent, PendingIntent.FLAG_CANCEL_CURRENT); PendingIntent pi = PendingIntent.getBroadcast(context, 0, intent, PendingIntent.FLAG_CANCEL_CURRENT);
AlarmManager am = getAlarmManager(); AlarmManager am = getAlarmManager();
NotificationManager nc = getNotificationManager();
try {
am.cancel(pi); am.cancel(pi);
} catch (Exception e) {} nc.cancel(Integer.parseInt(notificationId));
} }
/** /**
...@@ -86,13 +87,16 @@ public class Helper { ...@@ -86,13 +87,16 @@ public class Helper {
* by one. * by one.
*/ */
public void cancelAll() { public void cancelAll() {
SharedPreferences settings = context.getSharedPreferences(LocalNotification.PLUGIN_NAME, Context.MODE_PRIVATE); SharedPreferences settings = LocalNotification.getSharedPreferences();
NotificationManager nc = getNotificationManager();
Map<String, ?> alarms = settings.getAll(); Map<String, ?> alarms = settings.getAll();
Set<String> alarmIds = alarms.keySet(); Set<String> alarmIds = alarms.keySet();
for (String alarmId : alarmIds) { for (String alarmId : alarmIds) {
cancel(alarmId); cancel(alarmId);
} }
nc.cancelAll();
} }
/** /**
...@@ -100,11 +104,13 @@ public class Helper { ...@@ -100,11 +104,13 @@ public class Helper {
* This will allow the application to restore the alarm upon device reboot. * This will allow the application to restore the alarm upon device reboot.
* Also this is used by the cancelAll method. * Also this is used by the cancelAll method.
* *
* @param alarmId
* The Id of the notification that must be persisted.
* @param args * @param args
* The assumption is that parse has been called already. * The assumption is that parse has been called already.
*/ */
public void persist (String alarmId, JSONArray args) { public void persist (String alarmId, JSONArray args) {
Editor editor = context.getSharedPreferences(LocalNotification.PLUGIN_NAME, Context.MODE_PRIVATE).edit(); Editor editor = LocalNotification.getSharedPreferences().edit();
if (alarmId != null) { if (alarmId != null) {
editor.putString(alarmId, args.toString()); editor.putString(alarmId, args.toString());
...@@ -119,7 +125,7 @@ public class Helper { ...@@ -119,7 +125,7 @@ public class Helper {
* The Id of the notification that must be removed. * The Id of the notification that must be removed.
*/ */
public void unpersist (String alarmId) { public void unpersist (String alarmId) {
Editor editor = context.getSharedPreferences(LocalNotification.PLUGIN_NAME, Context.MODE_PRIVATE).edit(); Editor editor = LocalNotification.getSharedPreferences().edit();
if (alarmId != null) { if (alarmId != null) {
editor.remove(alarmId); editor.remove(alarmId);
...@@ -131,7 +137,7 @@ public class Helper { ...@@ -131,7 +137,7 @@ public class Helper {
* Clear all alarms from the Android shared Preferences. * Clear all alarms from the Android shared Preferences.
*/ */
public void unpersistAll () { public void unpersistAll () {
Editor editor = context.getSharedPreferences(LocalNotification.PLUGIN_NAME, Context.MODE_PRIVATE).edit(); Editor editor = LocalNotification.getSharedPreferences().edit();
editor.clear(); editor.clear();
editor.commit(); editor.commit();
...@@ -140,4 +146,8 @@ public class Helper { ...@@ -140,4 +146,8 @@ public class Helper {
private AlarmManager getAlarmManager () { private AlarmManager getAlarmManager () {
return (AlarmManager) context.getSystemService(Context.ALARM_SERVICE); return (AlarmManager) context.getSystemService(Context.ALARM_SERVICE);
} }
private NotificationManager getNotificationManager () {
return (NotificationManager) context.getSystemService(Context.NOTIFICATION_SERVICE);
}
} }
\ No newline at end of file
...@@ -13,6 +13,7 @@ import org.json.JSONException; ...@@ -13,6 +13,7 @@ import org.json.JSONException;
import org.json.JSONObject; import org.json.JSONObject;
import android.content.Context; import android.content.Context;
import android.content.SharedPreferences;
import org.apache.cordova.CordovaPlugin; import org.apache.cordova.CordovaPlugin;
import org.apache.cordova.CallbackContext; import org.apache.cordova.CallbackContext;
...@@ -26,7 +27,7 @@ import org.apache.cordova.CordovaWebView; ...@@ -26,7 +27,7 @@ import org.apache.cordova.CordovaWebView;
*/ */
public class LocalNotification extends CordovaPlugin { public class LocalNotification extends CordovaPlugin {
public static final String PLUGIN_NAME = "LocalNotification"; public final static String PLUGIN_NAME = "LocalNotification";
public static CordovaWebView webView = null; public static CordovaWebView webView = null;
public static Context context = null; public static Context context = null;
...@@ -77,9 +78,7 @@ public class LocalNotification extends CordovaPlugin { ...@@ -77,9 +78,7 @@ public class LocalNotification extends CordovaPlugin {
* The options that can be specified per alarm. * The options that can be specified per alarm.
*/ */
public static void add (Options options) { public static void add (Options options) {
Helper helper = new Helper(context); getHelper().add(options);
helper.add(options);
} }
/** /**
...@@ -90,9 +89,7 @@ public class LocalNotification extends CordovaPlugin { ...@@ -90,9 +89,7 @@ public class LocalNotification extends CordovaPlugin {
* registered using add() * registered using add()
*/ */
public static void cancel (String notificationId) { public static void cancel (String notificationId) {
Helper helper = new Helper(context); getHelper().cancel(notificationId);
helper.cancel(notificationId);
} }
/** /**
...@@ -104,9 +101,7 @@ public class LocalNotification extends CordovaPlugin { ...@@ -104,9 +101,7 @@ public class LocalNotification extends CordovaPlugin {
* by one. * by one.
*/ */
public static void cancelAll() { public static void cancelAll() {
Helper helper = new Helper(context); getHelper().cancelAll();
helper.cancelAll();
} }
/** /**
...@@ -114,13 +109,13 @@ public class LocalNotification extends CordovaPlugin { ...@@ -114,13 +109,13 @@ public class LocalNotification extends CordovaPlugin {
* This will allow the application to restore the alarm upon device reboot. * This will allow the application to restore the alarm upon device reboot.
* Also this is used by the cancelAll method. * Also this is used by the cancelAll method.
* *
* @param alarmId
* The Id of the notification that must be persisted.
* @param args * @param args
* The assumption is that parse has been called already. * The assumption is that parse has been called already.
*/ */
public static void persist (String alarmId, JSONArray args) { public static void persist (String alarmId, JSONArray args) {
Helper helper = new Helper(context); getHelper().persist(alarmId, args);
helper.persist(alarmId, args);
} }
/** /**
...@@ -130,18 +125,25 @@ public class LocalNotification extends CordovaPlugin { ...@@ -130,18 +125,25 @@ public class LocalNotification extends CordovaPlugin {
* The Id of the notification that must be removed. * The Id of the notification that must be removed.
*/ */
public static void unpersist (String alarmId) { public static void unpersist (String alarmId) {
Helper helper = new Helper(context); getHelper().unpersist(alarmId);
helper.unpersist(alarmId);
} }
/** /**
* Clear all alarms from the Android shared Preferences. * Clear all alarms from the Android shared Preferences.
*/ */
public static void unpersistAll () { public static void unpersistAll () {
Helper helper = new Helper(context); getHelper().unpersistAll();
}
/**
* Local storage of the application.
*/
public static SharedPreferences getSharedPreferences () {
return context.getSharedPreferences(PLUGIN_NAME, Context.MODE_PRIVATE);
}
helper.unpersistAll(); private final static Helper getHelper () {
return new Helper(context);
} }
/** /**
...@@ -149,6 +151,6 @@ public class LocalNotification extends CordovaPlugin { ...@@ -149,6 +151,6 @@ public class LocalNotification extends CordovaPlugin {
*/ */
private void rememberCordovaVarsForStaticUse () { private void rememberCordovaVarsForStaticUse () {
LocalNotification.webView = super.webView; LocalNotification.webView = super.webView;
LocalNotification.context = super.cordova.getActivity(); LocalNotification.context = super.cordova.getActivity().getApplicationContext();
} }
} }
\ No newline at end of file
...@@ -47,7 +47,9 @@ public class Receiver extends BroadcastReceiver { ...@@ -47,7 +47,9 @@ public class Receiver extends BroadcastReceiver {
try { try {
args = new JSONObject(bundle.getString(OPTIONS)); args = new JSONObject(bundle.getString(OPTIONS));
options = new Options(context).parse(args); options = new Options(context).parse(args);
} catch (JSONException e) {} } catch (JSONException e) {
return;
}
this.context = context; this.context = context;
this.options = options; this.options = options;
......
/** /**
* LocalNotificationRestore.java
* Cordova LocalNotification Plugin * Cordova LocalNotification Plugin
* *
* Created by Sebastian Katzer (github.com/katzer). * Created by Sebastian Katzer (github.com/katzer) on 31/08/2013.
* Copyright 2013 Sebastian Katzer. All rights reserved. * Copyright 2013 Sebastian Katzer. All rights reserved.
* LGPL v2.1 licensed * GPL v2 licensed
*/ */
package de.appplant.cordova.plugin.localnotification; package de.appplant.cordova.plugin.localnotification;
...@@ -27,10 +28,8 @@ public class Restore extends BroadcastReceiver { ...@@ -27,10 +28,8 @@ public class Restore extends BroadcastReceiver {
@Override @Override
public void onReceive(Context context, Intent intent) { public void onReceive(Context context, Intent intent) {
String pluginName = LocalNotification.PLUGIN_NAME;
// Obtain alarm details form Shared Preferences // Obtain alarm details form Shared Preferences
SharedPreferences alarms = context.getSharedPreferences(pluginName, Context.MODE_PRIVATE); SharedPreferences alarms = LocalNotification.getSharedPreferences();
Set<String> alarmIds = alarms.getAll().keySet(); Set<String> alarmIds = alarms.getAll().keySet();
/* /*
......
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