您的位置:首页 > 移动开发 > IOS开发

ios 本地推送的添加和取消

2012-11-29 14:36 651 查看
//取消

- (void)shutdownClock:(int)clockID

{

NSArray *localNotifications = [[UIApplication sharedApplication] scheduledLocalNotifications];

for(UILocalNotification *notification in localNotifications)

{

if ([[[notification userInfo] objectForKey:@"ActivityClock"] intValue] == clockID) {

NSLog(@"Shutdown localNotification:%@", [notification fireDate]);

[[UIApplication sharedApplication] cancelLocalNotification:notification];

}

}

}

//添加

- (void)addClock:(int)clockID(时间) body:(NSString *)name(文字)

{

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

notif1.fireDate = [NSDate dateWithTimeIntervalSinceNow:clockID];

notif1.soundName = UILocalNotificationDefaultSoundName;

notif1.alertBody = [NSString stringWithFormat:name];

//显示在icon上的红色圈中的数子

notif1.applicationIconBadgeNumber = 1;

//设置userinfo 方便在之后需要撤销的时候使用

NSString *strnum=[NSString stringWithFormat:@"%i",clockID];

NSDictionary *info = [NSDictionary dictionaryWithObject:strnum forKey:@"ActivityClock"];

notif1.userInfo = info;

[[UIApplication sharedApplication] scheduleLocalNotification:notif1];

[notif1 release];

}

时间的计算方法

NSDate* now = [NSDate date];

NSCalendar *gregorian = [[NSCalendar alloc]

initWithCalendarIdentifier:NSGregorianCalendar];

NSDateComponents *dateComponents = [gregorian components:(NSHourCalendarUnit

| NSMinuteCalendarUnit

| NSSecondCalendarUnit

) fromDate:now];

[gregorian release];

//当前的"小时"

NSInteger currentHour = [dateComponents hour];

NSInteger currentMin = [dateComponents minute];

//当前“分钟”

//24小时和闹钟时间的计算

int currentTotal = currentHour*3600 + currentMin * 60;

int alarmTotal = hour * 3600 + min *60;

ontimer = alarmTotal - currentTotal;

ontimer就是得到的时间,传给方法就可以了
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: