APPLocalNotification.m 9.6 KB

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