簡體   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