Explorar el Código

Re-register the alarms with the AlarmManager since these alarms are lost in case of reboot

Sebastián Katzer hace 12 años
padre
commit
89015b59e0
Se han modificado 3 ficheros con 87 adiciones y 9 borrados
  1. 17 0
      plugin.xml
  2. 13 9
      src/android/LocalNotification.java
  3. 57 0
      src/android/LocalNotificationRestore.java

+ 17 - 0
plugin.xml

@@ -44,7 +44,24 @@
         </config-file>
 
         <config-file target="AndroidManifest.xml" parent="/manifest/application">
+            <!--
+             * The alarm receiver is triggered when a scheduled alarm is fired. This class
+             * reads the information in the intent and displays this information in the
+             * Android notification bar. The notification uses the default notification
+             * sound and it vibrates the phone.
+            -->
             <receiver android:name="de.appplant.cordova.plugin.LocalNotificationReceiver" />
+
+            <!--
+             * This class is triggered upon reboot of the device. It needs to re-register
+             * the alarms with the AlarmManager since these alarms are lost in case of
+             * reboot.
+             -->
+            <receiver android:name="de.appplant.cordova.plugin.LocalNotificationRestore" >
+                <intent-filter>
+                    <action android:name="android.intent.action.BOOT_COMPLETED" />
+                </intent-filter>
+            </receiver>
         </config-file>
 
         <config-file target="AndroidManifest.xml" parent="/manifest">

+ 13 - 9
src/android/LocalNotification.java

@@ -23,6 +23,7 @@ import android.app.PendingIntent;
 import android.content.SharedPreferences;
 import android.content.SharedPreferences.Editor;
 
+import org.apache.cordova.CordovaInterface;
 import org.apache.cordova.CordovaPlugin;
 import org.apache.cordova.CallbackContext;
 
@@ -34,13 +35,17 @@ import org.apache.cordova.CallbackContext;
  */
 public class LocalNotification extends CordovaPlugin {
 
-    public static final String PLUGIN_NAME = "LocalNotification";
+    public static final String PLUGIN_NAME  = "LocalNotification";
+
+    public static CordovaInterface cordova = null;
 
     @Override
     public boolean execute (String action, JSONArray args, CallbackContext callbackContext) throws JSONException {
         JSONObject arguments             = args.getJSONObject(0);
         LocalNotificationOptions options = new LocalNotificationOptions(arguments);
 
+        LocalNotification.cordova = super.cordova;
+
         String alarmId = options.getId();
 
         if (action.equalsIgnoreCase("add")) {
@@ -74,14 +79,13 @@ public class LocalNotification extends CordovaPlugin {
      * @param options
      *            The options that can be specified per alarm.
      */
-    private void add (LocalNotificationOptions options) {
+    public static void add (LocalNotificationOptions options) {
         long triggerTime = options.getDate();
         Intent intent    = new Intent(cordova.getActivity(), LocalNotificationReceiver.class);
 
         intent.setAction("" + options.getId());
         intent.putExtra(LocalNotificationReceiver.OPTIONS, options.getJSONObject().toString());
 
-        /* Get the AlarmManager service */
         AlarmManager am      = getAlarmManager();
         PendingIntent sender = PendingIntent.getBroadcast(cordova.getActivity(), 0, intent, PendingIntent.FLAG_CANCEL_CURRENT);
 
@@ -99,7 +103,7 @@ public class LocalNotification extends CordovaPlugin {
      *            The original ID of the notification that was used when it was
      *            registered using add()
      */
-    private void cancel (String notificationId) {
+    public static void cancel (String notificationId) {
         /*
          * Create an intent that looks similar, to the one that was registered
          * using add. Making sure the notification id in the action is the same.
@@ -126,7 +130,7 @@ public class LocalNotification extends CordovaPlugin {
      * all our alarms to loop through these alarms and unregister them one
      * by one.
      */
-    private void cancelAll() {
+    public static void cancelAll() {
         SharedPreferences settings = cordova.getActivity().getSharedPreferences(PLUGIN_NAME, Context.MODE_PRIVATE);
         Map<String, ?> alarms      = settings.getAll();
         Set<String> alarmIds       = alarms.keySet();
@@ -144,7 +148,7 @@ public class LocalNotification extends CordovaPlugin {
      * @param args
      *            The assumption is that parse has been called already.
      */
-    private void persist (String alarmId, JSONArray args) {
+    public static void persist (String alarmId, JSONArray args) {
         Editor editor = cordova.getActivity().getSharedPreferences(PLUGIN_NAME, Context.MODE_PRIVATE).edit();
 
         if (alarmId != null) {
@@ -159,7 +163,7 @@ public class LocalNotification extends CordovaPlugin {
      * @param alarmId
      *            The Id of the notification that must be removed.
      */
-    private void unpersist (String alarmId) {
+    public static void unpersist (String alarmId) {
         Editor editor = cordova.getActivity().getSharedPreferences(PLUGIN_NAME, Context.MODE_PRIVATE).edit();
 
         if (alarmId != null) {
@@ -171,14 +175,14 @@ public class LocalNotification extends CordovaPlugin {
     /**
      * Clear all alarms from the Android shared Preferences.
      */
-    private void unpersistAll () {
+    public static void unpersistAll () {
         Editor editor = cordova.getActivity().getSharedPreferences(PLUGIN_NAME, Context.MODE_PRIVATE).edit();
 
         editor.clear();
         editor.commit();
     }
 
-    private AlarmManager getAlarmManager () {
+    private static AlarmManager getAlarmManager () {
         return (AlarmManager) cordova.getActivity().getSystemService(Context.ALARM_SERVICE);
     }
 }

+ 57 - 0
src/android/LocalNotificationRestore.java

@@ -0,0 +1,57 @@
+/**
+ *  LocalNotificationRestore.java
+ *  Cordova LocalNotification Plugin
+ *
+ *  Created by Sebastian Katzer (github.com/katzer) on 31/08/2013.
+ *  Copyright 2013 Sebastian Katzer. All rights reserved.
+ *  GPL v2 licensed
+ */
+
+package de.appplant.cordova.plugin;
+
+import java.util.Set;
+
+import org.json.JSONException;
+import org.json.JSONObject;
+
+import android.content.BroadcastReceiver;
+import android.content.Context;
+import android.content.Intent;
+import android.content.SharedPreferences;
+
+/**
+ * This class is triggered upon reboot of the device. It needs to re-register
+ * the alarms with the AlarmManager since these alarms are lost in case of
+ * reboot.
+ */
+public class LocalNotificationRestore extends BroadcastReceiver {
+
+	@Override
+	public void onReceive(Context context, Intent intent) {
+		String pluginName = LocalNotification.PLUGIN_NAME;
+
+		if (LocalNotification.cordova == null) {
+			return;
+		}
+
+		// Obtain alarm details form Shared Preferences
+		SharedPreferences alarms = context.getSharedPreferences(pluginName, Context.MODE_PRIVATE);
+		Set<String> alarmIds     = alarms.getAll().keySet();
+
+		/*
+		 * For each alarm, parse its alarm options and register is again with
+		 * the Alarm Manager
+		 */
+		for (String alarmId : alarmIds) {
+			LocalNotificationOptions options;
+			JSONObject args;
+
+			try {
+				args    = new JSONObject(alarms.getString(alarmId, ""));
+				options = new LocalNotificationOptions(args);
+
+				LocalNotification.add(options);
+			} catch (JSONException e) {}
+		}
+	}
+}