iPhone:如何设置重复的每日/每小时本地通知? [英] iPhone: How to set repeat daily/hourly local notification?

查看:41
本文介绍了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 属性.iOS 处理它;自己的通知.只需添加这一行......你错过了.否则你的代码很好&会工作的.

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:如何设置重复的每日/每小时本地通知?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆