APPNotificationContent.m 3.4 KB

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