繁体   English   中英

计划的本地通知未存储在scheduledLocalNotification数组中

[英]scheduled local notification is not being stored in the scheduledLocalNotification array

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

// 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函数很简单,只是构造一个本地通知:

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

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

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

localNotification.fireDate = fireIn;

在这一行检查天气“fireIn”NSDateNSString对象。

如果它的NSStringNSDateformatter帮助下将其转换为NSDate对象,然后将其分配给localNotification fireDate。

我以前遇到过同样的问题,并通过上述步骤解决了这个问题。

如果计划具有无效的开火日期,它将不会被调度并返回结果空数组,因为从来没有有效的一个计划,尝试打印出如下所示的localNotification,您可能会发现问题。

NSLog(@"Notification--->: %@", localNotification);

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM