AlaramReceiver.java 3.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687
  1. package de.appplant.cordova.plugin;
  2. import java.util.Calendar;
  3. import android.app.Notification;
  4. import android.app.NotificationManager;
  5. import android.app.PendingIntent;
  6. import android.content.BroadcastReceiver;
  7. import android.content.Context;
  8. import android.content.Intent;
  9. import android.os.Bundle;
  10. import android.util.Log;
  11. /**
  12. * The alarm receiver is triggered when a scheduled alarm is fired. This class
  13. * reads the information in the intent and displays this information in the
  14. * Android notification bar. The notification uses the default notification
  15. * sound and it vibrates the phone.
  16. *
  17. * @author dvtoever
  18. */
  19. public class AlarmReceiver extends BroadcastReceiver {
  20. public static final String TITLE = "ALARM_TITLE";
  21. public static final String SUBTITLE = "ALARM_SUBTITLE";
  22. public static final String TICKER_TEXT = "ALARM_TICKER";
  23. public static final String NOTIFICATION_ID = "NOTIFICATION_ID";
  24. /* Contains time in 24hour format 'HH:mm' e.g. '04:30' or '18:23' */
  25. public static final String HOUR_OF_DAY = "HOUR_OF_DAY";
  26. public static final String MINUTE = "MINUTES";
  27. @Override
  28. public void onReceive(Context context, Intent intent) {
  29. Log.d("AlarmReceiver", "AlarmReceiver invoked!");
  30. final Bundle bundle = intent.getExtras();
  31. final Object systemService = context.getSystemService(Context.NOTIFICATION_SERVICE);
  32. // Retrieve notification details from the intent
  33. final String tickerText = bundle.getString(TICKER_TEXT);
  34. final String notificationTitle = bundle.getString(TITLE);
  35. final String notificationSubText = bundle.getString(SUBTITLE);
  36. int notificationId = 0;
  37. try {
  38. notificationId = Integer.parseInt(bundle.getString(NOTIFICATION_ID));
  39. } catch (Exception e) {
  40. Log.d("AlarmReceiver", "Unable to process alarm with id: " + bundle.getString(NOTIFICATION_ID));
  41. }
  42. Calendar currentCal = Calendar.getInstance();
  43. int alarmHour = bundle.getInt(HOUR_OF_DAY);
  44. int alarmMin = bundle.getInt(MINUTE);
  45. int currentHour = currentCal.get(Calendar.HOUR_OF_DAY);
  46. int currentMin = currentCal.get(Calendar.MINUTE);
  47. if (currentHour != alarmHour && currentMin != alarmMin) {
  48. /*
  49. * If you set a repeating alarm at 11:00 in the morning and it
  50. * should trigger every morning at 08:00 o'clock, it will
  51. * immediately fire. E.g. Android tries to make up for the
  52. * 'forgotten' reminder for that day. Therefore we ignore the event
  53. * if Android tries to 'catch up'.
  54. */
  55. Log.d(LocalNotification.PLUGIN_NAME, "AlarmReceiver, ignoring alarm since it is due");
  56. return;
  57. }
  58. // Construct the notification and notificationManager objects
  59. final NotificationManager notificationMgr = (NotificationManager) systemService;
  60. final Notification notification = new Notification(R.drawable.ic_launcher, tickerText,
  61. System.currentTimeMillis());
  62. final PendingIntent contentIntent = PendingIntent.getActivity(context, 0, new Intent(), 0);
  63. notification.defaults |= Notification.DEFAULT_SOUND;
  64. notification.vibrate = new long[] { 0, 100, 200, 300 };
  65. notification.setLatestEventInfo(context, notificationTitle, notificationSubText, contentIntent);
  66. /*
  67. * If you want all reminders to stay in the notification bar, you should
  68. * generate a random ID. If you want do replace an existing
  69. * notification, make sure the ID below matches the ID that you store in
  70. * the alarm intent.
  71. */
  72. notificationMgr.notify(notificationId, notification);
  73. }
  74. }