UILocalNotification 安排警报

发布于 2024-10-17 08:42:55 字数 2895 浏览 5 评论 0原文

我安排了一个通知,并在它显示警报消息之前给它 60 分钟警告...

添加通知后,我的应用程序委托中的方法就会被调用:

- (void)application:(UIApplication *)application didReceiveLocalNotification:(UILocalNotification *)notification

我怎样才能知道它将在后台显示为警报?我是否需要重写或使用任何其他委托方法,以确保给定间隔为 60 分钟的预定警报...

- (void)scheduleNotificationWithItem:(NSDate *)item interval:(int)minutesBefore
{   
    NSCalendar *calendar = [NSCalendar autoupdatingCurrentCalendar];

    NSDateComponents *dateComps = [[NSDateComponents alloc] init];
    //NSCalendar *gregorian = [[NSCalendar alloc] initWithCalendarIdentifier:NSGregorianCalendar];

    NSDateComponents *currentDateComponents = [calendar components:( NSWeekdayCalendarUnit | 
                                                                     NSYearCalendarUnit | NSMonthCalendarUnit | NSHourCalendarUnit | NSMinuteCalendarUnit | NSWeekCalendarUnit | NSMinuteCalendarUnit) fromDate:item];

    NSLog(@"- current components year = %i , month = %i , week = % i, weekday = %i", [currentDateComponents year], [currentDateComponents month], [currentDateComponents week], [currentDateComponents weekday]);

    NSLog(@"[currentDateComponents minute]: %i",    [currentDateComponents minute]);
    NSLog(@"[currentDateComponents hour]: %i",      [currentDateComponents hour]);
    NSLog(@"[currentDateComponents day]: %i",       [currentDateComponents day]);
    NSLog(@"[currentDateComponents week]: %i",      [currentDateComponents week]);
    NSLog(@"[currentDateComponents month]: %i",     [currentDateComponents month]);
    NSLog(@"[currentDateComponents year]: %i",      [currentDateComponents year]);

    [dateComps setDay: [currentDateComponents day]];

    [dateComps setMonth:[currentDateComponents month]];

    [dateComps setYear:[currentDateComponents year]];

    [dateComps setHour:[currentDateComponents hour]];

    [dateComps setMinute:[currentDateComponents minute]];

    NSDate *itemDate = [calendar dateFromComponents:dateComps];

    [dateComps release];

    UILocalNotification *localNotif = [[UILocalNotification alloc] init];

    if (localNotif == nil)
        return;

    localNotif.fireDate = [itemDate addTimeInterval:-(minutesBefore*60)];

    localNotif.timeZone = [NSTimeZone defaultTimeZone];

    localNotif.alertBody = [NSString stringWithFormat:@"%@\n%@",
                            streetAddress,
                            stringOfWhenAuctionIsOn];

    localNotif.alertAction = NSLocalizedString(@"View Details", nil);

    localNotif.soundName = UILocalNotificationDefaultSoundName;

    localNotif.applicationIconBadgeNumber = 1;

    NSDictionary *infoDict = [NSDictionary dictionaryWithObject:streetAddress
                                                         forKey:idOfStreetAlert];

    localNotif.userInfo = infoDict;

    [[UIApplication sharedApplication] scheduleLocalNotification:localNotif];

I schedule a Notification, and give it 60 minutes warning before it should display an alert message...

As soon as I add the notification the method in my App Delegate is called:

- (void)application:(UIApplication *)application didReceiveLocalNotification:(UILocalNotification *)notification

How can I know that it will show up in the background as an alert? Is there any other Delegate method I need to override or use in order to ensure that given a scheduled Alert with a 60 Minute Interval...

- (void)scheduleNotificationWithItem:(NSDate *)item interval:(int)minutesBefore
{   
    NSCalendar *calendar = [NSCalendar autoupdatingCurrentCalendar];

    NSDateComponents *dateComps = [[NSDateComponents alloc] init];
    //NSCalendar *gregorian = [[NSCalendar alloc] initWithCalendarIdentifier:NSGregorianCalendar];

    NSDateComponents *currentDateComponents = [calendar components:( NSWeekdayCalendarUnit | 
                                                                     NSYearCalendarUnit | NSMonthCalendarUnit | NSHourCalendarUnit | NSMinuteCalendarUnit | NSWeekCalendarUnit | NSMinuteCalendarUnit) fromDate:item];

    NSLog(@"- current components year = %i , month = %i , week = % i, weekday = %i", [currentDateComponents year], [currentDateComponents month], [currentDateComponents week], [currentDateComponents weekday]);

    NSLog(@"[currentDateComponents minute]: %i",    [currentDateComponents minute]);
    NSLog(@"[currentDateComponents hour]: %i",      [currentDateComponents hour]);
    NSLog(@"[currentDateComponents day]: %i",       [currentDateComponents day]);
    NSLog(@"[currentDateComponents week]: %i",      [currentDateComponents week]);
    NSLog(@"[currentDateComponents month]: %i",     [currentDateComponents month]);
    NSLog(@"[currentDateComponents year]: %i",      [currentDateComponents year]);

    [dateComps setDay: [currentDateComponents day]];

    [dateComps setMonth:[currentDateComponents month]];

    [dateComps setYear:[currentDateComponents year]];

    [dateComps setHour:[currentDateComponents hour]];

    [dateComps setMinute:[currentDateComponents minute]];

    NSDate *itemDate = [calendar dateFromComponents:dateComps];

    [dateComps release];

    UILocalNotification *localNotif = [[UILocalNotification alloc] init];

    if (localNotif == nil)
        return;

    localNotif.fireDate = [itemDate addTimeInterval:-(minutesBefore*60)];

    localNotif.timeZone = [NSTimeZone defaultTimeZone];

    localNotif.alertBody = [NSString stringWithFormat:@"%@\n%@",
                            streetAddress,
                            stringOfWhenAuctionIsOn];

    localNotif.alertAction = NSLocalizedString(@"View Details", nil);

    localNotif.soundName = UILocalNotificationDefaultSoundName;

    localNotif.applicationIconBadgeNumber = 1;

    NSDictionary *infoDict = [NSDictionary dictionaryWithObject:streetAddress
                                                         forKey:idOfStreetAlert];

    localNotif.userInfo = infoDict;

    [[UIApplication sharedApplication] scheduleLocalNotification:localNotif];

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

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

发布评论

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

评论(2

柒夜笙歌凉 2024-10-24 08:42:55

如果立即显示通知,则您的 fireDate 属性可能有问题。看起来您正在尝试验证发送的日期是否正确,但您还应该验证 fireDate 是否是您所期望的。

另外,您也许可以

localNotif.fireDate = [item dateByAddingTimeInterval:-60*minutesBefore];

实现相同的效果,而不必使用 NSDateComponents。我还没有测试过这个,但它可能会起作用。

至于当计时器触发并且您的应用程序未运行时会发生什么。如果应用程序尚未终止,application:didReceiveLocalNotification: 将被调用。如果您的应用程序已被终止,您需要检查 application:didFinishLaunchingWithOptions: 正如 @Ishu 所指出的。我通常只是将通知传递给通用方法以避免重复代码。

If the notification shows up right away there probably is something wrong with your fireDate property. It looks like you are trying to verify that the date sent is correct but you should also verify that that fireDate is what you expect.

Also, you could perhaps do

localNotif.fireDate = [item dateByAddingTimeInterval:-60*minutesBefore];

to achieve the same effect and don't have to mess around with NSDateComponents. I have not tested this but it might work.

As for what happens when the timer fires and your app is not running. If the app has not been terminated application:didReceiveLocalNotification: will get called. If on the other side your app has been terminated you need to check in application:didFinishLaunchingWithOptions: as pointed out by @Ishu. I usually just pass the notification to a common method to avoid duplicating the code.

蓝眸 2024-10-24 08:42:55

后台没有委托方法,您需要在 didFinishLaunchingWithOptions 中编写代码,

UILocalNotification *localNotif =
    [launchOptions objectForKey:UIApplicationLaunchOptionsLocalNotificationKey];

    if (localNotif) {
        //your code
        NSLog(@"Recieved Notification %@",localNotif);
    }

当应用程序从后台获取通知时,它会创建您的逻辑。不用担心应用程序会收到您设置通知的通知。

No delegate method for background, you need to write code in didFinishLaunchingWithOptions,

UILocalNotification *localNotif =
    [launchOptions objectForKey:UIApplicationLaunchOptionsLocalNotificationKey];

    if (localNotif) {
        //your code
        NSLog(@"Recieved Notification %@",localNotif);
    }

it create your logic when app get notification from background.Dont worry app get notification ones you set the notification.

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