//在ViewController.m中拖两个按钮,一个发送,一个取消
---------------发送按钮-----------------
- (IBAction)fasong:(id)sender {
if([UIApplication instancesRespondToSelector:@selector(registerUserNotificationSettings:)])
{
[[UIApplication sharedApplication] registerUserNotificationSettings:[UIUserNotificationSettings settingsForTypes:UIUserNotificationTypeAlert|UIUserNotificationTypeBadge|UIUserNotificationTypeSound categories:nil]];
}
//创建本地通知对象
UILocalNotification*ln = [[UILocalNotification alloc]init];
//1.设置通知的内容(如果此属性不设置是不会发送通知的)
ln.alertBody=@"小明,你妈叫你回家吃饭了!";
//2.设置通知触发的开始时间
ln.fireDate= [NSDate dateWithTimeIntervalSinceNow:10];
//3.设置重复通知的时间,间隔
ln.repeatInterval=kCFCalendarUnitMinute;
ln.timeZone= [NSTimeZone defaultTimeZone];
//5.设置应用图标右上角的数字
ln.applicationIconBadgeNumber=3;
ln.hasAction=YES;
//6.设置点击推送通知进入界面的时候显示,加载图片
ln.alertLaunchImage=@"";
//8设置一些额外信息
ln.userInfo=@{@"QQ":@"55555",@"info":@"约了没"};
//让应用调度通知
[[UIApplication sharedApplication]scheduleLocalNotification:ln];
}
//------------------------取消按钮------------------
- (IBAction)quxiaofasong:(id)sender
{
//获取所有处于调度中本地通知数组
NSArray*localArray = [[UIApplication sharedApplication]scheduledLocalNotifications];
if(localArray)
{
for(UILocalNotification*noti in localArray)
{
NSDictionary*dict = noti.userInfo;
if(dict)
{
//如果找到要取消的通知
NSString*inKey = [dict objectForKey:@"QQ"];
if([inKey isEqualToString:@"55555"])
{
//取消调度该通知
[[UIApplication sharedApplication]cancelLocalNotification:noti];//②
}
}
}
}
}