UILocalNotification+APPLocalNotification.m 3.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170
  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 methods
  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. }
  50. #pragma mark -
  51. #pragma mark Methods
  52. /**
  53. * The options provided by the plug-in.
  54. */
  55. - (APPLocalNotificationOptions*) options
  56. {
  57. APPLocalNotificationOptions* options = [self getOptions];
  58. if (!options) {
  59. options = [[APPLocalNotificationOptions alloc]
  60. initWithDict:[self userInfo]];
  61. [self setOptions:options];
  62. }
  63. return options;
  64. }
  65. /**
  66. * Get associated option object
  67. */
  68. - (APPLocalNotificationOptions*) getOptions
  69. {
  70. return objc_getAssociatedObject(self, &optionsKey);
  71. }
  72. /**
  73. * Set associated option object
  74. */
  75. - (void) setOptions:(APPLocalNotificationOptions*)options
  76. {
  77. objc_setAssociatedObject(self, &optionsKey,
  78. options, OBJC_ASSOCIATION_RETAIN_NONATOMIC);
  79. }
  80. /**
  81. * The repeating interval in seconds.
  82. */
  83. - (int) repeatIntervalInSeconds
  84. {
  85. switch (self.repeatInterval) {
  86. case NSCalendarUnitMinute:
  87. return 60;
  88. case NSCalendarUnitHour:
  89. return 60000;
  90. case NSCalendarUnitDay:
  91. case NSCalendarUnitWeekOfYear:
  92. case NSCalendarUnitMonth:
  93. case NSCalendarUnitYear:
  94. return 86400;
  95. default:
  96. return 1;
  97. }
  98. }
  99. /**
  100. * Timeinterval since fire date.
  101. */
  102. - (double) timeIntervalSinceFireDate
  103. {
  104. NSDate* now = [NSDate date];
  105. NSDate* fireDate = self.options.fireDate;
  106. int timespan = [now timeIntervalSinceDate:fireDate];
  107. if ([self isRepeating]) {
  108. timespan = timespan % [self repeatIntervalInSeconds];
  109. }
  110. return timespan;
  111. }
  112. /**
  113. * If the fire date was in the past.
  114. */
  115. - (BOOL) wasInThePast
  116. {
  117. return [self timeIntervalSinceFireDate] < 0;
  118. }
  119. /**
  120. * If the notification was already triggered.
  121. */
  122. - (BOOL) wasTriggered
  123. {
  124. NSDate* now = [NSDate date];
  125. NSDate* fireDate = self.fireDate;
  126. bool isLaterThanOrEqualTo = !([now compare:fireDate] == NSOrderedAscending);
  127. return isLaterThanOrEqualTo;
  128. }
  129. /**
  130. * If it's a repeating notification.
  131. */
  132. - (BOOL) isRepeating
  133. {
  134. return [self.options isRepeating];
  135. }
  136. @end