ios - 多个 UNUserNotifications 未触发

标签 ios objective-c ios10 usernotifications unusernotificationcenter

我正在如下设置多个 UNUsernotifications,

- (void)viewDidLoad {
    [super viewDidLoad];
    notifCount = 0;
    UNUserNotificationCenter *center = [UNUserNotificationCenter currentNotificationCenter];
    [center requestAuthorizationWithOptions:(UNAuthorizationOptionBadge | UNAuthorizationOptionSound | UNAuthorizationOptionAlert)
                          completionHandler:^(BOOL granted, NSError * _Nullable error) {
                              if (!error) {
                                  NSLog(@"request succeeded!");

                                  [self set10Notif];

                              }
                          }];
}

set10Notif 方法中,我设置了多个(8 个用于测试)通知,时间为当前时间 10 秒。

-(void) set10Notif
{
    notifCount = notifCount+1;
    if (SYSTEM_VERSION_GREATER_THAN_OR_EQUAL_TO(@"10.0") && notifCount < 10)
    {
        // create actions
      NSCalendar *calendar = [[NSCalendar alloc] initWithCalendarIdentifier:NSCalendarIdentifierGregorian];

      [calendar setTimeZone:[NSTimeZone localTimeZone]];

       NSDate *fireD = [[NSDate date] dateByAddingTimeInterval:notifCount*10];

       NSString *fireStr = [self returnStringFromDate:fireD withFormat:@"hh/mm/ss dd/MM/yyyy"];

       NSDateComponents *components = [calendar components:NSCalendarUnitYear|NSCalendarUnitMonth|NSCalendarUnitDay|NSCalendarUnitHour|NSCalendarUnitMinute|NSCalendarUnitSecond|NSCalendarUnitTimeZone fromDate:fireD];

      UNMutableNotificationContent *objNotificationContent = [[UNMutableNotificationContent alloc] init];
    objNotificationContent.title = [NSString localizedUserNotificationStringForKey:@"Notif!" arguments:nil];
    objNotificationContent.body = [NSString localizedUserNotificationStringForKey:fireStr
                                                                        arguments:nil];
    objNotificationContent.sound = [UNNotificationSound defaultSound];

      UNCalendarNotificationTrigger *trigger = [UNCalendarNotificationTrigger triggerWithDateMatchingComponents:components repeats:NO];

      UNNotificationRequest *request = [UNNotificationRequest requestWithIdentifier:@"Prayer"
                                                                          content:objNotificationContent trigger:trigger];
      UNUserNotificationCenter *userCenter = [UNUserNotificationCenter currentNotificationCenter];
      [userCenter addNotificationRequest:request withCompletionHandler:^(NSError * _Nullable error) {
        if (!error) {

            NSLog(@"Local Notification succeeded");
        }
        else {
            NSLog(@"Local Notification failed");
        }
        [self set10Notif];
       }];
   #endif
    }
}

已设置所有本地通知。但是设备中只触发了一个通知,这是最后一个。

为什么多个通知不能触发? 我做错了什么?

最佳答案

为每个通知提供不同的“requestWithIdentifier”和“time delay”并尝试,可能对你有用。

关于ios - 多个 UNUserNotifications 未触发,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/40216915/

相关文章:

在 id 的对象上找不到 iOS SpringBoard 属性 badgeNumberOrString

iphone - 检测设备的最佳方法是iPhone 5

iphone - 仅为一个 View Controller 从静态库维护 iOS 纵向方向

ios10 - iMessage 扩展应用程序上的推送通知

ios10 - 如何在 iOS 10 中使用 openURL?

iphone - ios自定义标签不支持字体系列只支持Lucida Grande?

ios - 在拒绝 UIActivityController 的“保存图像”权限后,如何将用户重定向到设置?

javascript - 如何在 Cordova/Phonegap 中的远程 html 文件中包含本地脚本?

IOS 'NSInvalidArgumentException',原因: 'data parameter is nil'

objective-c - 使用 Facebook SDK 4.15.1 在 iOS 10 上使用 Facebook SDK 登录、分享和点赞时获取空白页面