UILocalNotification+APPLocalNotification.m 4.8 KB

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