فهرست منبع

Each LocalNotificationOptions object needs to be created either with an intent or context to get informed about the package name. The `parse` method must be called to read the options passed through javascript.

Sebastián Katzer 12 سال پیش
والد
کامیت
9ad2292b5f

+ 8 - 5
src/android/LocalNotification.java

@@ -18,6 +18,7 @@ import org.json.JSONObject;
 
 import android.content.Intent;
 import android.content.Context;
+import android.app.Activity;
 import android.app.AlarmManager;
 import android.app.PendingIntent;
 import android.content.SharedPreferences;
@@ -36,10 +37,10 @@ import org.apache.cordova.CordovaWebView;
  */
 public class LocalNotification extends CordovaPlugin {
 
-    public static final String PLUGIN_NAME  = "LocalNotification";
+    public static final String PLUGIN_NAME = "LocalNotification";
 
-    public static CordovaInterface cordova  = null;
-    public static  CordovaWebView webView   = null;
+    public static CordovaInterface cordova = null;
+    public static CordovaWebView webView   = null;
 
     @Override
     public boolean execute (String action, JSONArray args, CallbackContext callbackContext) throws JSONException {
@@ -53,7 +54,8 @@ public class LocalNotification extends CordovaPlugin {
 
         if (action.equalsIgnoreCase("cancel")) {
             JSONObject arguments             = args.getJSONObject(0);
-            LocalNotificationOptions options = new LocalNotificationOptions(arguments);
+            Activity activity                = cordova.getActivity();
+            LocalNotificationOptions options = new LocalNotificationOptions(activity).parse(arguments);
             String alarmId                   = options.getId();
 
             cancel(alarmId);
@@ -77,7 +79,8 @@ public class LocalNotification extends CordovaPlugin {
         cordova.getThreadPool().execute(new Runnable() {
             public void run() {
                 JSONObject arguments             = args.optJSONObject(0);
-                LocalNotificationOptions options = new LocalNotificationOptions(arguments);
+                Activity activity                = cordova.getActivity();
+                LocalNotificationOptions options = new LocalNotificationOptions(activity).parse(arguments);
 
                 persist(options.getId(), args);
                 add(options);

+ 7 - 14
src/android/LocalNotificationOptions.java

@@ -14,6 +14,7 @@ import java.util.Date;
 
 import org.json.JSONObject;
 
+import android.app.Activity;
 import android.app.AlarmManager;
 import android.content.Context;
 import android.media.RingtoneManager;
@@ -33,28 +34,18 @@ public class LocalNotificationOptions {
     private long interval      = 0;
     private long date          = 0;
 
-    /**
-     * Parse options passed from javascript part of this plugin.
-     */
-    LocalNotificationOptions (JSONObject options) {
-        packageName = LocalNotification.cordova.getActivity().getPackageName();
-
-        parse(options);
+    LocalNotificationOptions (Activity activity) {
+        packageName = activity.getPackageName();
     }
 
-    /**
-     * Parse options passed from javascript part of this plugin.
-     */
-    LocalNotificationOptions (JSONObject options, Context context) {
+    LocalNotificationOptions (Context context) {
         packageName = context.getPackageName();
-
-        parse(options);
     }
 
     /**
      * Parst die übergebenen Eigenschaften.
      */
-    private void parse (JSONObject options) {
+    public LocalNotificationOptions parse (JSONObject options) {
         String repeat = options.optString("repeat");
 
         this.options = options;
@@ -70,6 +61,8 @@ public class LocalNotificationOptions {
         } else if (repeat.equalsIgnoreCase("yearly")) {
             interval = AlarmManager.INTERVAL_DAY*365;
         }
+
+        return this;
     }
 
     /**

+ 1 - 1
src/android/LocalNotificationReceiver.java

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

+ 1 - 1
src/android/LocalNotificationRestore.java

@@ -48,7 +48,7 @@ public class LocalNotificationRestore extends BroadcastReceiver {
 
             try {
                 args    = new JSONObject(alarms.getString(alarmId, ""));
-                options = new LocalNotificationOptions(args, context);
+                options = new LocalNotificationOptions(context).parse(args);
 
                 LocalNotification.add(options);
             } catch (JSONException e) {}