Receiver.java 6.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190
  1. /*
  2. Copyright 2013-2014 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.support.v4.app.NotificationCompat;
  25. import android.support.v4.app.NotificationCompat.*;
  26. import android.app.NotificationManager;
  27. import android.app.PendingIntent;
  28. import android.content.BroadcastReceiver;
  29. import android.content.Context;
  30. import android.content.Intent;
  31. import android.graphics.Bitmap;
  32. import android.graphics.BitmapFactory;
  33. import android.net.Uri;
  34. import android.os.Build;
  35. import android.os.Bundle;
  36. /**
  37. * The alarm receiver is triggered when a scheduled alarm is fired. This class
  38. * reads the information in the intent and displays this information in the
  39. * Android notification bar. The notification uses the default notification
  40. * sound and it vibrates the phone.
  41. */
  42. public class Receiver extends BroadcastReceiver {
  43. public static final String OPTIONS = "LOCAL_NOTIFICATION_OPTIONS";
  44. private Context context;
  45. private Options options;
  46. @Override
  47. public void onReceive (Context context, Intent intent) {
  48. Options options = null;
  49. Bundle bundle = intent.getExtras();
  50. JSONObject args;
  51. try {
  52. args = new JSONObject(bundle.getString(OPTIONS));
  53. options = new Options(context).parse(args);
  54. } catch (JSONException e) {
  55. return;
  56. }
  57. this.context = context;
  58. this.options = options;
  59. // The context may got lost if the app was not running before
  60. LocalNotification.setContext(context);
  61. fireTriggerEvent();
  62. if (options.getInterval() == 0) {
  63. LocalNotification.unpersist(options.getId());
  64. } else if (isFirstAlarmInFuture()) {
  65. return;
  66. } else {
  67. LocalNotification.add(options.moveDate(), false);
  68. }
  69. Builder notification = buildNotification();
  70. showNotification(notification);
  71. }
  72. /*
  73. * If you set a repeating alarm at 11:00 in the morning and it
  74. * should trigger every morning at 08:00 o'clock, it will
  75. * immediately fire. E.g. Android tries to make up for the
  76. * 'forgotten' reminder for that day. Therefore we ignore the event
  77. * if Android tries to 'catch up'.
  78. */
  79. private Boolean isFirstAlarmInFuture () {
  80. if (options.getInterval() > 0) {
  81. Calendar now = Calendar.getInstance();
  82. Calendar alarm = options.getCalendar();
  83. int alarmHour = alarm.get(Calendar.HOUR_OF_DAY);
  84. int alarmMin = alarm.get(Calendar.MINUTE);
  85. int currentHour = now.get(Calendar.HOUR_OF_DAY);
  86. int currentMin = now.get(Calendar.MINUTE);
  87. if (currentHour != alarmHour && currentMin != alarmMin) {
  88. return true;
  89. }
  90. }
  91. return false;
  92. }
  93. /**
  94. * Creates the notification.
  95. */
  96. @SuppressLint("NewApi")
  97. private Builder buildNotification () {
  98. Bitmap icon = BitmapFactory.decodeResource(context.getResources(), options.getIcon());
  99. Uri sound = options.getSound();
  100. Builder notification = new NotificationCompat.Builder(context)
  101. .setContentTitle(options.getTitle())
  102. .setContentText(options.getMessage())
  103. .setNumber(options.getBadge())
  104. .setTicker(options.getMessage())
  105. .setSmallIcon(options.getSmallIcon())
  106. .setLargeIcon(icon)
  107. .setAutoCancel(options.getAutoCancel())
  108. .setOngoing(options.getOngoing());
  109. if (sound != null) {
  110. notification.setSound(sound);
  111. }
  112. if (Build.VERSION.SDK_INT > 16) {
  113. notification.setStyle(new NotificationCompat.BigTextStyle()
  114. .bigText(options.getMessage()));
  115. }
  116. setClickEvent(notification);
  117. return notification;
  118. }
  119. /**
  120. * Adds an onclick handler to the notification
  121. */
  122. private Builder setClickEvent (Builder notification) {
  123. Intent intent = new Intent(context, ReceiverActivity.class)
  124. .putExtra(OPTIONS, options.getJSONObject().toString())
  125. .setFlags(Intent.FLAG_ACTIVITY_NO_HISTORY);
  126. int requestCode = new Random().nextInt();
  127. PendingIntent contentIntent = PendingIntent.getActivity(context, requestCode, intent, PendingIntent.FLAG_CANCEL_CURRENT);
  128. return notification.setContentIntent(contentIntent);
  129. }
  130. /**
  131. * Shows the notification
  132. */
  133. @SuppressWarnings("deprecation")
  134. private void showNotification (Builder notification) {
  135. NotificationManager mgr = (NotificationManager) context.getSystemService(Context.NOTIFICATION_SERVICE);
  136. int id = 0;
  137. try {
  138. id = Integer.parseInt(options.getId());
  139. } catch (Exception e) {}
  140. if (Build.VERSION.SDK_INT<16) {
  141. // build notification for HoneyComb to ICS
  142. mgr.notify(id, notification.getNotification());
  143. } else if (Build.VERSION.SDK_INT>15) {
  144. // Notification for Jellybean and above
  145. mgr.notify(id, notification.build());
  146. }
  147. }
  148. /**
  149. * Fires ontrigger event.
  150. */
  151. private void fireTriggerEvent () {
  152. LocalNotification.fireEvent("trigger", options.getId(), options.getJSON());
  153. }
  154. }