Receiver.java 6.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187
  1. /*
  2. Copyright 2013 appPlant UG
  3. Licensed to the Apache Software Foundation (ASF) under one
  4. or more contributor license agreements. See the NOTICE file
  5. distributed with this work for additional information
  6. regarding copyright ownership. The ASF licenses this file
  7. to you under the Apache License, Version 2.0 (the
  8. "License"); you may not use this file except in compliance
  9. with the License. You may obtain a copy of the License at
  10. http://www.apache.org/licenses/LICENSE-2.0
  11. Unless required by applicable law or agreed to in writing,
  12. software distributed under the License is distributed on an
  13. "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
  14. KIND, either express or implied. See the License for the
  15. specific language governing permissions and limitations
  16. under the License.
  17. */
  18. package de.appplant.cordova.plugin.localnotification;
  19. import java.util.Calendar;
  20. import java.util.Random;
  21. import org.json.JSONException;
  22. import org.json.JSONObject;
  23. import android.annotation.SuppressLint;
  24. import android.app.ActivityManager;
  25. import android.app.Notification;
  26. import android.app.Notification.Builder;
  27. import android.app.NotificationManager;
  28. import android.app.PendingIntent;
  29. import android.content.BroadcastReceiver;
  30. import android.content.Context;
  31. import android.content.Intent;
  32. import android.os.Build;
  33. import android.os.Bundle;
  34. /**
  35. * The alarm receiver is triggered when a scheduled alarm is fired. This class
  36. * reads the information in the intent and displays this information in the
  37. * Android notification bar. The notification uses the default notification
  38. * sound and it vibrates the phone.
  39. */
  40. public class Receiver extends BroadcastReceiver {
  41. public static final String OPTIONS = "LOCAL_NOTIFICATION_OPTIONS";
  42. private Context context;
  43. private Options options;
  44. @Override
  45. public void onReceive (Context context, Intent intent) {
  46. Options options = null;
  47. Bundle bundle = intent.getExtras();
  48. JSONObject args;
  49. try {
  50. args = new JSONObject(bundle.getString(OPTIONS));
  51. options = new Options(context).parse(args);
  52. } catch (JSONException e) {
  53. return;
  54. }
  55. this.context = context;
  56. this.options = options;
  57. // The context may got lost if the app was not running before
  58. LocalNotification.setContext(context);
  59. if (options.getInterval() == 0) {
  60. LocalNotification.unpersist(options.getId());
  61. } else if (isFirstAlarmInFuture()) {
  62. return;
  63. }
  64. Builder notification = buildNotification();
  65. if (!isInBackground(context)) {
  66. invokeForegroundCallback();
  67. }
  68. showNotification(notification);
  69. }
  70. /*
  71. * If you set a repeating alarm at 11:00 in the morning and it
  72. * should trigger every morning at 08:00 o'clock, it will
  73. * immediately fire. E.g. Android tries to make up for the
  74. * 'forgotten' reminder for that day. Therefore we ignore the event
  75. * if Android tries to 'catch up'.
  76. */
  77. private Boolean isFirstAlarmInFuture () {
  78. if (options.getInterval() > 0) {
  79. Calendar now = Calendar.getInstance();
  80. Calendar alarm = options.getCalendar();
  81. int alarmHour = alarm.get(Calendar.HOUR_OF_DAY);
  82. int alarmMin = alarm.get(Calendar.MINUTE);
  83. int currentHour = now.get(Calendar.HOUR_OF_DAY);
  84. int currentMin = now.get(Calendar.MINUTE);
  85. if (currentHour != alarmHour && currentMin != alarmMin) {
  86. return true;
  87. }
  88. }
  89. return false;
  90. }
  91. /**
  92. * Erstellt die Notification.
  93. */
  94. private Builder buildNotification () {
  95. Builder notification = new Notification.Builder(context)
  96. .setContentTitle(options.getTitle())
  97. .setContentText(options.getMessage())
  98. .setNumber(options.getBadge())
  99. .setTicker(options.getTitle())
  100. .setSmallIcon(options.getIcon())
  101. .setSound(options.getSound());
  102. setClickEvent(notification);
  103. return notification;
  104. }
  105. /**
  106. * Fügt der Notification einen onclick Handler hinzu.
  107. */
  108. private Builder setClickEvent (Builder notification) {
  109. Intent intent = new Intent(context, ReceiverActivity.class)
  110. .putExtra(OPTIONS, options.getJSONObject().toString())
  111. .setFlags(Intent.FLAG_ACTIVITY_NO_HISTORY);
  112. int requestCode = new Random().nextInt();
  113. PendingIntent contentIntent = PendingIntent.getActivity(context, requestCode, intent, PendingIntent.FLAG_CANCEL_CURRENT);
  114. return notification.setContentIntent(contentIntent);
  115. }
  116. /**
  117. * Zeigt die Notification an.
  118. */
  119. @SuppressWarnings("deprecation")
  120. @SuppressLint("NewApi")
  121. private void showNotification (Builder notification) {
  122. NotificationManager mgr = (NotificationManager) context.getSystemService(Context.NOTIFICATION_SERVICE);
  123. int id = 0;
  124. try {
  125. id = Integer.parseInt(options.getId());
  126. } catch (Exception e) {}
  127. if (Build.VERSION.SDK_INT<16) {
  128. // build notification for HoneyComb to ICS
  129. mgr.notify(id, notification.getNotification());
  130. } else if (Build.VERSION.SDK_INT>15) {
  131. // Notification for Jellybean and above
  132. mgr.notify(id, notification.build());
  133. }
  134. }
  135. /**
  136. * Gibt an, ob die App im Hintergrund läuft.
  137. */
  138. private boolean isInBackground (Context context) {
  139. return !context.getPackageName().equalsIgnoreCase(((ActivityManager)context.getSystemService(Context.ACTIVITY_SERVICE)).getRunningTasks(1).get(0).topActivity.getPackageName());
  140. }
  141. /**
  142. * Ruft die `foreground` Callback Funktion auf.
  143. */
  144. private void invokeForegroundCallback () {
  145. String function = options.getForeground();
  146. // after reboot, LocalNotification.webView is always null
  147. // may be call foreground callback later
  148. if (function != null && LocalNotification.webView != null) {
  149. LocalNotification.webView.sendJavascript(function + "(" + options.getId() + ")");
  150. }
  151. }
  152. }