UILocalNotification 调用延迟

发布于 2024-12-22 11:53:34 字数 9474 浏览 0 评论 0原文

我制作了一个应用程序,它是一个定制的警报应用程序。 UILocalNotification 应该在我在 UIDatePicker 中选择的时间调用,但它没有在正确的时间调用。例如,我选择闹钟时间为下午 2:00,因此通知将在下午 2:00 到下午 2:01 之间调用...但不确定何时...它给了我随机时间的延迟。在我的 UITableView 中,您可以看到显示的描述也是错误的。我知道我来自印度,所以它显示的是 GMT 时间,但是可以更正吗?

这是我的整个代码:- ------------------------------AppDelegate.m 文件 :---------------- --------------

@synthesize window,viewController,timeViewController;
NSString *kRemindMeNotificationDataKey = @"kRemindMeNotificationDataKey";
#pragma mark -
#pragma mark === Application Delegate Methods ===
#pragma mark -
- (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions {    
    int x = [[NSUserDefaults standardUserDefaults] integerForKey:@"Mayank"];
    if( x == 1 )
    {
        timeViewController = [[TimeViewController alloc]initWithNibName:@"TimeViewController" bundle:nil];
        timeViewController.modalTransitionStyle = UIModalTransitionStyleCrossDissolve;
        CGRect statusBarFrame = [UIApplication sharedApplication].statusBarFrame;
        CGRect frame = timeViewController.view.frame;
        frame.origin = CGPointMake(frame.origin.x,frame.origin.y + statusBarFrame.size.height );
        timeViewController.view.frame = frame;
        [self.window addSubview:timeViewController.view];
    }
    else
    {
        [[NSUserDefaults standardUserDefaults]setInteger:1 forKey:@"Mayank"];
        [[NSUserDefaults standardUserDefaults]synchronize]; 
        UIAlertView *alert = [[UIAlertView alloc] initWithTitle:@"Do you want to set the Default Alarm?" message:@"at 4:20 PM" delegate:self cancelButtonTitle:@"Cancel" otherButtonTitles:@"Ok",nil];
        [alert show];
        [alert release];
    }
    sleep(1);
    [self.window makeKeyAndVisible];
    application.applicationIconBadgeNumber = 0;
    // Handle launching from a notification
    UILocalNotification *localNotification =
    [launchOptions objectForKey:UIApplicationLaunchOptionsLocalNotificationKey];
    if (localNotification) {
        NSString *reminderText = [localNotification.userInfo 
                                  objectForKey:kRemindMeNotificationDataKey];
        [viewController showReminder:reminderText];
    }
    return YES;
}
- (void)alertView:(UIAlertView *)alertView clickedButtonAtIndex:(NSInteger)buttonIndex
{
    if(buttonIndex == 0)
    {
        timeViewController = [[TimeViewController alloc]initWithNibName:@"TimeViewController" bundle:nil];
        timeViewController.modalTransitionStyle = UIModalTransitionStyleCrossDissolve;
        CGRect statusBarFrame = [UIApplication sharedApplication].statusBarFrame;
        CGRect frame = timeViewController.view.frame;
        frame.origin = CGPointMake(frame.origin.x,frame.origin.y + statusBarFrame.size.height );
        timeViewController.view.frame = frame;
        [self.window addSubview:timeViewController.view];
    }
    if(buttonIndex == 1)
    {
        viewController = [[SetAlarmViewController alloc]initWithNibName:@"SetAlarmViewController" bundle:nil];
        viewController.modalTransitionStyle = UIModalTransitionStyleCrossDissolve;
        CGRect statusBarFrame = [UIApplication sharedApplication].statusBarFrame;
        CGRect frame = viewController.view.frame;
        frame.origin = CGPointMake(frame.origin.x,frame.origin.y + statusBarFrame.size.height );
        viewController.view.frame = frame;
        [self.window addSubview:viewController.view];       
    }
}
- (void)application:(UIApplication *)application 
didReceiveLocalNotification:(UILocalNotification *)notification {
    NSString *reminderText = [notification.userInfo
                              objectForKey:kRemindMeNotificationDataKey];
    [viewController showReminder:reminderText];
    application.applicationIconBadgeNumber = 0;
}

-----------------------------mainViewController.m 文件:-- ----------------------------

@implementation SetAlarmViewController
@synthesize datePicker,tableview, eventText,titleBar,setAlarmButton,returnKeyType;


// Implement viewDidLoad to do additional setup after loading the view, typically from a nib.
- (void)viewDidLoad {

    [super viewDidLoad];

    appDelegate = (The420DudeAppDelegate *)[[UIApplication sharedApplication] delegate];    
    eventText.returnKeyType = UIReturnKeyDone;

//  datePicker.minimumDate = [NSDate date];
    NSDate *now = [NSDate date];
    [datePicker setDate:now animated:YES];
    eventText.delegate = self;
    index = 0;
}

-(void)viewDidAppear:(BOOL)animated
{
    [super viewDidAppear:YES];
    [self.tableview reloadData];
}

- (IBAction) scheduleAlarm:(id) sender {
    [eventText resignFirstResponder];

// Get the current date
    NSDate *pickerDate = [self.datePicker date];

    UILocalNotification *localNotif = [[UILocalNotification alloc] init];
    if (localNotif == nil)
        return;
    localNotif.fireDate = pickerDate;
//  NSLog(@"%@",localNotif.fireDate);
    localNotif.timeZone = [NSTimeZone defaultTimeZone];
//  NSLog(@"%@",localNotif.timeZone);

    // Notification details
    localNotif.alertBody = [eventText text];

    // Set the action button
    localNotif.alertAction = @"Show me";
    localNotif.repeatInterval = NSDayCalendarUnit;
    localNotif.soundName = @"jet.wav";
    // Specify custom data for the notification
    NSDictionary *userDict = [NSDictionary dictionaryWithObject:eventText.text
                                                         forKey:kRemindMeNotificationDataKey];
    localNotif.userInfo = userDict;

    // Schedule the notification
    [[UIApplication sharedApplication] scheduleLocalNotification:localNotif];
    [localNotif release];

    [self.tableview reloadData];
    eventText.text = @"";

    viewController = [[TimeViewController alloc] initWithNibName:@"TimeViewController" bundle:nil];
    [self presentModalViewController:viewController animated:YES];
}

#pragma mark -
#pragma mark Table view data source

- (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView {
    // Return the number of sections.
    return 1;
}


- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section {
    // Return the number of rows in the section.
    return [[[UIApplication sharedApplication] scheduledLocalNotifications] count];
}

- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath
{
    index = indexPath.row;
    UIAlertView *alertView = [[UIAlertView alloc] initWithTitle:@"Warning!!!" 
                                                        message:@"Are you sure you want to Delete???" delegate:self
                                              cancelButtonTitle:@"Cancel"
                                              otherButtonTitles:@"Ok",nil];
    [alertView show];
    [alertView release];

}
- (void)alertView:(UIAlertView *)alertView clickedButtonAtIndex:(NSInteger)buttonIndex
{
    NSArray *notificationArray = [[UIApplication sharedApplication] scheduledLocalNotifications];
    UILocalNotification *notify = [notificationArray objectAtIndex:index];

    if(buttonIndex == 0)
    {
        // Do Nothing on Tapping Cancel...
    }
    if(buttonIndex ==1)
    {
        if(notify)
            [[UIApplication sharedApplication] cancelLocalNotification:notify];
    }
    [self.tableview reloadData];
}

// Customize the appearance of table view cells.
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {

    static NSString *CellIdentifier = @"Cell";

    UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:CellIdentifier];
    if (cell == nil) {
        cell = [[[UITableViewCell alloc] initWithStyle:UITableViewCellStyleSubtitle reuseIdentifier:CellIdentifier] autorelease];
    }
    // Configure the cell...

    NSArray *notificationArray = [[UIApplication sharedApplication] scheduledLocalNotifications];
    UILocalNotification *notif = [notificationArray objectAtIndex:indexPath.row];

    [cell.textLabel setText:notif.alertBody];
    [cell.detailTextLabel setText:[notif.fireDate description]];    
    return cell;
}


- (void)showReminder:(NSString *)text {
    /*
     UIAlertView *alertView = [[UIAlertView alloc] initWithTitle:@"Reminder" 
     message:@"hello" delegate:self
     cancelButtonTitle:@"OK"
     otherButtonTitles:nil];
     [alertView show];
     [self.tableview reloadData];
     [alertView release];
     */

    NSString *path = [[NSBundle mainBundle]pathForResource:@"jet" ofType:@"wav"];
    NSURL *url = [NSURL fileURLWithPath:path];

    player = [[AVAudioPlayer alloc]initWithContentsOfURL:url error:nil];
    player.numberOfLoops = -1;
    [player play];

    UIActionSheet *actionSheet = [[UIActionSheet alloc]initWithTitle:nil delegate:self cancelButtonTitle:@"Cancel" destructiveButtonTitle:nil otherButtonTitles:nil];
    [actionSheet setActionSheetStyle:UIActionSheetStyleBlackTranslucent];
    [actionSheet showInView:self.view];
//  [actionSheet showInView:[[UIApplication sharedApplication] keyWindow]];
    [actionSheet release];

}
- (void)actionSheet:(UIActionSheet *)actionSheet clickedButtonAtIndex:(NSInteger)buttonIndex
{
    [player stop];
    if(buttonIndex == 0)
    {
        NSLog(@"OK Tapped");
    }
    if(buttonIndex ==  1)
    {
        NSLog(@"Cancel Tapped");
    }
}

这张图片显示了我的应用程序视图:

警报应用程序视图

I have made an app which is an Customized Alarm App. The UILocalNotification should invoke at the time selected by me in UIDatePicker, but is is not invoking at correct timing. For example, I selected the time as 2:00PM for the alarm, so the notification will invoke between 2:00PM and 2:01PM... but not sure when... it is giving me a delay of random time. In my UITableView you can see that it is the description displayed is also comes wrong. I know I am from India so it showing GMT timings, but can it could be corrected?

Here is my whole code:-
-----------------------------AppDelegate.m File :------------------------------

@synthesize window,viewController,timeViewController;
NSString *kRemindMeNotificationDataKey = @"kRemindMeNotificationDataKey";
#pragma mark -
#pragma mark === Application Delegate Methods ===
#pragma mark -
- (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions {    
    int x = [[NSUserDefaults standardUserDefaults] integerForKey:@"Mayank"];
    if( x == 1 )
    {
        timeViewController = [[TimeViewController alloc]initWithNibName:@"TimeViewController" bundle:nil];
        timeViewController.modalTransitionStyle = UIModalTransitionStyleCrossDissolve;
        CGRect statusBarFrame = [UIApplication sharedApplication].statusBarFrame;
        CGRect frame = timeViewController.view.frame;
        frame.origin = CGPointMake(frame.origin.x,frame.origin.y + statusBarFrame.size.height );
        timeViewController.view.frame = frame;
        [self.window addSubview:timeViewController.view];
    }
    else
    {
        [[NSUserDefaults standardUserDefaults]setInteger:1 forKey:@"Mayank"];
        [[NSUserDefaults standardUserDefaults]synchronize]; 
        UIAlertView *alert = [[UIAlertView alloc] initWithTitle:@"Do you want to set the Default Alarm?" message:@"at 4:20 PM" delegate:self cancelButtonTitle:@"Cancel" otherButtonTitles:@"Ok",nil];
        [alert show];
        [alert release];
    }
    sleep(1);
    [self.window makeKeyAndVisible];
    application.applicationIconBadgeNumber = 0;
    // Handle launching from a notification
    UILocalNotification *localNotification =
    [launchOptions objectForKey:UIApplicationLaunchOptionsLocalNotificationKey];
    if (localNotification) {
        NSString *reminderText = [localNotification.userInfo 
                                  objectForKey:kRemindMeNotificationDataKey];
        [viewController showReminder:reminderText];
    }
    return YES;
}
- (void)alertView:(UIAlertView *)alertView clickedButtonAtIndex:(NSInteger)buttonIndex
{
    if(buttonIndex == 0)
    {
        timeViewController = [[TimeViewController alloc]initWithNibName:@"TimeViewController" bundle:nil];
        timeViewController.modalTransitionStyle = UIModalTransitionStyleCrossDissolve;
        CGRect statusBarFrame = [UIApplication sharedApplication].statusBarFrame;
        CGRect frame = timeViewController.view.frame;
        frame.origin = CGPointMake(frame.origin.x,frame.origin.y + statusBarFrame.size.height );
        timeViewController.view.frame = frame;
        [self.window addSubview:timeViewController.view];
    }
    if(buttonIndex == 1)
    {
        viewController = [[SetAlarmViewController alloc]initWithNibName:@"SetAlarmViewController" bundle:nil];
        viewController.modalTransitionStyle = UIModalTransitionStyleCrossDissolve;
        CGRect statusBarFrame = [UIApplication sharedApplication].statusBarFrame;
        CGRect frame = viewController.view.frame;
        frame.origin = CGPointMake(frame.origin.x,frame.origin.y + statusBarFrame.size.height );
        viewController.view.frame = frame;
        [self.window addSubview:viewController.view];       
    }
}
- (void)application:(UIApplication *)application 
didReceiveLocalNotification:(UILocalNotification *)notification {
    NSString *reminderText = [notification.userInfo
                              objectForKey:kRemindMeNotificationDataKey];
    [viewController showReminder:reminderText];
    application.applicationIconBadgeNumber = 0;
}

-----------------------------mainViewController.m File :------------------------------

@implementation SetAlarmViewController
@synthesize datePicker,tableview, eventText,titleBar,setAlarmButton,returnKeyType;


// Implement viewDidLoad to do additional setup after loading the view, typically from a nib.
- (void)viewDidLoad {

    [super viewDidLoad];

    appDelegate = (The420DudeAppDelegate *)[[UIApplication sharedApplication] delegate];    
    eventText.returnKeyType = UIReturnKeyDone;

//  datePicker.minimumDate = [NSDate date];
    NSDate *now = [NSDate date];
    [datePicker setDate:now animated:YES];
    eventText.delegate = self;
    index = 0;
}

-(void)viewDidAppear:(BOOL)animated
{
    [super viewDidAppear:YES];
    [self.tableview reloadData];
}

- (IBAction) scheduleAlarm:(id) sender {
    [eventText resignFirstResponder];

// Get the current date
    NSDate *pickerDate = [self.datePicker date];

    UILocalNotification *localNotif = [[UILocalNotification alloc] init];
    if (localNotif == nil)
        return;
    localNotif.fireDate = pickerDate;
//  NSLog(@"%@",localNotif.fireDate);
    localNotif.timeZone = [NSTimeZone defaultTimeZone];
//  NSLog(@"%@",localNotif.timeZone);

    // Notification details
    localNotif.alertBody = [eventText text];

    // Set the action button
    localNotif.alertAction = @"Show me";
    localNotif.repeatInterval = NSDayCalendarUnit;
    localNotif.soundName = @"jet.wav";
    // Specify custom data for the notification
    NSDictionary *userDict = [NSDictionary dictionaryWithObject:eventText.text
                                                         forKey:kRemindMeNotificationDataKey];
    localNotif.userInfo = userDict;

    // Schedule the notification
    [[UIApplication sharedApplication] scheduleLocalNotification:localNotif];
    [localNotif release];

    [self.tableview reloadData];
    eventText.text = @"";

    viewController = [[TimeViewController alloc] initWithNibName:@"TimeViewController" bundle:nil];
    [self presentModalViewController:viewController animated:YES];
}

#pragma mark -
#pragma mark Table view data source

- (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView {
    // Return the number of sections.
    return 1;
}


- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section {
    // Return the number of rows in the section.
    return [[[UIApplication sharedApplication] scheduledLocalNotifications] count];
}

- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath
{
    index = indexPath.row;
    UIAlertView *alertView = [[UIAlertView alloc] initWithTitle:@"Warning!!!" 
                                                        message:@"Are you sure you want to Delete???" delegate:self
                                              cancelButtonTitle:@"Cancel"
                                              otherButtonTitles:@"Ok",nil];
    [alertView show];
    [alertView release];

}
- (void)alertView:(UIAlertView *)alertView clickedButtonAtIndex:(NSInteger)buttonIndex
{
    NSArray *notificationArray = [[UIApplication sharedApplication] scheduledLocalNotifications];
    UILocalNotification *notify = [notificationArray objectAtIndex:index];

    if(buttonIndex == 0)
    {
        // Do Nothing on Tapping Cancel...
    }
    if(buttonIndex ==1)
    {
        if(notify)
            [[UIApplication sharedApplication] cancelLocalNotification:notify];
    }
    [self.tableview reloadData];
}

// Customize the appearance of table view cells.
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {

    static NSString *CellIdentifier = @"Cell";

    UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:CellIdentifier];
    if (cell == nil) {
        cell = [[[UITableViewCell alloc] initWithStyle:UITableViewCellStyleSubtitle reuseIdentifier:CellIdentifier] autorelease];
    }
    // Configure the cell...

    NSArray *notificationArray = [[UIApplication sharedApplication] scheduledLocalNotifications];
    UILocalNotification *notif = [notificationArray objectAtIndex:indexPath.row];

    [cell.textLabel setText:notif.alertBody];
    [cell.detailTextLabel setText:[notif.fireDate description]];    
    return cell;
}


- (void)showReminder:(NSString *)text {
    /*
     UIAlertView *alertView = [[UIAlertView alloc] initWithTitle:@"Reminder" 
     message:@"hello" delegate:self
     cancelButtonTitle:@"OK"
     otherButtonTitles:nil];
     [alertView show];
     [self.tableview reloadData];
     [alertView release];
     */

    NSString *path = [[NSBundle mainBundle]pathForResource:@"jet" ofType:@"wav"];
    NSURL *url = [NSURL fileURLWithPath:path];

    player = [[AVAudioPlayer alloc]initWithContentsOfURL:url error:nil];
    player.numberOfLoops = -1;
    [player play];

    UIActionSheet *actionSheet = [[UIActionSheet alloc]initWithTitle:nil delegate:self cancelButtonTitle:@"Cancel" destructiveButtonTitle:nil otherButtonTitles:nil];
    [actionSheet setActionSheetStyle:UIActionSheetStyleBlackTranslucent];
    [actionSheet showInView:self.view];
//  [actionSheet showInView:[[UIApplication sharedApplication] keyWindow]];
    [actionSheet release];

}
- (void)actionSheet:(UIActionSheet *)actionSheet clickedButtonAtIndex:(NSInteger)buttonIndex
{
    [player stop];
    if(buttonIndex == 0)
    {
        NSLog(@"OK Tapped");
    }
    if(buttonIndex ==  1)
    {
        NSLog(@"Cancel Tapped");
    }
}

This Pic Shows My App View :

Alarm App View

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(3

有深☉意 2024-12-29 11:53:34
 NSDate *pickerDate = [self.pickerTime date];
// Break the date up into components
NSDateComponents *dateComponents = [calendar components:( NSYearCalendarUnit | NSMonthCalendarUnit |  NSDayCalendarUnit ) fromDate:pickerDate];
NSDateComponents *timeComponents = [calendar components:( NSHourCalendarUnit | NSMinuteCalendarUnit | NSSecondCalendarUnit) fromDate:pickerDate];
// Set up the fire time
NSDateComponents *dateComps = [[NSDateComponents alloc] init];
[dateComps setDay:[dateComponents day]];
[dateComps setMonth:[dateComponents month]];
[dateComps setYear:[dateComponents year]];
[dateComps setHour:[timeComponents hour]];
[dateComps setMinute:[timeComponents minute]];
**[dateComps setSecond:00];**
NSDate *itemDate = [calendar dateFromComponents:dateComps];
localNotification.fireDate = itemDate;
 NSDate *pickerDate = [self.pickerTime date];
// Break the date up into components
NSDateComponents *dateComponents = [calendar components:( NSYearCalendarUnit | NSMonthCalendarUnit |  NSDayCalendarUnit ) fromDate:pickerDate];
NSDateComponents *timeComponents = [calendar components:( NSHourCalendarUnit | NSMinuteCalendarUnit | NSSecondCalendarUnit) fromDate:pickerDate];
// Set up the fire time
NSDateComponents *dateComps = [[NSDateComponents alloc] init];
[dateComps setDay:[dateComponents day]];
[dateComps setMonth:[dateComponents month]];
[dateComps setYear:[dateComponents year]];
[dateComps setHour:[timeComponents hour]];
[dateComps setMinute:[timeComponents minute]];
**[dateComps setSecond:00];**
NSDate *itemDate = [calendar dateFromComponents:dateComps];
localNotification.fireDate = itemDate;
那些过往 2024-12-29 11:53:34

请记住,日期选择器的日期与 GMT 一致。您必须自行转换为您自己的时区。这可能是您的情况的问题。

just keep in mind, the date from datepicker comes aligned with GMT. you have to convert to your own timezone by yourself. this could be the issue in your case.

半衬遮猫 2024-12-29 11:53:34

我认为这里的问题与 NSDatePicker 返回当前时间的秒数以及所选时间有关。您需要从 NSDatePicker 返回的日期中去除秒数,并将其用于警报和本地通知。

NSDate *selectedDate = [datePicker date]; // you don't need to alloc-init the variable first
NSCalendar *cal = [NSCalendar currentCalendar];  
NSDateComponents *dc = [cal components: (NSYearCalendarUnit | NSMonthCalendarUnit | NSDayCalendarUnit | NSHourCalendarUnit | NSMinuteCalendarUnit) fromDate:selectedDate];
selectedDate = [cal dateFromComponents:dc];

// 现在你的闹钟有一个零秒的 NSDate
您的通知应该更加准确,但我认为它们不能保证准确无误。

I think the problem here has to do with the NSDatePicker returning the seconds of the current time along with the selected time. You need to strip the seconds from the date returned by the NSDatePicker and use that for your alarm and local notification.

NSDate *selectedDate = [datePicker date]; // you don't need to alloc-init the variable first
NSCalendar *cal = [NSCalendar currentCalendar];  
NSDateComponents *dc = [cal components: (NSYearCalendarUnit | NSMonthCalendarUnit | NSDayCalendarUnit | NSHourCalendarUnit | NSMinuteCalendarUnit) fromDate:selectedDate];
selectedDate = [cal dateFromComponents:dc];

// now you have an NSDate with zero seconds for your alarm
You should get much better accuracy on your notifications, but I don't think they're guaranteed to be exactly on the split second.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文