TriggerReceiver.java 4.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119
  1. /*
  2. * Apache 2.0 License
  3. *
  4. * Copyright (c) Sebastian Katzer 2017
  5. *
  6. * This file contains Original Code and/or Modifications of Original Code
  7. * as defined in and that are subject to the Apache License
  8. * Version 2.0 (the 'License'). You may not use this file except in
  9. * compliance with the License. Please obtain a copy of the License at
  10. * http://opensource.org/licenses/Apache-2.0/ and read it before using this
  11. * file.
  12. *
  13. * The Original Code and all software distributed under the License are
  14. * distributed on an 'AS IS' basis, WITHOUT WARRANTY OF ANY KIND, EITHER
  15. * EXPRESS OR IMPLIED, AND APPLE HEREBY DISCLAIMS ALL SUCH WARRANTIES,
  16. * INCLUDING WITHOUT LIMITATION, ANY WARRANTIES OF MERCHANTABILITY,
  17. * FITNESS FOR A PARTICULAR PURPOSE, QUIET ENJOYMENT OR NON-INFRINGEMENT.
  18. * Please see the License for the specific language governing rights and
  19. * limitations under the License.
  20. */
  21. package de.appplant.cordova.plugin.localnotification;
  22. import android.content.Context;
  23. import android.os.Build;
  24. import android.os.Bundle;
  25. import android.os.PowerManager;
  26. import de.appplant.cordova.plugin.notification.Builder;
  27. import de.appplant.cordova.plugin.notification.Manager;
  28. import de.appplant.cordova.plugin.notification.Notification;
  29. import de.appplant.cordova.plugin.notification.Options;
  30. import de.appplant.cordova.plugin.notification.receiver.AbstractTriggerReceiver;
  31. import static android.content.Context.POWER_SERVICE;
  32. import static android.os.Build.VERSION.SDK_INT;
  33. import static android.os.Build.VERSION_CODES.LOLLIPOP;
  34. import static android.os.PowerManager.RELEASE_FLAG_WAIT_FOR_NO_PROXIMITY;
  35. /**
  36. * The alarm receiver is triggered when a scheduled alarm is fired. This class
  37. * reads the information in the intent and displays this information in the
  38. * Android notification bar. The notification uses the default notification
  39. * sound and it vibrates the phone.
  40. */
  41. public class TriggerReceiver extends AbstractTriggerReceiver {
  42. /**
  43. * Called when a local notification was triggered. Does present the local
  44. * notification, re-schedule the alarm if necessary and fire trigger event.
  45. *
  46. * @param notification Wrapper around the local notification.
  47. * @param bundle The bundled extras.
  48. */
  49. @Override
  50. public void onTrigger (Notification notification, Bundle bundle) {
  51. boolean isUpdate = bundle.getBoolean(Notification.EXTRA_UPDATE, false);
  52. Context context = notification.getContext();
  53. Options options = notification.getOptions();
  54. int badge = options.getBadgeNumber();
  55. if (badge > 0) {
  56. Manager.getInstance(context).setBadge(badge);
  57. }
  58. if (options.shallWakeUp()) {
  59. wakeUp(context);
  60. }
  61. notification.show();
  62. if (!isUpdate) {
  63. LocalNotification.fireEvent("trigger", notification);
  64. }
  65. }
  66. /**
  67. * Wakeup the device.
  68. *
  69. * @param context The application context.
  70. */
  71. private void wakeUp (Context context) {
  72. PowerManager pm = (PowerManager) context.getSystemService(POWER_SERVICE);
  73. if (pm == null)
  74. return;
  75. int level = PowerManager.SCREEN_DIM_WAKE_LOCK
  76. | PowerManager.ACQUIRE_CAUSES_WAKEUP;
  77. PowerManager.WakeLock wakeLock = pm.newWakeLock(
  78. level, "LocalNotification");
  79. wakeLock.setReferenceCounted(false);
  80. wakeLock.acquire(1000);
  81. if (SDK_INT >= LOLLIPOP) {
  82. wakeLock.release(RELEASE_FLAG_WAIT_FOR_NO_PROXIMITY);
  83. } else {
  84. wakeLock.release();
  85. }
  86. }
  87. /**
  88. * Build notification specified by options.
  89. *
  90. * @param builder Notification builder.
  91. * @param bundle The bundled extras.
  92. */
  93. @Override
  94. public Notification buildNotification (Builder builder, Bundle bundle) {
  95. return builder
  96. .setClickActivity(ClickReceiver.class)
  97. .setClearReceiver(ClearReceiver.class)
  98. .setExtras(bundle)
  99. .build();
  100. }
  101. }