UILocalNotification+APPLocalNotification.m 5.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244
  1. /*
  2. * Copyright (c) 2013-2015 by appPlant UG. All rights reserved.
  3. *
  4. * @APPPLANT_LICENSE_HEADER_START@
  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. * @APPPLANT_LICENSE_HEADER_END@
  22. */
  23. #import "UILocalNotification+APPLocalNotification.h"
  24. #import "APPLocalNotificationOptions.h"
  25. #import <objc/runtime.h>
  26. static char optionsKey;
  27. NSInteger const APPLocalNotificationTypeScheduled = 1;
  28. NSInteger const APPLocalNotificationTypeTriggered = 2;
  29. @implementation UILocalNotification (APPLocalNotification)
  30. #pragma mark -
  31. #pragma mark Init
  32. /**
  33. * Initialize a local notification with the given options when calling on JS side:
  34. * notification.local.add(options)
  35. */
  36. - (id) initWithOptions:(NSDictionary*)dict
  37. {
  38. self = [super init];
  39. [self setUserInfo:dict];
  40. [self __init];
  41. return self;
  42. }
  43. /**
  44. * Applies the given options when calling on JS side:
  45. * notification.local.add(options)
  46. */
  47. - (void) __init
  48. {
  49. APPLocalNotificationOptions* options = self.options;
  50. self.fireDate = options.fireDate;
  51. self.timeZone = [NSTimeZone defaultTimeZone];
  52. self.applicationIconBadgeNumber = options.badgeNumber;
  53. self.repeatInterval = options.repeatInterval;
  54. self.alertBody = options.alertBody;
  55. self.soundName = options.soundName;
  56. if ([self wasInThePast]) {
  57. self.fireDate = [NSDate date];
  58. }
  59. }
  60. #pragma mark -
  61. #pragma mark Methods
  62. /**
  63. * The options provided by the plug-in.
  64. */
  65. - (APPLocalNotificationOptions*) options
  66. {
  67. APPLocalNotificationOptions* options = [self getOptions];
  68. if (!options) {
  69. options = [[APPLocalNotificationOptions alloc]
  70. initWithDict:[self userInfo]];
  71. [self setOptions:options];
  72. }
  73. return options;
  74. }
  75. /**
  76. * Get associated option object
  77. */
  78. - (APPLocalNotificationOptions*) getOptions
  79. {
  80. return objc_getAssociatedObject(self, &optionsKey);
  81. }
  82. /**
  83. * Set associated option object
  84. */
  85. - (void) setOptions:(APPLocalNotificationOptions*)options
  86. {
  87. objc_setAssociatedObject(self, &optionsKey,
  88. options, OBJC_ASSOCIATION_RETAIN_NONATOMIC);
  89. }
  90. /**
  91. * The repeating interval in seconds.
  92. */
  93. - (int) repeatIntervalInSeconds
  94. {
  95. switch (self.repeatInterval) {
  96. case NSCalendarUnitMinute:
  97. return 60;
  98. case NSCalendarUnitHour:
  99. return 60000;
  100. case NSCalendarUnitDay:
  101. case NSCalendarUnitWeekOfYear:
  102. case NSCalendarUnitMonth:
  103. case NSCalendarUnitYear:
  104. return 86400;
  105. default:
  106. return 1;
  107. }
  108. }
  109. /**
  110. * Timeinterval since fire date.
  111. */
  112. - (double) timeIntervalSinceFireDate
  113. {
  114. NSDate* now = [NSDate date];
  115. NSDate* fireDate = self.fireDate;
  116. int timespan = [now timeIntervalSinceDate:fireDate];
  117. return timespan;
  118. }
  119. /**
  120. * Timeinterval since last trigger date.
  121. */
  122. - (double) timeIntervalSinceLastTrigger
  123. {
  124. int timespan = [self timeIntervalSinceFireDate];
  125. if ([self isRepeating]) {
  126. timespan = timespan % [self repeatIntervalInSeconds];
  127. }
  128. return timespan;
  129. }
  130. /**
  131. * Encode the user info dict to JSON.
  132. */
  133. - (NSString*) encodeToJSON
  134. {
  135. NSString* json;
  136. NSData* data;
  137. NSMutableDictionary* obj = [self.userInfo mutableCopy];
  138. [obj removeObjectForKey:@"updatedAt"];
  139. data = [NSJSONSerialization dataWithJSONObject:obj
  140. options:NSJSONWritingPrettyPrinted
  141. error:Nil];
  142. json = [[NSString alloc] initWithData:data
  143. encoding:NSUTF8StringEncoding];
  144. return [json stringByReplacingOccurrencesOfString:@"\n"
  145. withString:@""];
  146. }
  147. #pragma mark -
  148. #pragma mark State
  149. /**
  150. * If the fire date was in the past.
  151. */
  152. - (BOOL) wasInThePast
  153. {
  154. return [self timeIntervalSinceLastTrigger] > 0;
  155. }
  156. // If the notification was already scheduled
  157. - (BOOL) isScheduled
  158. {
  159. return [self isRepeating] || ![self wasInThePast];
  160. }
  161. /**
  162. * If the notification was already triggered.
  163. */
  164. - (BOOL) isTriggered
  165. {
  166. NSDate* now = [NSDate date];
  167. NSDate* fireDate = self.fireDate;
  168. bool isLaterThanFireDate = !([now compare:fireDate] == NSOrderedAscending);
  169. return isLaterThanFireDate;
  170. }
  171. /**
  172. * If the notification was updated.
  173. */
  174. - (BOOL) wasUpdated
  175. {
  176. NSDate* now = [NSDate date];
  177. NSDate* updatedAt = [self.userInfo objectForKey:@"updatedAt"];
  178. if (updatedAt == NULL)
  179. return NO;
  180. int timespan = [now timeIntervalSinceDate:updatedAt];
  181. return timespan < 1;
  182. }
  183. /**
  184. * If it's a repeating notification.
  185. */
  186. - (BOOL) isRepeating
  187. {
  188. return [self.options isRepeating];
  189. }
  190. /**
  191. * Process state type of the local notification.
  192. */
  193. - (APPLocalNotificationType) type
  194. {
  195. return [self isTriggered] ? NotifcationTypeTriggered : NotifcationTypeScheduled;
  196. }
  197. @end