每天在不同时间点火本地通知 [英] Fire local notification every day on different times

查看:82
本文介绍了每天在不同时间点火本地通知的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

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

I'm developing a project that notifies the user every day on a different time from a pre-defined database, five times a day to be precise.

我的问题是如何计划所有这些时间, 5次* 365days = 1825 火灾日期!!是否可以安排所有这些?

My problem is how schedule all these times, 5times*365days = 1825 fire dates !! is it possible to schedule them all?

任何想法都会非常感激

推荐答案

根据Apple文档:


应用程序只能有有限数量的预定通知;系统保持最快的64次通知(自动重新安排的通知计为单个通知)并丢弃其余的

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类型。

I solved that problem by setting a "queue" of notifications. For example, in my app I have three different types of notifications, let's just say type A, B and C.

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

I schedule the A, B and C notifications for the next month, everytime the user opens the app, I checked how many notifications left. If, for example, the are no more A notifications, the app schedules more A notifications and so on.

我是如何实现这一目标的?

How I achieve this?

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

Everytime I schedule a notification, I use the userInfo property. I set a dictionary with a key called type and a value.

在我的app委托中,我检查所有待定的通知并计算每种类型剩余的数量。代码如下所示:

In my app delegate I check all the pending notifications and count how many left for each type. The code looks like this:

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++;
        }

}

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

If the count of any type is zero the app schedules more notifications.

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

Finally, if the notifications are show for example, everyday at the same hour you can use the repeatInterval property BUT you can't create your own repeating intervals, you can only use the repeating intervals defined in NSCalendarUnit.

希望它有所帮助。

这篇关于每天在不同时间点火本地通知的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆