APPNotificationContent.m 3.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148
  1. /*
  2. * This file contains Original Code and/or Modifications of Original Code
  3. * as defined in and that are subject to the Apache License
  4. * Version 2.0 (the 'License'). You may not use this file except in
  5. * compliance with the License. Please obtain a copy of the License at
  6. * http://opensource.org/licenses/Apache-2.0/ and read it before using this
  7. * file.
  8. *
  9. * The Original Code and all software distributed under the License are
  10. * distributed on an 'AS IS' basis, WITHOUT WARRANTY OF ANY KIND, EITHER
  11. * EXPRESS OR IMPLIED, AND APPLE HEREBY DISCLAIMS ALL SUCH WARRANTIES,
  12. * INCLUDING WITHOUT LIMITATION, ANY WARRANTIES OF MERCHANTABILITY,
  13. * FITNESS FOR A PARTICULAR PURPOSE, QUIET ENJOYMENT OR NON-INFRINGEMENT.
  14. * Please see the License for the specific language governing rights and
  15. * limitations under the License.
  16. */
  17. #import "APPNotificationContent.h"
  18. #import "APPNotificationOptions.h"
  19. #import <objc/runtime.h>
  20. @import UserNotifications;
  21. static char optionsKey;
  22. @implementation APPNotificationContent : UNMutableNotificationContent
  23. #pragma mark -
  24. #pragma mark Init
  25. /**
  26. * Initialize a notification with the given options.
  27. *
  28. * @param [ NSDictionary* ] dict A key-value property map.
  29. *
  30. * @return [ UNMutableNotificationContent ]
  31. */
  32. - (id) initWithOptions:(NSDictionary*)dict
  33. {
  34. self = [self init];
  35. [self setUserInfo:dict];
  36. [self __init];
  37. return self;
  38. }
  39. /**
  40. * Initialize a notification by using the options found under userInfo.
  41. *
  42. * @return [ Void ]
  43. */
  44. - (void) __init
  45. {
  46. APPNotificationOptions* options = self.options;
  47. self.title = options.title;
  48. self.subtitle = options.subtitle;
  49. self.body = options.text;
  50. self.sound = options.sound;
  51. self.badge = options.badge;
  52. self.attachments = options.attachments;
  53. self.categoryIdentifier = options.categoryId;
  54. }
  55. #pragma mark -
  56. #pragma mark Public
  57. /**
  58. * The options used to initialize the notification.
  59. *
  60. * @return [ APPNotificationOptions* ] options
  61. */
  62. - (APPNotificationOptions*) options
  63. {
  64. APPNotificationOptions* options = [self getOptions];
  65. if (!options) {
  66. options = [[APPNotificationOptions alloc]
  67. initWithDict:[self userInfo]];
  68. [self setOptions:options];
  69. }
  70. return options;
  71. }
  72. /**
  73. * The notifcations request ready to add to the notification center including
  74. * all informations about trigger behavior.
  75. *
  76. * @return [ UNNotificationRequest* ]
  77. */
  78. - (UNNotificationRequest*) request
  79. {
  80. APPNotificationOptions* opts = [self getOptions];
  81. return [UNNotificationRequest requestWithIdentifier:opts.identifier
  82. content:self
  83. trigger:opts.trigger];
  84. }
  85. /**
  86. * The category for the notification with all the actions.
  87. *
  88. * @return [ UNNotificationCategory* ]
  89. */
  90. - (UNNotificationCategory*) category
  91. {
  92. NSString* categoryId = self.categoryIdentifier;
  93. NSArray* actions = self.options.actions;
  94. if (!actions.count)
  95. return NULL;
  96. return [UNNotificationCategory categoryWithIdentifier:categoryId
  97. actions:actions
  98. intentIdentifiers:@[]
  99. options:UNNotificationCategoryOptionCustomDismissAction];
  100. }
  101. #pragma mark -
  102. #pragma mark Private
  103. /**
  104. * The options used to initialize the notification.
  105. *
  106. * @return [ APPNotificationOptions* ]
  107. */
  108. - (APPNotificationOptions*) getOptions
  109. {
  110. return objc_getAssociatedObject(self, &optionsKey);
  111. }
  112. /**
  113. * Set the options used to initialize the notification.
  114. *
  115. * @param [ NSDictionary* ] dict A key-value property map.
  116. *
  117. * @return [ Void ]
  118. */
  119. - (void) setOptions:(APPNotificationOptions*)options
  120. {
  121. objc_setAssociatedObject(self, &optionsKey,
  122. options, OBJC_ASSOCIATION_RETAIN_NONATOMIC);
  123. }
  124. @end