gpt4 book ai didi

objective-c - 每天在不同时间触发本地通知

转载 作者:可可西里 更新时间:2023-11-01 06:19:20 25 4
gpt4 key购买 nike

我正在开发一个项目,每天在与预定义数据库不同的时间通知用户,准确地说是一天五次。

我的问题是如何安排所有这些时间,5times*365days = 1825 开火日期!!是否可以全部安排?

任何想法将不胜感激

最佳答案

根据 Apple 文档:

An application can have only a limited number of scheduled notifications; the system keeps the soonest firing 64 notifications (with automatically rescheduled notifications counting as a single notification) and discards the rest

我通过设置通知“队列”解决了这个问题。例如,在我的应用程序中,我有三种不同类型的通知,我们只说类型 A、B 和 C。

我为下个月安排了 A、B 和 C 通知,每次用户打开应用程序时,我都会检查剩余的通知数量。例如,如果不再有 A 通知,应用会安排更多 A 通知等。

我如何实现这一目标?

每次安排通知时,我都会使用 userInfo 属性。我设置了一个字典,其中包含一个名为 type 的键和一个值。

在我的应用委托(delegate)中,我检查了所有待处理的通知并计算每种类型剩余的数量。代码如下所示:

NSArray *scheduledNotifications = [UIApplication scheduledLocalNotifications];

NSUInteger AType, BType, CType;

for (UILocalNotification *notif in scheduledNotifications) {
//Classify notifications by type
NSUInteger notifType = [[notif.userInfo objectForKey:@"type"]integerValue];
if (notifType == 0) {
AType++;
}else if(notifType == 1){
BType++;
}else{
CType++;
}

}

如果任何类型的计数为零,应用程序会安排更多通知。

最后,如果显示通知,例如,每天同一时间您可以使用 repeatInterval 属性但是您不能创建自己的重复间隔,您只能使用 NSCalendarUnit 中定义的重复间隔。

希望对您有所帮助。

关于objective-c - 每天在不同时间触发本地通知,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/10341476/

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