ios - 如何获取特定的通知计数

标签 ios objective-c notifications uilocalnotification

我有三类 UILocalNotification - 每个星期日、星期一和星期六通知。

如何获取特定通知,即周一触发计数。 我有一个通知 UILocalNotification(不是远程或推送通知)。

如何统计两个月内触发的周一通知?

注意:没有推送或远程通知的范围。

最佳答案

您必须手动管理它,我建议这样做的方法是利用 AppDelegate 的 didReceiveLocalNotification 方法。实现它,然后检查 UILocalNotificationObjectfireDate 属性。您可以使用以下代码将其解析为星期几:

int dayOfWeek = [[NSCalendar currentCalendar] component:NSCalendarUnitWeekday fromDate:notification.fireDate];

然后您可以根据您拥有的数量将此值存储在 NSUserDefaults 中。 1 将是星期一,2 将是星期二等...

long dayOfWeek = [[NSCalendar currentCalendar] component:NSCalendarUnitWeekday fromDate:notification.fireDate];
long currentCount = [[NSUserDefaults standardUserDefaults] integerForKey:[NSString stringWithFormat:@"weekdayNotificationCount_%ld", dayOfWeek]];
[[NSUserDefaults standardUserDefaults] setInteger:currentCount + 1 forKey:[NSString stringWithFormat:@"weekdayNotificationCount_%ld", dayOfWeek]];

这样一来,您将始终在 NSUserDefaults 中存储一个运行总计,并且只有在删除应用程序时才会消失。

关于ios - 如何获取特定的通知计数,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/41237586/

相关文章:

java - 即使设置为通知也不会播放声音或显示灯光

java - 在 Java/Swing 中,有没有办法合法地使用 "attempt to mutate in notification"?

ios - 如何设置通知对象为 'UIDevice.currentDevice()'?

ios - 在服务器通知后更新应用程序中的数据 - 即使强制退出?

ios - 向 AlertView 发送参数

objective-c - 有人可以向我解释这里发生了什么吗?

objective-c - 只显示很少的cell时,是否需要实现可重用的UITableViewCells的出队机制?

ios - UIPageViewController 和不同的 View Controller

ios - 找不到类型的初始值设定项

ios - Objective-C : Is there the way to enumerate properties in code?