Parcourir la source

`cancel` && `cancelAll` now removes the notification(s) from the notification center as well, + some code refactoring.

Sebastián Katzer il y a 12 ans
Parent
commit
99abac4abe

+ 26 - 16
src/android/Helper.java

@@ -14,6 +14,7 @@ import java.util.Set;
 import org.json.JSONArray;
 
 import android.app.AlarmManager;
+import android.app.NotificationManager;
 import android.app.PendingIntent;
 import android.content.Context;
 import android.content.Intent;
@@ -22,10 +23,10 @@ import android.content.SharedPreferences.Editor;
 
 public class Helper {
 
-	private Context context;
+    private Context context;
 
     public Helper(Context context) {
-    	this.context = context;
+        this.context = context;
     }
 
     /**
@@ -36,10 +37,10 @@ public class Helper {
      */
     public void add (Options options) {
         long triggerTime = options.getDate();
-        Intent intent    = new Intent(context, Receiver.class);
 
-        intent.setAction("" + options.getId());
-        intent.putExtra(Receiver.OPTIONS, options.getJSONObject().toString());
+        Intent intent = new Intent(context, Receiver.class)
+            .setAction("" + options.getId())
+            .putExtra(Receiver.OPTIONS, options.getJSONObject().toString());
 
         AlarmManager am  = getAlarmManager();
         PendingIntent pi = PendingIntent.getBroadcast(context, 0, intent, PendingIntent.FLAG_CANCEL_CURRENT);
@@ -66,15 +67,15 @@ public class Helper {
          * and cancel it.
          */
 
-        Intent intent = new Intent(context, Receiver.class);
-        intent.setAction("" + notificationId);
+        Intent intent = new Intent(context, Receiver.class)
+            .setAction("" + notificationId);
 
-        PendingIntent pi = PendingIntent.getBroadcast(context, 0, intent, PendingIntent.FLAG_CANCEL_CURRENT);
-        AlarmManager am  = getAlarmManager();
+        PendingIntent pi       = PendingIntent.getBroadcast(context, 0, intent, PendingIntent.FLAG_CANCEL_CURRENT);
+        AlarmManager am        = getAlarmManager();
+        NotificationManager nc = getNotificationManager();
 
-        try {
-            am.cancel(pi);
-        } catch (Exception e) {}
+        am.cancel(pi);
+        nc.cancel(Integer.parseInt(notificationId));
     }
 
     /**
@@ -86,13 +87,16 @@ public class Helper {
      * by one.
      */
     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();
         Set<String> alarmIds       = alarms.keySet();
 
         for (String alarmId : alarmIds) {
             cancel(alarmId);
         }
+
+        nc.cancelAll();
     }
 
     /**
@@ -100,11 +104,13 @@ public class Helper {
      * This will allow the application to restore the alarm upon device reboot.
      * Also this is used by the cancelAll method.
      *
+     * @param alarmId
+     *            The Id of the notification that must be persisted.
      * @param args
      *            The assumption is that parse has been called already.
      */
     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) {
             editor.putString(alarmId, args.toString());
@@ -119,7 +125,7 @@ public class Helper {
      *            The Id of the notification that must be removed.
      */
     public void unpersist (String alarmId) {
-        Editor editor = context.getSharedPreferences(LocalNotification.PLUGIN_NAME, Context.MODE_PRIVATE).edit();
+        Editor editor = LocalNotification.getSharedPreferences().edit();
 
         if (alarmId != null) {
             editor.remove(alarmId);
@@ -131,7 +137,7 @@ public class Helper {
      * Clear all alarms from the Android shared Preferences.
      */
     public void unpersistAll () {
-        Editor editor = context.getSharedPreferences(LocalNotification.PLUGIN_NAME, Context.MODE_PRIVATE).edit();
+        Editor editor = LocalNotification.getSharedPreferences().edit();
 
         editor.clear();
         editor.commit();
@@ -140,4 +146,8 @@ public class Helper {
     private AlarmManager getAlarmManager () {
         return (AlarmManager) context.getSystemService(Context.ALARM_SERVICE);
     }
+
+    private NotificationManager getNotificationManager () {
+        return (NotificationManager) context.getSystemService(Context.NOTIFICATION_SERVICE);
+    }
 }

+ 21 - 19
src/android/LocalNotification.java

@@ -13,6 +13,7 @@ import org.json.JSONException;
 import org.json.JSONObject;
 
 import android.content.Context;
+import android.content.SharedPreferences;
 
 import org.apache.cordova.CordovaPlugin;
 import org.apache.cordova.CallbackContext;
@@ -26,7 +27,7 @@ import org.apache.cordova.CordovaWebView;
  */
 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 Context context          = null;
@@ -77,9 +78,7 @@ public class LocalNotification extends CordovaPlugin {
      *            The options that can be specified per alarm.
      */
     public static void add (Options options) {
-        Helper helper = new Helper(context);
-
-        helper.add(options);
+        getHelper().add(options);
     }
 
     /**
@@ -90,9 +89,7 @@ public class LocalNotification extends CordovaPlugin {
      *            registered using add()
      */
     public static void cancel (String notificationId) {
-        Helper helper = new Helper(context);
-
-        helper.cancel(notificationId);
+        getHelper().cancel(notificationId);
     }
 
     /**
@@ -104,9 +101,7 @@ public class LocalNotification extends CordovaPlugin {
      * by one.
      */
     public static void cancelAll() {
-        Helper helper = new Helper(context);
-
-        helper.cancelAll();
+        getHelper().cancelAll();
     }
 
     /**
@@ -114,13 +109,13 @@ public class LocalNotification extends CordovaPlugin {
      * This will allow the application to restore the alarm upon device reboot.
      * Also this is used by the cancelAll method.
      *
+     * @param alarmId
+     *            The Id of the notification that must be persisted.
      * @param args
      *            The assumption is that parse has been called already.
      */
     public static void persist (String alarmId, JSONArray args) {
-        Helper helper = new Helper(context);
-
-        helper.persist(alarmId, args);
+        getHelper().persist(alarmId, args);
     }
 
     /**
@@ -130,18 +125,25 @@ public class LocalNotification extends CordovaPlugin {
      *            The Id of the notification that must be removed.
      */
     public static void unpersist (String alarmId) {
-        Helper helper = new Helper(context);
-
-        helper.unpersist(alarmId);
+        getHelper().unpersist(alarmId);
     }
 
     /**
      * Clear all alarms from the Android shared Preferences.
      */
     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 {
      */
     private void rememberCordovaVarsForStaticUse () {
         LocalNotification.webView = super.webView;
-        LocalNotification.context = super.cordova.getActivity();
+        LocalNotification.context = super.cordova.getActivity().getApplicationContext();
     }
 }

+ 3 - 1
src/android/Receiver.java

@@ -47,7 +47,9 @@ public class Receiver extends BroadcastReceiver {
         try {
             args    = new JSONObject(bundle.getString(OPTIONS));
             options = new Options(context).parse(args);
-        } catch (JSONException e) {}
+        } catch (JSONException e) {
+            return;
+        }
 
         this.context = context;
         this.options = options;

+ 4 - 5
src/android/Restore.java

@@ -1,9 +1,10 @@
 /**
+ *  LocalNotificationRestore.java
  *  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.
- *  LGPL v2.1 licensed
+ *  GPL v2 licensed
  */
 
 package de.appplant.cordova.plugin.localnotification;
@@ -27,10 +28,8 @@ public class Restore extends BroadcastReceiver {
 
     @Override
     public void onReceive(Context context, Intent intent) {
-        String pluginName = LocalNotification.PLUGIN_NAME;
-
         // Obtain alarm details form Shared Preferences
-        SharedPreferences alarms = context.getSharedPreferences(pluginName, Context.MODE_PRIVATE);
+        SharedPreferences alarms = LocalNotification.getSharedPreferences();
         Set<String> alarmIds     = alarms.getAll().keySet();
 
         /*