APPLocalNotification.m 4.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111
  1. /**
  2. * APPLocalNotification.m
  3. * Cordova LocalNotification Plugin
  4. *
  5. * Created by Sebastian Katzer (github.com/katzer) on 10/08/2013.
  6. * Copyright 2013 Sebastian Katzer. All rights reserved.
  7. * GPL v2 licensed
  8. */
  9. #import "APPLocalNotification.h"
  10. @implementation APPLocalNotification
  11. /**
  12. * Fügt eine neue Notification-Eintrag hinzu.
  13. *
  14. * @param {NSMutableDictionary} options
  15. */
  16. - (void) add:(CDVInvokedUrlCommand*)command {
  17. NSArray *arguments = [command arguments];
  18. NSMutableDictionary *options = [arguments objectAtIndex:0];
  19. UILocalNotification *notification = [self prepareNotification:options];
  20. notification.userInfo = [self userDict:options];
  21. [[UIApplication sharedApplication] scheduleLocalNotification:notification];
  22. }
  23. /**
  24. * Entfernt den anhand der ID angegebenen Eintrag.
  25. *
  26. * @param {NSString} id Die ID der Notification
  27. */
  28. - (void) cancel:(CDVInvokedUrlCommand*)command {
  29. NSArray *arguments = [command arguments];
  30. NSString *notificationId = [arguments objectAtIndex:0];
  31. NSArray *notifications = [[UIApplication sharedApplication] scheduledLocalNotifications];
  32. for (UILocalNotification *notification in notifications) {
  33. NSString *notId = [notification.userInfo objectForKey:@"notificationId"];
  34. if ([notificationId isEqualToString:notId]) {
  35. [[UIApplication sharedApplication] cancelLocalNotification:notification];
  36. }
  37. }
  38. }
  39. /**
  40. * Entfernt alle registrierten Einträge.
  41. */
  42. - (void) cancelAll:(CDVInvokedUrlCommand*)command {
  43. [[UIApplication sharedApplication] cancelAllLocalNotifications];
  44. }
  45. /**
  46. * @private
  47. */
  48. - (NSMutableDictionary*) repeatDict {
  49. NSMutableDictionary *repeatDict = [[NSMutableDictionary alloc] init];
  50. [repeatDict setObject:[NSNumber numberWithInt:NSDayCalendarUnit] forKey:@"daily"];
  51. [repeatDict setObject:[NSNumber numberWithInt:NSWeekCalendarUnit] forKey:@"weekly"];
  52. [repeatDict setObject:[NSNumber numberWithInt:NSMonthCalendarUnit] forKey:@"monthly"];
  53. [repeatDict setObject:[NSNumber numberWithInt:NSYearCalendarUnit] forKey:@"yearly"];
  54. [repeatDict setObject:[NSNumber numberWithInt:0] forKey:@""];
  55. return repeatDict;
  56. }
  57. /**
  58. * @private
  59. */
  60. - (NSDictionary*) userDict:(NSMutableDictionary*)options {
  61. NSString *notificationId = [options objectForKey:@"id"];
  62. NSString *bg = [options objectForKey:@"background"];
  63. NSString *fg = [options objectForKey:@"foreground"];
  64. NSDictionary *userDict = [NSDictionary dictionaryWithObjectsAndKeys:notificationId, @"notificationId", bg, @"background", fg, @"foreground", nil];
  65. return userDict;
  66. }
  67. /**
  68. * @private
  69. */
  70. - (UILocalNotification*) prepareNotification:(NSMutableDictionary*)options {
  71. double timestamp = [[options objectForKey:@"date"] doubleValue];
  72. NSString* msg = [options objectForKey:@"message"];
  73. NSString* action = [options objectForKey:@"action"];
  74. NSString* sound = [options objectForKey:@"sound"];
  75. NSString* repeat = [options objectForKey:@"repeat"];
  76. NSInteger badge = [[options objectForKey:@"badge"] intValue];
  77. bool hasAction = ([[options objectForKey:@"hasAction"] intValue] == 1) ? YES : NO;
  78. NSDate* date = [NSDate dateWithTimeIntervalSince1970:timestamp];
  79. UILocalNotification* notification = [[UILocalNotification alloc] init];
  80. notification.fireDate = date;
  81. notification.hasAction = hasAction;
  82. notification.timeZone = [NSTimeZone defaultTimeZone];
  83. notification.repeatInterval = [[[self repeatDict] objectForKey: repeat] intValue];
  84. notification.alertBody = ([msg isEqualToString:@""])?nil:msg;
  85. notification.alertAction = action;
  86. notification.soundName = sound;
  87. notification.applicationIconBadgeNumber = badge;
  88. return notification;
  89. }
  90. @end