LocalNotificationManager.m 6.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192
  1. //
  2. // LocalNotificationManager.m
  3. // YYXC
  4. //
  5. // Created by 张嵘 on 2020/11/25.
  6. // Copyright © 2020 JCZ. All rights reserved.
  7. //
  8. #import "LocalNotificationManager.h"
  9. static LocalNotificationManager *_instance = nil;
  10. static dispatch_once_t onceToken;
  11. @interface LocalNotificationManager ()
  12. @end
  13. @implementation LocalNotificationManager
  14. + (instancetype)shareManager {
  15. return [[self alloc] init];
  16. }
  17. - (instancetype)init{
  18. dispatch_once(&onceToken, ^{
  19. _instance = [super init];
  20. });
  21. return _instance;
  22. }
  23. /** 添加本地推送通知*/
  24. - (void)addLocalNotificationWithTitle:(NSString *)title subTitle:(NSString *)subTitle body:(NSString *)body timeInterval:(long)timeInterval identifier:(NSString *)identifier userInfo:(NSDictionary *)userInfo repeats:(int)repeats {
  25. if (title.length == 0 || body.length == 0 || identifier.length == 0) {
  26. return;
  27. }
  28. if (@available(iOS 10.0, *)) {
  29. UNUserNotificationCenter *center = [UNUserNotificationCenter currentNotificationCenter];
  30. UNMutableNotificationContent *content = [[UNMutableNotificationContent alloc] init];
  31. // 标题
  32. if (title.length) {
  33. content.title = title;
  34. }
  35. if (subTitle.length) {
  36. content.subtitle = subTitle;
  37. }
  38. // 内容
  39. if (body.length) {
  40. content.body = body;
  41. }
  42. if (userInfo != nil) {
  43. content.userInfo = userInfo;
  44. }
  45. // 声音
  46. // 默认声音
  47. content.sound = [UNNotificationSound defaultSound];
  48. // 添加自定义声音
  49. //content.sound = [UNNotificationSound soundNamed:@"Alert_ActivityGoalAttained_Salient_Haptic.caf"];
  50. // 角标 (我这里测试的角标无效,暂时没找到原因)
  51. content.badge = @1;
  52. // 多少秒后发送,可以将固定的日期转化为时间
  53. NSTimeInterval time = [[NSDate dateWithTimeIntervalSinceNow:timeInterval] timeIntervalSinceNow];
  54. UNNotificationTrigger *trigger = nil;
  55. // repeats,是否重复,如果重复的话时间必须大于60s,要不会报错
  56. if (repeats > 0 && repeats < 7) {
  57. NSDate *date = [NSDate dateWithTimeIntervalSinceNow:timeInterval];
  58. // 定义一个时间字段的旗标,指定将会获取指定年、月、日、时、分、秒的信息
  59. unsigned unitFlags = NSCalendarUnitYear | NSCalendarUnitMonth | NSCalendarUnitDay | NSCalendarUnitHour | NSCalendarUnitWeekday | NSCalendarUnitMinute | NSCalendarUnitSecond;
  60. NSCalendar *gregorian = [[NSCalendar alloc] initWithCalendarIdentifier:NSCalendarIdentifierGregorian];
  61. // 获取不同时间字段的信息
  62. NSDateComponents* comp = [gregorian components:unitFlags fromDate:date];
  63. NSDateComponents *components = [[NSDateComponents alloc] init];
  64. components.second = comp.second;
  65. if (repeats == 6) {
  66. //每分钟循环
  67. } else if (repeats == 5) {
  68. //每小时循环
  69. components.minute = comp.minute;
  70. } else if (repeats == 4) {
  71. //每天循环
  72. components.minute = comp.minute;
  73. components.hour = comp.hour;
  74. } else if (repeats == 3) {
  75. //每周循环
  76. components.minute = comp.minute;
  77. components.hour = comp.hour;
  78. components.weekday = comp.weekday;
  79. } else if (repeats == 2) {
  80. //每月循环
  81. components.minute = comp.minute;
  82. components.hour = comp.hour;
  83. components.day = comp.day;
  84. components.month = comp.month;
  85. } else if (repeats == 1) {
  86. //每年循环
  87. components.minute = comp.minute;
  88. components.hour = comp.hour;
  89. components.day = comp.day;
  90. components.month = comp.month;
  91. components.year = comp.year;
  92. }
  93. trigger = [UNCalendarNotificationTrigger triggerWithDateMatchingComponents:components repeats:YES];
  94. } else {
  95. //不循环
  96. trigger = [UNTimeIntervalNotificationTrigger triggerWithTimeInterval:time repeats:NO];
  97. }
  98. // 添加通知的标识符,可以用于移除,更新等操作 identifier
  99. UNNotificationRequest *request = [UNNotificationRequest requestWithIdentifier:identifier content:content trigger:trigger];
  100. [center addNotificationRequest:request withCompletionHandler:^(NSError *_Nullable error) {
  101. NSLog(@"ECKPushSDK log:添加本地推送成功");
  102. }];
  103. } else {
  104. UILocalNotification *notif = [[UILocalNotification alloc] init];
  105. // 发出推送的日期
  106. notif.fireDate = [NSDate dateWithTimeIntervalSinceNow:timeInterval];
  107. if (title.length > 0) {
  108. notif.alertTitle = title;
  109. }
  110. // 推送的内容
  111. if (body.length > 0) {
  112. notif.alertBody = body;
  113. }
  114. if (userInfo != nil) {
  115. NSMutableDictionary *mdict = [NSMutableDictionary dictionaryWithDictionary:userInfo];
  116. [mdict setObject:identifier forKey:@"identifier"];
  117. notif.userInfo = mdict;
  118. } else {
  119. // 可以添加特定信息
  120. notif.userInfo = @{@"identifier":identifier};
  121. }
  122. // 角标
  123. notif.applicationIconBadgeNumber = 1;
  124. // 提示音
  125. notif.soundName = UILocalNotificationDefaultSoundName;
  126. // 循环提醒
  127. if (repeats == 6) {
  128. //每分钟循环
  129. notif.repeatInterval = NSCalendarUnitMinute;
  130. } else if (repeats == 5) {
  131. //每小时循环
  132. notif.repeatInterval = NSCalendarUnitHour;
  133. } else if (repeats == 4) {
  134. //每天循环
  135. notif.repeatInterval = NSCalendarUnitDay;
  136. } else if (repeats == 3) {
  137. //每周循环
  138. notif.repeatInterval = NSCalendarUnitWeekday;
  139. } else if (repeats == 2) {
  140. //每月循环
  141. notif.repeatInterval = NSCalendarUnitMonth;
  142. } else if (repeats == 1) {
  143. //每年循环
  144. notif.repeatInterval = NSCalendarUnitYear;
  145. } else {
  146. //不循环
  147. }
  148. [[UIApplication sharedApplication] scheduleLocalNotification:notif];
  149. }
  150. }
  151. /** 移除某一个指定的通知*/
  152. - (void)removeNotificationWithIdentifierID:(NSString *)noticeId {
  153. if (@available(iOS 10.0, *)) {
  154. UNUserNotificationCenter *center = [UNUserNotificationCenter currentNotificationCenter];
  155. [center getPendingNotificationRequestsWithCompletionHandler:^(NSArray<UNNotificationRequest *> * _Nonnull requests) {
  156. for (UNNotificationRequest *req in requests){
  157. NSLog(@"ECKPushSDK log: 当前存在的本地通知identifier: %@\n", req.identifier);
  158. }
  159. }];
  160. [center removePendingNotificationRequestsWithIdentifiers:@[noticeId]];
  161. } else {
  162. NSArray *array = [[UIApplication sharedApplication] scheduledLocalNotifications];
  163. for (UILocalNotification *localNotification in array){
  164. NSDictionary *userInfo = localNotification.userInfo;
  165. NSString *obj = [userInfo objectForKey:@"identifier"];
  166. if ([obj isEqualToString:noticeId]) {
  167. [[UIApplication sharedApplication] cancelLocalNotification:localNotification];
  168. }
  169. }
  170. }
  171. }
  172. /** 移除所有通知*/
  173. - (void)removeAllNotification {
  174. if (@available(iOS 10.0, *)) {
  175. UNUserNotificationCenter *center = [UNUserNotificationCenter currentNotificationCenter];
  176. [center removeAllPendingNotificationRequests];
  177. }else {
  178. [[UIApplication sharedApplication] cancelAllLocalNotifications];
  179. }
  180. }
  181. @end