Jelajahi Sumber

Improvement of repeating notifications at exact time (#44). `AlarmManager.setRepeating` triggers the notifications at strange dates, like everything others with Android...

Sebastián Katzer 12 tahun lalu
induk
melakukan
7a6e799623
4 mengubah file dengan 20 tambahan dan 9 penghapusan
  1. 1 0
      README.md
  2. 1 5
      src/android/LocalNotification.java
  3. 16 4
      src/android/Options.java
  4. 2 0
      src/android/Receiver.java

+ 1 - 0
README.md

@@ -56,6 +56,7 @@ More informations can be found [here](https://build.phonegap.com/plugins/331).
 - [feature:] Repeat with custom intervals on Android.
 - **[bugfix:]** Callbacks are called with the ID as a number and not as a string.
 - [enhancement:] The background callback on Android is called, even the app is not running when the notification is tapped.
+- [enhancement:] Notifications are repeated more precisely.
 
 #### Version 0.6.3 (12.12.2013)
 - [bugfix:] Black screen on Android.

+ 1 - 5
src/android/LocalNotification.java

@@ -114,11 +114,7 @@ public class LocalNotification extends CordovaPlugin {
         AlarmManager am  = getAlarmManager();
         PendingIntent pi = PendingIntent.getBroadcast(context, 0, intent, PendingIntent.FLAG_CANCEL_CURRENT);
 
-        if (options.getInterval() > 0) {
-            am.setRepeating(AlarmManager.RTC_WAKEUP, triggerTime, options.getInterval(), pi);
-        } else {
-            am.set(AlarmManager.RTC_WAKEUP, triggerTime, pi);
-        }
+        am.set(AlarmManager.RTC_WAKEUP, triggerTime, pi);
     }
 
     /**

+ 16 - 4
src/android/Options.java

@@ -24,6 +24,7 @@ package de.appplant.cordova.plugin.localnotification;
 import java.util.Calendar;
 import java.util.Date;
 
+import org.json.JSONException;
 import org.json.JSONObject;
 
 import android.app.Activity;
@@ -68,14 +69,25 @@ public class Options {
         } else if (repeat.equalsIgnoreCase("yearly")) {
             interval = AlarmManager.INTERVAL_DAY*365;
         } else {
-        	try {
-        		interval = Integer.parseInt(repeat) * 60000;
-        	} catch (Exception e) {};
+            try {
+                interval = Integer.parseInt(repeat) * 60000;
+            } catch (Exception e) {};
         }
 
         return this;
     }
 
+    /**
+     * Setzt die neue Zeit an Hand des Intervalls.
+     */
+    public Options moveDate () {
+        try {
+            options.put("date", (getDate() + interval) / 1000);
+        } catch (JSONException e) {}
+
+        return this;
+    }
+
     /**
      * Gibt die Eigenschaften als JSONObjekt an.
      */
@@ -84,7 +96,7 @@ public class Options {
     }
 
     /**
-     * Gibt die Zeit in Sekunden an, wann die Notification aufpoppen soll.
+     * Gibt die Zeit in Millisekunden an, wann die Notification aufpoppen soll.
      */
     public long getDate() {
         return options.optLong("date", 0) * 1000;

+ 2 - 0
src/android/Receiver.java

@@ -76,6 +76,8 @@ public class Receiver extends BroadcastReceiver {
             LocalNotification.unpersist(options.getId());
         } else if (isFirstAlarmInFuture()) {
             return;
+        } else {
+            LocalNotification.add(options.moveDate());
         }
 
         Builder notification = buildNotification();