计划的本地通知未存储在scheduledLocalNotification数组中 [英] scheduled local notification is not being stored in the scheduledLocalNotification array

查看:90
本文介绍了计划的本地通知未存储在scheduledLocalNotification数组中的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

如果用户当天尚未打开应用,我目前正在安排本地通知每天下午6点出现一次。如果用户已经加载了应用程序,那么我想取消当天的通知,并在明天下午6点安排新的通知。但是,当我尝试迭代计划通知列表(这不是我在应用程序中的唯一本地通知)时,通知会正确显示,[[UIApplication sharedApplication] scheduledLocalNotifications]数组始终为空。以下是给我带来麻烦的代码段:

I am currently scheduling local notifications to appear once per day at 6PM if a user has not already opened the app that day. If the user has already loaded the application, then I want to cancel the notification for that day and schedule a new one for tomorrow at 6PM. The notification displays properly, however, when I try to iterate of the list of scheduled notifications (this is not the only local notification I have in the application), the [[UIApplication sharedApplication] scheduledLocalNotifications] array is always empty. Below is the code snippet that is giving me trouble:

// See if we need to create a local notification to tell the user that they can receive a daily reward
NSArray *notifications = [[UIApplication sharedApplication] scheduledLocalNotifications]; // <- This is always empty
// Iterate over all the pending notifications
for( int iter = 0; iter < [notifications count]; iter++ )
{
    UILocalNotification* localNotification = [notifications objectAtIndex:iter];

    if( [[localNotification.userInfo objectForKey:@"source"] isEqualToString:@"dailyReminder"] )
        [[UIApplication sharedApplication] cancelLocalNotification:localNotification];
}

NSCalendar* calendar = [[[NSCalendar alloc] initWithCalendarIdentifier:NSGregorianCalendar] autorelease];
[calendar setTimeZone:[NSTimeZone localTimeZone]];
NSDateComponents *nowComponents = [calendar components:NSDayCalendarUnit | NSMonthCalendarUnit | NSYearCalendarUnit | NSSecondCalendarUnit | NSMinuteCalendarUnit | NSSecondCalendarUnit fromDate:serverDate];

int hour = [nowComponents hour];
int minute = [nowComponents minute];
int second = [nowComponents second];

int hoursToAdd = 18 - hour;

NSDateComponents *comps = [[[NSDateComponents alloc] init] autorelease];
[comps setDay:1];
[comps setHour:hoursToAdd];
[comps setMinute:-minute];
[comps setSecond:-second];
NSDate *tomorrow6PM = [calendar dateByAddingComponents:comps 
                                                toDate:serverDate
                                               options:0];

NSMutableDictionary* userInfo = [NSMutableDictionary dictionary];
[userInfo setObject:@"dailyReminder" forKey:@"source"];

scheduleNotification(tomorrow6PM, NSLocalizedString(@"Come Back!", @"daily reminder notification message"), NSLocalizedString(@"Launch", @"daily reminder notification button text"), [UIApplication sharedApplication].applicationIconBadgeNumber+1, userInfo);

scheduleNotification功能很简单,只需构建一个本地通知:

the scheduleNotification function is straightforward and just constructs a local notification:

void scheduleNotification(NSDate* fireIn, NSString* bodyText, NSString* alertText, NSUInteger badgeCount, NSMutableDictionary* userInfo)
{
static int appCount = 0;
appCount += 1;

if(!isLocalNotificationEnabled()) 
    return;

Class localNotificationClass = NSClassFromString(@"UILocalNotification");
UILocalNotification* localNotification = [[localNotificationClass alloc] init];
if(!localNotification)
    return;

localNotification.fireDate = fireIn;
localNotification.timeZone = [NSTimeZone systemTimeZone];
localNotification.alertBody = bodyText;
localNotification.alertAction = alertText;
localNotification.soundName = UILocalNotificationDefaultSoundName;
localNotification.applicationIconBadgeNumber = badgeCount;
localNotification.userInfo = userInfo;

[[UIApplication sharedApplication] scheduleLocalNotification:localNotification];
[localNotification release];
}

我不明白为什么本地通知数组总是为空。就像我在正确的时间显示通知之前说的那样,所以他们正在安排。任何帮助表示赞赏。

I don't understand why the array of local notifications is always empty. Like I said before the notifications display at the correct time, so they are getting scheduled. Any help is appreciated.

推荐答案

现在遇到类似的问题。我的猜测是iOS不会立即安排通知,而只是在当前运行循环结束时。在同一个运行循环中多次设置scheduledLocalNotifications属性时,我遇到了这些问题,并且更改似乎没有相应更新。我想我会自己保留本地通知数组的副本,只设置scheduledLocalNotifications,从不读它。

Having similar issues right now. My guess here is that iOS does not schedule the notifications immediately but only at the end of the current run loop. I am running into these problems when setting the scheduledLocalNotifications property several times in the same run loop and changes don't seem to be updated accordingly. I think I will just keep a copy of the local notifications array myself and only set scheduledLocalNotifications and never read it.

这篇关于计划的本地通知未存储在scheduledLocalNotification数组中的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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