iPhone:如何设置重复每日/每小时本地通知?

编程入门 行业动态 更新时间:2024-10-25 06:25:16
本文介绍了iPhone:如何设置重复每日/每小时本地通知?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧! 问题描述

我想测试添加本地通知。我希望它每天/每小时重复一次。我该怎么做?

I want to test add local notification. I want it repeat daily/hourly. How can I do that?

NSCalendar *calendar = [NSCalendar autoupdatingCurrentCalendar]; // Get the current date NSDate *now = [NSDate date]; // Break the date up into components NSDateComponents *dateComponents = [calendar components:( NSYearCalendarUnit | NSMonthCalendarUnit | NSDayCalendarUnit ) fromDate:now]; NSDateComponents *timeComponents = [calendar components:( NSHourCalendarUnit | NSMinuteCalendarUnit | NSSecondCalendarUnit) fromDate:now]; // 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:[timeComponents second]+10]; // Notification will fire in one minute NSDate *itemDate = [calendar dateFromComponents:dateComps]; [dateComps release]; UILocalNotification *localNotif = [[UILocalNotification alloc] init]; if (localNotif == nil) return; localNotif.fireDate = itemDate; localNotif.timeZone = [NSTimeZone defaultTimeZone]; // Notification details localNotif.alertBody = @"Hello World"; // Set the action button localNotif.alertAction = @"View"; localNotif.soundName = UILocalNotificationDefaultSoundName; applicationIconBadgeNumber++; localNotif.applicationIconBadgeNumber = applicationIconBadgeNumber; // Schedule the notification [[UIApplication sharedApplication] scheduleLocalNotification:localNotif]; [localNotif release];

推荐答案

如果要在特定时间设置通知每天...然后你需要设置通知 repeatInterval property.iOS处理它; s拥有通知。只需添加这一行...你错过了。否则你的代码很好&将会有效。

If you want to set the Notifications at a particular time on daily ... .then you need to set the notification repeatInterval property.iOS handles of it;s own about the notification.Just add this single line ...which you missed out. Otherwise you code is fine & will work.

notif.repeatInterval = NSDayCalendarUnit;

更多推荐

iPhone:如何设置重复每日/每小时本地通知?

本文发布于:2023-11-27 07:00:43,感谢您对本站的认可!
本文链接:https://www.elefans.com/category/jswz/34/1637061.html
版权声明:本站内容均来自互联网,仅供演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系,我们将在24小时内删除。
本文标签:每小时   如何设置   通知   iPhone

发布评论

评论列表 (有 0 条评论)
草根站长

>www.elefans.com

编程频道|电子爱好者 - 技术资讯及电子产品介绍!