我有以下代码ViewController.m
:
-(void) checker {
UILocalNotification *notification = [[UILocalNotification alloc] init];
[notification setAlertBody: @"Im your local notification"];
[notification setFireDate: [NSDate dateWithTimeIntervalSinceNow: 1]];
[notification setTimeZone: [NSTimeZone defaultTimeZone]];
[UIApplication setScheduledLocalNotifications: [NSArray arrayWithObject: notification]];
}
最后一行产生警告:
找不到类方法“+setScheduledLocalNotifications”(返回类型默认为 id)
并且在处理时会出错。如何实例化通知?正如我所说,我是新手,如果您能提供完整的答案,将不胜感激。
编辑:我有一个计时器,它每 60 秒重复一次,并调用一个发出通知的函数。
timer = [NSTimer scheduledTimerWithTimeInterval:60 target:self selector:@selector(checker) userInfo:nil repeats:YES];
/* ... */
-(void)checker {
NSLog(@"Notification routine");
UIApplication* app = [UIApplication sharedApplication];
NSArray *oldNotifications = [app scheduledLocalNotifications];
// Clear out the old notification before scheduling a new one.(if needed)
if ([oldNotifications count] > 0)
[app cancelAllLocalNotifications];
// Create a new notification.
UILocalNotification* alarm = [[[UILocalNotification alloc] init] autorelease];
if (alarm) {
alarm.fireDate = [NSDate dateWithTimeIntervalSinceNow:10];
alarm.timeZone = [NSTimeZone defaultTimeZone];
alarm.repeatInterval = 0;
alarm.alertBody = @"Msg to show";
[app scheduleLocalNotification:alarm];
}
}
我可以看到日志每分钟只触发一次,但oldNotifications
计数没有增加。