看起来
didFinishLaunchingWithOptions:(NSDictionary *)launchOptions
和
didReceiveLocalNotification:(UILocalNotification *)notification
只有当用户确认UILocalNotification时才会触发,例如,通过滑动滑块或触摸iOS的通知下拉菜单中的条目。
如果用户忽略UILocalNotification并通过简单地单击应用程序图标重新进入应用程序,有什么方法可以判断UILocalNotification已经熄灭吗?
我应该指出的是,这实际上只适用于重复通知,因为可以通过观察总计数来检测非重复通知的触发。也就是说,当他们开火时,[[UIApplication sharedApplication] scheduledLocalNotifications]中的消失。
我在找像这样的东西..。
[[UIApplication sharedApplication] unacknowledgedLocalNotifications]
可惜,我找不到任何类似的东西。
发布于 2013-05-10 04:52:18
好吧,你可以在[[UIApplication sharedApplication] scheduledLocalNotifications]中查看你的预定通知。若要查看计划的重复通知是否已触发,请访问fireDate属性以查看为该通知设置的初始日期。然后检查repeatInterval属性。
这里有两个变量,一个是初始间隔,假设是2013-05-08 12:00,第二个是重复间隔,假设是NSDate。通过执行[NSDate date],您将获得当前日期,我所在的位置(在瑞典)现在是2013-05-09 22:45。因此,这意味着有一个通知用户没有采取行动。
因此,您将需要创建一个方法,该方法将接受这些参数,然后从初始日期开始迭代,以查看在当前日期时间之前错过了多少通知。
您会发现NSCalendar的dateByAddingComponents:toDate:options很有用。
发布于 2013-06-25 07:07:43
自那以后,每个人都可能已经离开了,但我想分享我对这个问题的解决方案。(很抱歉有这么长的变量名...)
想法很简单:始终将fireDate保留在未来。
-every time didFinishLaunchingWithOptions或didReceiveLocalNotification被调用,只需取消当前的通知,并在将来使用fireDate one interval单位重新安排一个新的通知
-When您的应用程序启动迭代所有计划的通知,如果fireDate不在未来,您知道它已被忽略
在我的例子中,通知有每周的重复间隔。我首先在didFinishLaunchingWithOptions中重新安排所有已确认的通知:
- (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions
{
UILocalNotification* localNotif = [launchOptions objectForKey:UIApplicationLaunchOptionsLocalNotificationKey];
if (localNotif != nil)
{
[NotificationsHelper rescheduleNotification:localNotif];
}
}也是在didReceiveLocalNotification中:
- (void)application:(UIApplication *)application didReceiveLocalNotification:(UILocalNotification *) notification
{
[NotificationsHelper rescheduleNotification:notification];
} 在应用程序发布时,我会检查所有通知,查看是否有过去使用fireDate的通知:
- (void)applicationDidBecomeActive:(UIApplication *)application
{
[self checkLocalNotifications:application];
}我的"checkLocalNotifications“函数代码:
- (void) checkLocalNotifications:(UIApplication *) application
{
UIApplication* app = [UIApplication sharedApplication];
NSArray* eventArray = [app scheduledLocalNotifications];
for (int i = 0; i < [eventArray count]; i++)
{
UILocalNotification* notification = [eventArray objectAtIndex:i];
if ([NotificationsHelper wasWeeklyRepeatingNotificationIgnored:notification])
{
[NotificationsHelper rescheduleNotification:notification];
NSLog(@"NotificationWasIgnored: %@ %@",notification.alertAction, notification.alertBody );
}
}
}我的"wasWeeklyRepeatingNotificationIgnored“函数代码:
+ (BOOL) wasWeeklyRepeatingNotificationIgnored:(UILocalNotification*) the_notification
{
BOOL result;
NSDate* now = [NSDate date];
// FireDate is earlier than now
if ([the_notification.fireDate compare:now] == NSOrderedAscending)
{
result = TRUE;
}
else
{
result = FALSE;
}
return result;
}我的"rescheduleNotification“函数代码:
+ (void) rescheduleNotification:(UILocalNotification*) the_notification
{
UILocalNotification* new_notification = [[UILocalNotification alloc] init];
NSMutableDictionary* userinfo = [[NSMutableDictionary alloc] init];
[new_notification setUserInfo:userinfo];
[new_notification setRepeatInterval:the_notification.repeatInterval];
[new_notification setSoundName:UILocalNotificationDefaultSoundName];
[new_notification setTimeZone:[NSTimeZone defaultTimeZone]];
[new_notification setAlertAction:the_notification.alertAction];
[new_notification setAlertBody:the_notification.alertBody];
[new_notification setRepeatCalendar:[NSCalendar currentCalendar]];
[new_notification setApplicationIconBadgeNumber:the_notification.applicationIconBadgeNumber];
NSCalendar* gregorian = [[NSCalendar alloc] initWithCalendarIdentifier:NSGregorianCalendar];
NSDateComponents* weekdayComponents = [gregorian components:NSWeekdayCalendarUnit
fromDate:the_notification.fireDate];
NSInteger weekday = [weekdayComponents weekday];
NSDate* next_week = [self addDay:weekday toHourMinute:the_notification.fireDate];
[new_notification setFireDate:next_week];
[[UIApplication sharedApplication] scheduleLocalNotification:new_notification];
[[UIApplication sharedApplication] cancelLocalNotification:the_notification];
}发布于 2013-09-06 03:54:37
如果您的UILocalNotifications增加了应用程序图标徽章编号(即应用程序图标右上角的红色圆圈中的数字),那么有一种非常简单的方法可以检查未确认的UILocalNotifications:只需检查当前的applicationIconBadgeNumber是什么:
- (void)applicationWillEnterForeground:(UIApplication *)application
{
int unacknowledgedNotifs = application.applicationIconBadgeNumber;
NSLog(@"I got %d unacknowledged notifications", unacknowledgedNotifs);
//do something about it...
//You might want to reset the count afterwards:
[[UIApplication sharedApplication] setApplicationIconBadgeNumber:0];
}https://stackoverflow.com/questions/16469721
复制相似问题