APPLocalNotificationOptions.m 4.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236
  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 "APPLocalNotificationOptions.h"
  24. // Default sound ressource path
  25. NSString* const DEFAULT_SOUND = @"res://platform_default";
  26. @interface APPLocalNotificationOptions ()
  27. // The dictionary which contains all notification properties
  28. @property(readwrite, assign) NSDictionary* dict;
  29. @end
  30. @implementation APPLocalNotificationOptions
  31. @synthesize dict;
  32. #pragma mark -
  33. #pragma mark Initialization
  34. /**
  35. * Initialize the object with the given options when calling on JS side:
  36. * notification.local.add(options)
  37. */
  38. - (id) initWithDict:(NSDictionary*)dictionary
  39. {
  40. self = [super init];
  41. self.dict = dictionary;
  42. return self;
  43. }
  44. #pragma mark -
  45. #pragma mark Attributes
  46. /**
  47. * The notification's ID.
  48. */
  49. - (NSString*) id
  50. {
  51. return [dict objectForKey:@"id"];
  52. }
  53. /**
  54. * The notification's title.
  55. */
  56. - (NSString*) title
  57. {
  58. return [dict objectForKey:@"title"];
  59. }
  60. /**
  61. * The notification's message.
  62. */
  63. - (NSString*) text
  64. {
  65. return [dict objectForKey:@"text"];
  66. }
  67. /**
  68. * The notification's badge number.
  69. */
  70. - (NSInteger) badgeNumber
  71. {
  72. return [[dict objectForKey:@"badge"] intValue];
  73. }
  74. #pragma mark -
  75. #pragma mark Complex Attributes
  76. /**
  77. * The notification's alert body.
  78. */
  79. - (NSString*) alertBody
  80. {
  81. NSString* title = [self title];
  82. NSString* msg = [self text];
  83. NSString* alertBody = msg;
  84. if (![self stringIsNullOrEmpty:title])
  85. {
  86. alertBody = [NSString stringWithFormat:@"%@\n%@",
  87. title, msg];
  88. }
  89. return alertBody;
  90. }
  91. /**
  92. * The notification's sound path.
  93. */
  94. - (NSString*) soundName
  95. {
  96. NSString* path = [dict objectForKey:@"sound"];
  97. if ([self stringIsNullOrEmpty:path])
  98. return NULL;
  99. if ([path isEqualToString:DEFAULT_SOUND])
  100. return UILocalNotificationDefaultSoundName;
  101. if ([path hasPrefix:@"file:/"])
  102. return [self soundNameForAsset:path];
  103. if ([path hasPrefix:@"res:"])
  104. return [self soundNameForResource:path];
  105. return NULL;
  106. }
  107. /**
  108. * The notification's fire date.
  109. */
  110. - (NSDate*) fireDate
  111. {
  112. double timestamp = [[dict objectForKey:@"at"]
  113. doubleValue];
  114. return [NSDate dateWithTimeIntervalSince1970:timestamp];
  115. }
  116. /**
  117. * The notification's repeat interval.
  118. */
  119. - (NSCalendarUnit) repeatInterval
  120. {
  121. NSString* interval = [dict objectForKey:@"every"];
  122. if ([self stringIsNullOrEmpty:interval]) {
  123. return NSCalendarUnitEra;
  124. }
  125. else if ([interval isEqualToString:@"second"]) {
  126. return NSCalendarUnitSecond;
  127. }
  128. else if ([interval isEqualToString:@"minute"]) {
  129. return NSCalendarUnitMinute;
  130. }
  131. else if ([interval isEqualToString:@"hour"]) {
  132. return NSCalendarUnitHour;
  133. }
  134. else if ([interval isEqualToString:@"day"]) {
  135. return NSCalendarUnitDay;
  136. }
  137. else if ([interval isEqualToString:@"week"]) {
  138. return NSCalendarUnitWeekOfYear;
  139. }
  140. else if ([interval isEqualToString:@"month"]) {
  141. return NSCalendarUnitMonth;
  142. }
  143. else if ([interval isEqualToString:@"year"]) {
  144. return NSCalendarUnitYear;
  145. }
  146. return NSCalendarUnitEra;
  147. }
  148. #pragma mark -
  149. #pragma mark Methods
  150. /**
  151. * The notification's user info dict.
  152. */
  153. - (NSDictionary*) userInfo
  154. {
  155. return dict;
  156. }
  157. /**
  158. * If it's a repeating notification.
  159. */
  160. - (BOOL) isRepeating
  161. {
  162. NSCalendarUnit interval = self.repeatInterval;
  163. return !(interval == NSCalendarUnitEra || interval == 0);
  164. }
  165. #pragma mark -
  166. #pragma mark Helpers
  167. /**
  168. * Convert relative path to valid sound name attribute.
  169. */
  170. - (NSString*) soundNameForAsset:(NSString*)path
  171. {
  172. return [path stringByReplacingOccurrencesOfString:@"file:/"
  173. withString:@"www"];
  174. }
  175. /**
  176. * Convert resource path to valid sound name attribute.
  177. */
  178. - (NSString*) soundNameForResource:(NSString*)path
  179. {
  180. return [path pathComponents].lastObject;
  181. }
  182. /**
  183. * If the string is empty.
  184. */
  185. - (BOOL) stringIsNullOrEmpty:(NSString*)str
  186. {
  187. if (str == (NSString*)[NSNull null])
  188. return YES;
  189. if ([str isEqualToString:@""])
  190. return YES;
  191. return NO;
  192. }
  193. @end