APPLocalNotification.m 12 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346
  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 "APPLocalNotification.h"
  19. @interface APPLocalNotification (Private)
  20. // Archiviert die Meldungen, sodass sie später abgerufen werden kann
  21. - (void) archiveNotification:(UILocalNotification*)notification;
  22. // Entfernt den zur ID passenden Eintrag
  23. - (void) cancelNotificationWithId:(NSString*)id fireEvent:(BOOL)fireEvent;
  24. // Nachschlagewerk für Zeitintervallangaben
  25. - (NSMutableDictionary*) repeatDict;
  26. // Alle zusätzlichen Metadaten der Notification als Hash
  27. - (NSDictionary*) userDict:(NSMutableDictionary*)options;
  28. // Erstellt die Notification und setzt deren Eigenschaften
  29. - (UILocalNotification*) notificationWithProperties:(NSMutableDictionary*)options;
  30. // Ruft die JS-Callbacks auf, nachdem eine Notification eingegangen ist
  31. - (void) didReceiveLocalNotification:(NSNotification*)localNotification;
  32. // Hilfsmethode gibt an, ob er String NULL oder Empty ist
  33. - (BOOL) strIsNullOrEmpty:(NSString*)str;
  34. // Fires the given event
  35. - (void) fireEvent:(NSString*) event id:(NSString*) id json:(NSString*) json;
  36. @end
  37. // Schlüssel-Präfix für alle archivierten Meldungen
  38. NSString *const kAPP_LOCALNOTIFICATION = @"APP_LOCALNOTIFICATION";
  39. @implementation APPLocalNotification
  40. /**
  41. * Fügt eine neue Notification-Eintrag hinzu.
  42. *
  43. * @param {NSMutableDictionary} options Die Eigenschaften der Notification
  44. */
  45. - (void) add:(CDVInvokedUrlCommand*)command
  46. {
  47. [self.commandDelegate runInBackground:^{
  48. NSArray* arguments = [command arguments];
  49. NSMutableDictionary* options = [arguments objectAtIndex:0];
  50. UILocalNotification* notification = [self notificationWithProperties:options];
  51. NSString* id = [notification.userInfo objectForKey:@"id"];
  52. NSString* json = [notification.userInfo objectForKey:@"json"];
  53. [self cancelNotificationWithId:id fireEvent:NO];
  54. [self archiveNotification:notification];
  55. [self fireEvent:@"add" id:id json:json];
  56. [[UIApplication sharedApplication] scheduleLocalNotification:notification];
  57. }];
  58. }
  59. /**
  60. * Entfernt die zur ID passende Meldung.
  61. *
  62. * @param {NSString} id Die ID der Notification
  63. */
  64. - (void) cancel:(CDVInvokedUrlCommand*)command
  65. {
  66. [self.commandDelegate runInBackground:^{
  67. NSArray* arguments = [command arguments];
  68. NSString* id = [arguments objectAtIndex:0];
  69. [self cancelNotificationWithId:id fireEvent:YES];
  70. }];
  71. }
  72. /**
  73. * Entfernt alle registrierten Einträge.
  74. */
  75. - (void) cancelAll:(CDVInvokedUrlCommand*)command
  76. {
  77. [self.commandDelegate runInBackground:^{
  78. NSDictionary* entries = [[NSUserDefaults standardUserDefaults] dictionaryRepresentation];
  79. for (NSString* key in [entries allKeys])
  80. {
  81. if ([key hasPrefix:kAPP_LOCALNOTIFICATION])
  82. {
  83. [self cancelNotificationWithId:key fireEvent:YES];
  84. }
  85. }
  86. [[NSUserDefaults standardUserDefaults] synchronize];
  87. [[UIApplication sharedApplication] cancelAllLocalNotifications];
  88. [[UIApplication sharedApplication] setApplicationIconBadgeNumber:0];
  89. }];
  90. }
  91. /**
  92. * Entfernt den zur ID passenden Eintrag.
  93. *
  94. * @param {NSString} id Die ID der Notification
  95. */
  96. - (void) cancelNotificationWithId:(NSString*)id fireEvent:(BOOL)fireEvent
  97. {
  98. if (![self strIsNullOrEmpty:id])
  99. {
  100. NSString* key = ([id hasPrefix:kAPP_LOCALNOTIFICATION])
  101. ? id
  102. : [kAPP_LOCALNOTIFICATION stringByAppendingString:id];
  103. NSData* data = [[NSUserDefaults standardUserDefaults] objectForKey:key];
  104. if (data)
  105. {
  106. UILocalNotification* notification = [NSKeyedUnarchiver unarchiveObjectWithData:data];
  107. [[NSUserDefaults standardUserDefaults] removeObjectForKey:key];
  108. [[UIApplication sharedApplication] cancelLocalNotification:notification];
  109. if (fireEvent)
  110. {
  111. NSString* json = [notification.userInfo objectForKey:@"json"];
  112. [self fireEvent:@"cancel" id:id json:json];
  113. }
  114. }
  115. }
  116. }
  117. /**
  118. * Entfernt alle Meldungen, die älter als x Sekunden sind.
  119. *
  120. * @param {float} seconds
  121. */
  122. - (void) cancelAllNotificationsWhichAreOlderThen:(float)seconds
  123. {
  124. NSDictionary* entries = [[NSUserDefaults standardUserDefaults] dictionaryRepresentation];
  125. NSDate* now = [NSDate date];
  126. for (NSString* key in [entries allKeys])
  127. {
  128. if ([key hasPrefix:kAPP_LOCALNOTIFICATION])
  129. {
  130. NSData* data = [[NSUserDefaults standardUserDefaults] objectForKey:key];
  131. if (data)
  132. {
  133. UILocalNotification* notification = [NSKeyedUnarchiver unarchiveObjectWithData:data];
  134. NSTimeInterval fireDateDistance = [now timeIntervalSinceDate:notification.fireDate];
  135. NSString* id = [notification.userInfo objectForKey:@"id"];
  136. if (notification.repeatInterval == NSEraCalendarUnit && fireDateDistance > seconds) {
  137. [self cancelNotificationWithId:id fireEvent:YES];
  138. }
  139. }
  140. }
  141. }
  142. }
  143. /**
  144. * Archiviert die Meldungen, sodass sie später abgerufen werden kann.
  145. *
  146. * @param {UILocalNotification} notification
  147. */
  148. - (void) archiveNotification:(UILocalNotification*)notification
  149. {
  150. NSString* id = [notification.userInfo objectForKey:@"id"];
  151. if (![self strIsNullOrEmpty:id])
  152. {
  153. NSData* data = [NSKeyedArchiver archivedDataWithRootObject:notification];
  154. NSString* key = [kAPP_LOCALNOTIFICATION stringByAppendingString:id];
  155. [[NSUserDefaults standardUserDefaults] setObject:data forKey:key];
  156. }
  157. }
  158. /**
  159. * Nachschlagewerk für Zeitintervallangaben.
  160. */
  161. - (NSMutableDictionary*) repeatDict
  162. {
  163. NSMutableDictionary* repeatDict = [[NSMutableDictionary alloc] init];
  164. #ifdef NSCalendarUnitHour
  165. [repeatDict setObject:[NSNumber numberWithInt:NSCalendarUnitHour] forKey:@"hourly"];
  166. [repeatDict setObject:[NSNumber numberWithInt:NSCalendarUnitDay] forKey:@"daily"];
  167. [repeatDict setObject:[NSNumber numberWithInt:NSWeekCalendarUnit] forKey:@"weekly"];
  168. [repeatDict setObject:[NSNumber numberWithInt:NSCalendarUnitMonth] forKey:@"monthly"];
  169. [repeatDict setObject:[NSNumber numberWithInt:NSCalendarUnitYear] forKey:@"yearly"];
  170. #else
  171. [repeatDict setObject:[NSNumber numberWithInt:NSHourCalendarUnit] forKey:@"hourly"];
  172. [repeatDict setObject:[NSNumber numberWithInt:NSDayCalendarUnit] forKey:@"daily"];
  173. [repeatDict setObject:[NSNumber numberWithInt:NSWeekCalendarUnit] forKey:@"weekly"];
  174. [repeatDict setObject:[NSNumber numberWithInt:NSMonthCalendarUnit] forKey:@"monthly"];
  175. [repeatDict setObject:[NSNumber numberWithInt:NSYearCalendarUnit] forKey:@"yearly"];
  176. #endif
  177. [repeatDict setObject:[NSNumber numberWithInt:NSEraCalendarUnit] forKey:@""];
  178. return repeatDict;
  179. }
  180. /**
  181. * Alle zusätzlichen Metadaten der Notification als Hash.
  182. */
  183. - (NSDictionary*) userDict:(NSMutableDictionary*)options
  184. {
  185. NSString* id = [options objectForKey:@"id"];
  186. NSString* ac = [options objectForKey:@"autoCancel"];
  187. NSString* js = [options objectForKey:@"json"];
  188. return [NSDictionary dictionaryWithObjectsAndKeys:
  189. id, @"id", ac, @"autoCancel", js, @"json", nil];
  190. }
  191. /**
  192. * Erstellt die Notification und setzt deren Eigenschaften.
  193. */
  194. - (UILocalNotification*) notificationWithProperties:(NSMutableDictionary*)options
  195. {
  196. UILocalNotification* notification = [[UILocalNotification alloc] init];
  197. double timestamp = [[options objectForKey:@"date"] doubleValue];
  198. NSString* msg = [options objectForKey:@"message"];
  199. NSString* title = [options objectForKey:@"title"];
  200. NSString* sound = [options objectForKey:@"sound"];
  201. NSString* repeat = [options objectForKey:@"repeat"];
  202. NSInteger badge = [[options objectForKey:@"badge"] intValue];
  203. notification.fireDate = [NSDate dateWithTimeIntervalSince1970:timestamp];
  204. notification.timeZone = [NSTimeZone defaultTimeZone];
  205. notification.repeatInterval = [[[self repeatDict] objectForKey: repeat] intValue];
  206. notification.userInfo = [self userDict:options];
  207. notification.applicationIconBadgeNumber = badge;
  208. if (![self strIsNullOrEmpty:msg])
  209. {
  210. if (![self strIsNullOrEmpty:title])
  211. {
  212. notification.alertBody = [NSString stringWithFormat:@"%@\n%@", title, msg];
  213. }
  214. else
  215. {
  216. notification.alertBody = msg;
  217. }
  218. }
  219. if (sound != (NSString*)[NSNull null])
  220. {
  221. if ([sound isEqualToString:@""]) {
  222. notification.soundName = UILocalNotificationDefaultSoundName;
  223. }
  224. else
  225. {
  226. notification.soundName = [self.commandDelegate pathForResource:sound];
  227. }
  228. }
  229. return notification;
  230. }
  231. /**
  232. * Ruft die JS-Callbacks auf, nachdem eine Notification eingegangen ist.
  233. */
  234. - (void) didReceiveLocalNotification:(NSNotification*)localNotification
  235. {
  236. UIApplicationState state = [[UIApplication sharedApplication] applicationState];
  237. bool isActive = state == UIApplicationStateActive;
  238. UILocalNotification* notification = [localNotification object];
  239. NSString* id = [notification.userInfo objectForKey:@"id"];
  240. NSString* json = [notification.userInfo objectForKey:@"json"];
  241. BOOL autoCancel = [[notification.userInfo objectForKey:@"autoCancel"] boolValue];
  242. NSDate* now = [NSDate date];
  243. NSTimeInterval fireDateDistance = [now timeIntervalSinceDate:notification.fireDate];
  244. NSString* event = (fireDateDistance < 0.05) ? @"trigger" : @"click";
  245. [self fireEvent:event id:id json:json];
  246. if (autoCancel && !isActive)
  247. {
  248. [self cancelNotificationWithId:id fireEvent:YES];
  249. }
  250. }
  251. /**
  252. * Registriert den Observer für LocalNotification Events.
  253. */
  254. - (void) pluginInitialize
  255. {
  256. [[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(didReceiveLocalNotification:) name:CDVLocalNotification object:nil];
  257. }
  258. /**
  259. * Löscht alle single-repeat Notifications, die älter als 5 Tage sind.
  260. */
  261. - (void) onAppTerminate
  262. {
  263. [self cancelAllNotificationsWhichAreOlderThen:432000];
  264. }
  265. /**
  266. * Hilfsmethode gibt an, ob er String NULL oder Empty ist.
  267. */
  268. - (BOOL) strIsNullOrEmpty:(NSString*)str
  269. {
  270. return (str == (NSString*)[NSNull null] || [str isEqualToString:@""]) ? YES : NO;
  271. }
  272. /**
  273. * Fires the given event.
  274. *
  275. * @param {String} event The Name of the event
  276. * @param {String} id The ID of the notification
  277. * @param {String} json A custom (JSON) string
  278. */
  279. - (void) fireEvent:(NSString*) event id:(NSString*) id json:(NSString*) json
  280. {
  281. UIApplicationState state = [[UIApplication sharedApplication] applicationState];
  282. bool isActive = state == UIApplicationStateActive;
  283. NSString* stateName = isActive ? @"foreground" : @"background";
  284. NSString* params = [NSString stringWithFormat:@"\"%@\",\"%@\",\\'%@\\'", id, stateName, json];
  285. NSString* js = [NSString stringWithFormat:@"setTimeout('plugin.notification.local.on%@(%@)',0)", event, params];
  286. [self.commandDelegate evalJs:js];
  287. }
  288. @end