//在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];//②
}
}
}
}
}