Notification.java 7.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281
  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.notification;
  22. import android.app.AlarmManager;
  23. import android.app.NotificationChannelGroup;
  24. import android.app.NotificationManager;
  25. import android.app.PendingIntent;
  26. import android.content.Context;
  27. import android.content.Intent;
  28. import android.content.SharedPreferences;
  29. import android.net.Uri;
  30. import android.os.Build;
  31. import android.support.v4.app.NotificationCompat;
  32. import org.json.JSONException;
  33. import org.json.JSONObject;
  34. import java.util.Date;
  35. import de.appplant.cordova.plugin.notification.receiver.TriggerReceiver;
  36. /**
  37. * Wrapper class around OS notification class. Handles basic operations
  38. * like show, delete, cancel for a single local notification instance.
  39. */
  40. public final class Notification {
  41. // Used to differ notifications by their life cycle state
  42. public enum Type {
  43. ALL, SCHEDULED, TRIGGERED
  44. }
  45. // Key for private preferences
  46. static final String PREF_KEY = "LocalNotification";
  47. // Application context passed by constructor
  48. private final Context context;
  49. // Notification options passed by JS
  50. private final Options options;
  51. // Builder with full configuration
  52. private final NotificationCompat.Builder builder;
  53. /**
  54. * Constructor
  55. *
  56. * @param context Application context.
  57. * @param options Parsed notification options.
  58. * @param builder Pre-configured notification builder.
  59. */
  60. Notification (Context context, Options options, NotificationCompat.Builder builder) {
  61. this.context = context;
  62. this.options = options;
  63. this.builder = builder;
  64. }
  65. /**
  66. * Constructor
  67. *
  68. * @param context Application context.
  69. * @param options Parsed notification options.
  70. */
  71. public Notification(Context context, Options options) {
  72. this.context = context;
  73. this.options = options;
  74. this.builder = null;
  75. }
  76. /**
  77. * Get application context.
  78. */
  79. public Context getContext () {
  80. return context;
  81. }
  82. /**
  83. * Get notification options.
  84. */
  85. public Options getOptions () {
  86. return options;
  87. }
  88. /**
  89. * Get notification ID.
  90. */
  91. public int getId () {
  92. return options.getId();
  93. }
  94. /**
  95. * If it's a repeating notification.
  96. */
  97. public boolean isRepeating () {
  98. return getOptions().getTrigger().has("every");
  99. }
  100. // /**
  101. // * If the notification is scheduled.
  102. // */
  103. // public boolean isScheduled () {
  104. // return isRepeating() || !wasInThePast();
  105. // }
  106. // /**
  107. // * If the notification is triggered.
  108. // */
  109. // public boolean isTriggered () {
  110. // return wasInThePast();
  111. // }
  112. // /**
  113. // * If the notification is an update.
  114. // *
  115. // * @param keepFlag
  116. // * Set to false to remove the flag from the option map
  117. // */
  118. // protected boolean isUpdate (boolean keepFlag) {
  119. // boolean updated = options.getDict().optBoolean("updated", false);
  120. // if (!keepFlag) {
  121. // options.getDict().remove("updated");
  122. // }
  123. // return updated;
  124. // }
  125. // /**
  126. // * Notification type can be one of pending or scheduled.
  127. // */
  128. // public Type getType () {
  129. // return isScheduled() ? Type.SCHEDULED : Type.TRIGGERED;
  130. // }
  131. /**
  132. * Clear the local notification without canceling repeating alarms.
  133. */
  134. public void clear () {
  135. // if (!isRepeating() && wasInThePast())
  136. // unpersist();
  137. // if (!isRepeating())
  138. // getNotMgr().cancel(getId());
  139. }
  140. /**
  141. * Cancel the local notification.
  142. *
  143. * Create an intent that looks similar, to the one that was registered
  144. * using schedule. Making sure the notification id in the action is the
  145. * same. Now we can search for such an intent using the 'getService'
  146. * method and cancel it.
  147. */
  148. public void cancel() {
  149. // Intent intent = new Intent(context, receiver)
  150. // .setAction(options.getIdStr());
  151. // PendingIntent pi = PendingIntent.
  152. // getBroadcast(context, 0, intent, 0);
  153. // getAlarmMgr().cancel(pi);
  154. // getNotMgr().cancel(options.getId());
  155. // unpersist();
  156. }
  157. /**
  158. * Present the local notification to user.
  159. */
  160. public void show () {
  161. if (builder == null)
  162. return;
  163. String sound = builder.getExtras().getString(Options.SOUND_EXTRA);
  164. Uri soundUri = Uri.parse(sound);
  165. context.grantUriPermission("com.android.systemui", soundUri,
  166. Intent.FLAG_GRANT_READ_URI_PERMISSION);
  167. getNotMgr().notify(getId(), builder.build());
  168. }
  169. // /**
  170. // * Count of triggers since schedule.
  171. // */
  172. // public int getTriggerCountSinceSchedule() {
  173. // long now = System.currentTimeMillis();
  174. // long triggerTime = options.getTriggerTime();
  175. // if (!wasInThePast())
  176. // return 0;
  177. // if (!isRepeating())
  178. // return 1;
  179. // return (int) ((now - triggerTime) / options.getRepeatInterval());
  180. // }
  181. /**
  182. * Encode options to JSON.
  183. */
  184. public String toString() {
  185. JSONObject dict = options.getDict();
  186. JSONObject json = new JSONObject();
  187. try {
  188. json = new JSONObject(dict.toString());
  189. } catch (JSONException e) {
  190. e.printStackTrace();
  191. }
  192. return json.toString();
  193. }
  194. /**
  195. * Persist the information of this notification to the Android Shared
  196. * Preferences. This will allow the application to restore the notification
  197. * upon device reboot, app restart, retrieve notifications, aso.
  198. */
  199. private void persist () {
  200. SharedPreferences.Editor editor = getPrefs().edit();
  201. editor.putString(options.getIdentifier(), options.toString());
  202. editor.apply();
  203. }
  204. /**
  205. * Remove the notification from the Android shared Preferences.
  206. */
  207. private void unpersist () {
  208. SharedPreferences.Editor editor = getPrefs().edit();
  209. editor.remove(options.getIdentifier());
  210. editor.apply();
  211. }
  212. /**
  213. * Shared private preferences for the application.
  214. */
  215. private SharedPreferences getPrefs () {
  216. return context.getSharedPreferences(PREF_KEY, Context.MODE_PRIVATE);
  217. }
  218. /**
  219. * Notification manager for the application.
  220. */
  221. private NotificationManager getNotMgr () {
  222. return (NotificationManager) context
  223. .getSystemService(Context.NOTIFICATION_SERVICE);
  224. }
  225. /**
  226. * Alarm manager for the application.
  227. */
  228. private AlarmManager getAlarmMgr () {
  229. return (AlarmManager) context.getSystemService(Context.ALARM_SERVICE);
  230. }
  231. }