0

我有以下代码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计数没有增加。

4

2 回答 2

2
[[UIApplication sharedApplication] scheduleLocalNotification:notification];

错误消息中的“+”表示它正在尝试调用类方法而不是实例方法。该[UIApplication sharedApplication]调用将返回应用程序对象的单例实例,允许您将该方法作为实例方法调用。

您还应该阅读UIApplication的文档。你只需要调用scheduleLocalNotification:通知对象来安排它。这将允许您将内存释放给对象,因为 schedule 方法将复制对象。

于 2013-02-06T20:48:02.927 回答
1

我就是这样做的

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:60*60];//afterone hour
    alarm.timeZone = [NSTimeZone defaultTimeZone];
    alarm.repeatInterval = 0;
    alarm.soundName = @"alarmsound.caf";
    alarm.alertBody = @"Msg to show";
    [app scheduleLocalNotification:alarm];
}

更新:

- (void)scheduleAlarmForDate:(NSDate*)theDate andBody:(NSString*)Body
{
    UIApplication* app = [UIApplication sharedApplication];
    NSArray *oldNotifications = [app scheduledLocalNotifications];

    // Clear out the old notification before scheduling a new one.
    if ([oldNotifications count] > 0)
        [app cancelAllLocalNotifications];

    // Create a new notification.
    UILocalNotification* alarm = [[UILocalNotification alloc] init];//Using ARC no autorelease
    if (alarm)
    {
        alarm.fireDate = theDate;
        alarm.timeZone = [NSTimeZone defaultTimeZone];
        alarm.repeatInterval = 0;
        alarm.soundName = @"alarmsound.caf";
        alarm.alertBody = Body;
        [app scheduleLocalNotification:alarm];
    }
}

现在如果我调用这个方法两次,

 NSDate *fireDate1 = [NSDate dateWithTimeIntervalSinceNow:4.0];
[self scheduleAlarmForDate:fireDate1 andBody:@"My alertBody 1"];

NSDate *fireDate2 = [NSDate dateWithTimeIntervalSinceNow:6.0];
[self scheduleAlarmForDate:fireDate2 andBody:@"My alertBody 2"];

第一个警报将被取消,,

于 2013-02-06T20:48:48.607 回答