OGeek|极客世界-中国程序员成长平台

标题: ios - 多个 UNUserNotifications 未触发 [打印本页]

作者: 菜鸟教程小白    时间: 2022-12-11 17:57
标题: ios - 多个 UNUserNotifications 未触发

我正在设置多个 UNUsernotifications,如下所示,

- (void)viewDidLoad {
    [super viewDidLoad];
    notifCount = 0;
    UNUserNotificationCenter *center = [UNUserNotificationCenter currentNotificationCenter];
    [center requestAuthorizationWithOptionsUNAuthorizationOptionBadge | 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"rayer"
                                                                          contentbjNotificationContent 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
    }
}

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

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



Best Answer-推荐答案


为每个通知提供不同的“requestWithIdentifier”和“时间延迟”并尝试,可能对您有用。

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






欢迎光临 OGeek|极客世界-中国程序员成长平台 (http://ogeek.cn/) Powered by Discuz! X3.4