iOS UILocalNotification - 徽章图标和通知声音

时间:2013-03-06 00:33:46

标签: ios notifications uilocalnotification badge

我有一个应该相当简单的快速问题。

首先,我将首先发布代码:

-(void)notification
{
UILocalNotification *localNotification = [[UILocalNotification alloc] init];

if (!localNotification)
    return;

// Current date
NSCalendar *calendar = [NSCalendar currentCalendar]; // gets default calendar
NSDateComponents *components = [calendar components:(NSYearCalendarUnit | NSMonthCalendarUnit |  NSDayCalendarUnit | NSHourCalendarUnit | NSMinuteCalendarUnit) fromDate:[NSDate date]]; // gets the year, month, day, hour and minutesfor today's date
[components setHour:19];
[components setMinute:30];
[components setTimeZone:[NSTimeZone timeZoneWithAbbreviation:@"EST"]];

localNotification.fireDate = [calendar dateFromComponents:components];

localNotification.repeatInterval = NSDayCalendarUnit;

[localNotification setAlertBody:@"Your 'Daily Quote' is ready!"];

[[UIApplication sharedApplication] scheduleLocalNotification:localNotification];

localNotification.applicationIconBadgeNumber = 1;

localNotification.soundName = UILocalNotificationDefaultSoundName;

}

似乎显示了localNotification,但徽章和声音从未显示和播放。可能是因为这是由于“repeatInterval”而循环吗?

如何显示这些内容?通知正在指定时间显示,但徽章图标未更改...

谢谢!

-Henry

1 个答案:

答案 0 :(得分:3)

您应该更改代码顺序:

localNotification.applicationIconBadgeNumber = 1;

localNotification.soundName = UILocalNotificationDefaultSoundName;

以前的代码:

[[UIApplication sharedApplication] scheduleLocalNotification:localNotification];

同样的逻辑在iOS 5.1中适用于我,但是代码顺序。 在通知应用于系统后重置badgeNumber和soundName。 NotificationCenter需要本地通知对象包含的信息,而不是对象本身,因此,在那个时候,NotificationCenter无法获得soundName或badgeNumber.Glad有帮助:-)