Receiver.java 6.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200
  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.graphics.Bitmap;
  33. import android.graphics.BitmapFactory;
  34. import android.os.Build;
  35. import android.os.Bundle;
  36. import android.text.TextUtils;
  37. /**
  38. * The alarm receiver is triggered when a scheduled alarm is fired. This class
  39. * reads the information in the intent and displays this information in the
  40. * Android notification bar. The notification uses the default notification
  41. * sound and it vibrates the phone.
  42. */
  43. public class Receiver extends BroadcastReceiver {
  44. public static final String OPTIONS = "LOCAL_NOTIFICATION_OPTIONS";
  45. private Context context;
  46. private Options options;
  47. @Override
  48. public void onReceive (Context context, Intent intent) {
  49. Options options = null;
  50. Bundle bundle = intent.getExtras();
  51. JSONObject args;
  52. try {
  53. args = new JSONObject(bundle.getString(OPTIONS));
  54. options = new Options(context).parse(args);
  55. } catch (JSONException e) {
  56. return;
  57. }
  58. this.context = context;
  59. this.options = options;
  60. // The context may got lost if the app was not running before
  61. LocalNotification.setContext(context);
  62. if (options.getInterval() == 0) {
  63. LocalNotification.unpersist(options.getId());
  64. } else if (isFirstAlarmInFuture()) {
  65. return;
  66. } else {
  67. LocalNotification.add(options.moveDate());
  68. }
  69. Builder notification = buildNotification();
  70. if (!isInBackground(context)) {
  71. invokeForegroundCallback();
  72. }
  73. showNotification(notification);
  74. }
  75. /*
  76. * If you set a repeating alarm at 11:00 in the morning and it
  77. * should trigger every morning at 08:00 o'clock, it will
  78. * immediately fire. E.g. Android tries to make up for the
  79. * 'forgotten' reminder for that day. Therefore we ignore the event
  80. * if Android tries to 'catch up'.
  81. */
  82. private Boolean isFirstAlarmInFuture () {
  83. if (options.getInterval() > 0) {
  84. Calendar now = Calendar.getInstance();
  85. Calendar alarm = options.getCalendar();
  86. int alarmHour = alarm.get(Calendar.HOUR_OF_DAY);
  87. int alarmMin = alarm.get(Calendar.MINUTE);
  88. int currentHour = now.get(Calendar.HOUR_OF_DAY);
  89. int currentMin = now.get(Calendar.MINUTE);
  90. if (currentHour != alarmHour && currentMin != alarmMin) {
  91. return true;
  92. }
  93. }
  94. return false;
  95. }
  96. /**
  97. * Erstellt die Notification.
  98. */
  99. private Builder buildNotification () {
  100. Bitmap icon = BitmapFactory.decodeResource(context.getResources(), options.getIcon());
  101. Builder notification = new Notification.Builder(context)
  102. .setContentTitle(options.getTitle())
  103. .setContentText(options.getMessage())
  104. .setNumber(options.getBadge())
  105. .setTicker(options.getTitle())
  106. .setSmallIcon(options.getSmallIcon())
  107. .setLargeIcon(icon)
  108. .setSound(options.getSound())
  109. .setAutoCancel(options.getAutoCancel())
  110. .setOngoing(options.getOngoing());
  111. setClickEvent(notification);
  112. return notification;
  113. }
  114. /**
  115. * Fügt der Notification einen onclick Handler hinzu.
  116. */
  117. private Builder setClickEvent (Builder notification) {
  118. Intent intent = new Intent(context, ReceiverActivity.class)
  119. .putExtra(OPTIONS, options.getJSONObject().toString())
  120. .setFlags(Intent.FLAG_ACTIVITY_NO_HISTORY);
  121. int requestCode = new Random().nextInt();
  122. PendingIntent contentIntent = PendingIntent.getActivity(context, requestCode, intent, PendingIntent.FLAG_CANCEL_CURRENT);
  123. return notification.setContentIntent(contentIntent);
  124. }
  125. /**
  126. * Zeigt die Notification an.
  127. */
  128. @SuppressWarnings("deprecation")
  129. @SuppressLint("NewApi")
  130. private void showNotification (Builder notification) {
  131. NotificationManager mgr = (NotificationManager) context.getSystemService(Context.NOTIFICATION_SERVICE);
  132. int id = 0;
  133. try {
  134. id = Integer.parseInt(options.getId());
  135. } catch (Exception e) {}
  136. if (Build.VERSION.SDK_INT<16) {
  137. // build notification for HoneyComb to ICS
  138. mgr.notify(id, notification.getNotification());
  139. } else if (Build.VERSION.SDK_INT>15) {
  140. // Notification for Jellybean and above
  141. mgr.notify(id, notification.build());
  142. }
  143. }
  144. /**
  145. * Gibt an, ob die App im Hintergrund läuft.
  146. */
  147. private boolean isInBackground (Context context) {
  148. return !context.getPackageName().equalsIgnoreCase(((ActivityManager)context.getSystemService(Context.ACTIVITY_SERVICE)).getRunningTasks(1).get(0).topActivity.getPackageName());
  149. }
  150. /**
  151. * Ruft die `foreground` Callback Funktion auf.
  152. */
  153. private void invokeForegroundCallback () {
  154. String function = options.getForeground();
  155. // after reboot, LocalNotification.webView is always null
  156. // may be call foreground callback later
  157. if (!TextUtils.isEmpty(function) && LocalNotification.webView != null) {
  158. String params = "\"" + options.getId() + "\",\\'" + JSONObject.quote(options.getJSON()) + "\\'.replace(/(^\"|\"$)/g, \\'\\')";
  159. String js = "setTimeout('" + function + "(" + params + ")',0)";
  160. LocalNotification.webView.sendJavascript(js);
  161. }
  162. }
  163. }