gpt4 book ai didi

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

转载 作者:塔克拉玛干 更新时间:2023-11-02 09:04:45 25 4
gpt4 key购买 nike

我目前安排本地通知在每天下午 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 并且永远不会读取它。

关于iphone - 计划的本地通知未存储在 scheduledLocalNotification 数组中,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/8706301/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com