LocalNotificationRestore.java 1.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657
  1. /**
  2. * LocalNotificationRestore.java
  3. * Cordova LocalNotification Plugin
  4. *
  5. * Created by Sebastian Katzer (github.com/katzer) on 31/08/2013.
  6. * Copyright 2013 Sebastian Katzer. All rights reserved.
  7. * GPL v2 licensed
  8. */
  9. package de.appplant.cordova.plugin.localnotification;
  10. import java.util.Set;
  11. import org.json.JSONException;
  12. import org.json.JSONObject;
  13. import android.content.BroadcastReceiver;
  14. import android.content.Context;
  15. import android.content.Intent;
  16. import android.content.SharedPreferences;
  17. /**
  18. * This class is triggered upon reboot of the device. It needs to re-register
  19. * the alarms with the AlarmManager since these alarms are lost in case of
  20. * reboot.
  21. */
  22. public class LocalNotificationRestore extends BroadcastReceiver {
  23. @Override
  24. public void onReceive(Context context, Intent intent) {
  25. String pluginName = LocalNotification.PLUGIN_NAME;
  26. if (LocalNotification.cordova == null) {
  27. return;
  28. }
  29. // Obtain alarm details form Shared Preferences
  30. SharedPreferences alarms = context.getSharedPreferences(pluginName, Context.MODE_PRIVATE);
  31. Set<String> alarmIds = alarms.getAll().keySet();
  32. /*
  33. * For each alarm, parse its alarm options and register is again with
  34. * the Alarm Manager
  35. */
  36. for (String alarmId : alarmIds) {
  37. LocalNotificationOptions options;
  38. JSONObject args;
  39. try {
  40. args = new JSONObject(alarms.getString(alarmId, ""));
  41. options = new LocalNotificationOptions(args);
  42. LocalNotification.add(options);
  43. } catch (JSONException e) {}
  44. }
  45. }
  46. }