UILocalNotification 不会触发



我正在写一个闹钟iOS应用程序。这是我第一次使用UILocalNotification。我从日期选择器中获取日期。我对日期进行了格式化,以检查函数是否传递了正确的日期。我检查了UILocalNotification所需的所有属性我都有了,但我的通知仍然不会触发。知道为什么吗?谢谢你的帮助。

#import "BIDAlarmViewController.h"
@interface BIDAlarmViewController () 
@end
@implementation BIDAlarmViewController
@synthesize datePicker; 
- (id)initWithNibName:(NSString *)nibNameOrNil bundle:(NSBundle *)nibBundleOrNil
{
    self = [super initWithNibName:nibNameOrNil bundle:nibBundleOrNil];
    if (self) {
    // Custom initialization
    return self;
}
- (void)viewDidLoad
{
[super viewDidLoad];
// Do any additional setup after loading the view from its nib

}
- (void)didReceiveMemoryWarning{
[super didReceiveMemoryWarning];
// Dispose of any resources that can be recreated.
}
-(IBAction)setReminderUsingDateFromDatePicker: (id)sender{
[self scheduleNotificationForDate: datePicker.date];
NSDateFormatter *dateFormatter = [[NSDateFormatter alloc] init];
[dateFormatter setDateFormat:@"yyyy-MM-dd 'at' HH:mm"];
NSString *formattedDateString = [dateFormatter stringFromDate:datePicker.date];

NSLog(@"Button Pressed.. date: %@", formattedDateString);
UIAlertView  *alert = [[UIAlertView alloc] initWithTitle:@"Alarm activated"
                                                 message:@"Alarm has been set"
                                                delegate:self cancelButtonTitle:@"OK"
                                       otherButtonTitles:nil];
[alert show];
}
-(void) scheduleNotificationForDate: (NSDate*)date {
UILocalNotification *alarm = [[UILocalNotification alloc] init];
if (alarm) {
    alarm.fireDate = date;
    alarm.timeZone = [NSTimeZone defaultTimeZone];
    alarm.repeatInterval = 0;
    alarm.soundName = @"alarmsound.caf";
    alarm.alertBody = @"Test message...";
    [[UIApplication sharedApplication] scheduleLocalNotification:alarm];
}
}
@end

http://developer.apple.com/library/ios/documentation/uikit/reference/UIApplicationDelegate_Protocol/Reference/Reference.html#//apple_ref/occ/intfm/UIApplicationDelegate/application:didReceiveLocalNotification:

确保你已经在你的应用委托中实现了被引用的方法,像这样:

- (void)application:(UIApplication *)application didReceiveLocalNotification:(UILocalNotification *)notification {
  NSLog(@"Notification fired"!);
}

Apple关于实现此方法的说明:

本地通知类似于远程推送通知,但是不同之处在于它们的计划、显示和接收都是完全开启的同样的装置。应用程序可以创建和调度本地通知,然后操作系统在安排日期和时间。如果它在应用程序没有交付时交付在前台活动,它显示一个警报,标记应用程序属性中指定的任何内容,或者播放声音UILocalNotification对象。如果应用程序运行在前景,没有警报,徽章,或声音;相反,方法被调用委托实现它。

委托可以实现这个方法,如果它想要被通知日志含义本地通知。例如,如果应用程序是日历应用程序,它可以枚举其日历事件列表确定哪些已经到期或即将到期很快发生它还可以重置应用程序图标徽章编号,它可以访问本地通知中的任何自定义数据对象的userInfo字典

最新更新