ViewController上的本地通知

时间:2013-02-06 20:42:25

标签: iphone ios xcode notifications local

我在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计数不会增加。

2 个答案:

答案 0 :(得分:2)

[[UIApplication sharedApplication] scheduleLocalNotification:notification];

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

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

答案 1 :(得分: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"];

第一个警报将被取消,

相关问题