gpt4 book ai didi

ios - 多个 UNUserNotifications 未触发

转载 作者:塔克拉玛干 更新时间:2023-11-02 22:56:28 24 4
gpt4 key购买 nike

我正在如下设置多个 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/40277590/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com