//在ViewController.m中拖兩個按鈕,一個發(fā)送,一個取消
---------------發(fā)送按鈕-----------------
- (IBAction)fasong:(id)sender {
if([UIApplication instancesRespondToSelector:@selector(registerUserNotificationSettings:)])
{
[[UIApplication sharedApplication] registerUserNotificationSettings:[UIUserNotificationSettings settingsForTypes:UIUserNotificationTypeAlert|UIUserNotificationTypeBadge|UIUserNotificationTypeSound categories:nil]];
}
//創(chuàng)建本地通知對象
UILocalNotification*ln = [[UILocalNotification alloc]init];
//1.設(shè)置通知的內(nèi)容(如果此屬性不設(shè)置是不會發(fā)送通知的)
ln.alertBody=@"小明,你媽叫你回家吃飯了!";
//2.設(shè)置通知觸發(fā)的開始時間
ln.fireDate= [NSDate dateWithTimeIntervalSinceNow:10];
//3.設(shè)置重復(fù)通知的時間,間隔
ln.repeatInterval=kCFCalendarUnitMinute;
ln.timeZone= [NSTimeZone defaultTimeZone];
//5.設(shè)置應(yīng)用圖標右上角的數(shù)字
ln.applicationIconBadgeNumber=3;
ln.hasAction=YES;
//6.設(shè)置點擊推送通知進入界面的時候顯示,加載圖片
ln.alertLaunchImage=@"";
//8設(shè)置一些額外信息
ln.userInfo=@{@"QQ":@"55555",@"info":@"約了沒"};
//讓應(yīng)用調(diào)度通知
[[UIApplication sharedApplication]scheduleLocalNotification:ln];
}
//------------------------取消按鈕------------------
- (IBAction)quxiaofasong:(id)sender
{
//獲取所有處于調(diào)度中本地通知數(shù)組
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"])
{
//取消調(diào)度該通知
[[UIApplication sharedApplication]cancelLocalNotification:noti];//②
}
}
}
}
}