1.AppDelegate.m里
- (void)application:(UIApplication*)application didReceiveLocalNotification:(UILocalNotification*)notification{
//如果应用程序在前台,将应用程序图标上红色徽标中数字设为0
application.applicationIconBadgeNumber=0;
//使用UIAlertView显示本地通知的信息
[[[UIAlertViewalloc]initWithTitle:@"收到通知"
message:notification.alertBody
delegate:nilcancelButtonTitle:@"确定"otherButtonTitles:nil]show];
}
2.发送通知按钮实现方法
if([UIApplicationinstancesRespondToSelector:@selector(registerUserNotificationSettings:)])
{
[[UIApplicationsharedApplication]registerUserNotificationSettings:[UIUserNotificationSettingssettingsForTypes:UIUserNotificationTypeAlert|UIUserNotificationTypeBadge|UIUserNotificationTypeSoundcategories:nil]];
}
//创建本地通知对象
UILocalNotification*ln = [[UILocalNotificationalloc]init];
//1.设置通知的内容(如果此属性不设置是不会发送通知的)
ln.alertBody=@"小明,你妈叫你回家吃饭了!";
//2.设置通知触发的开始时间
ln.fireDate= [NSDatedateWithTimeIntervalSinceNow:10];
//3.设置重复通知的时间,间隔
ln.repeatInterval=kCFCalendarUnitMinute;
ln.timeZone= [NSTimeZonedefaultTimeZone];
//5.设置应用图标右上角的数字
ln.applicationIconBadgeNumber=3;
ln.hasAction=YES;
//6.设置点击推送通知进入界面的时候显示,加载图片
ln.alertLaunchImage=@"";
//8设置一些额外信息
ln.userInfo=@{@"QQ":@"55555",@"info":@"约了没"};
//让应用调度通知
[[UIApplicationsharedApplication]scheduleLocalNotification:ln];
3.取消发送通知
//获取所有处于调度中本地通知数组
NSArray*localArray = [[UIApplicationsharedApplication]scheduledLocalNotifications];
if(localArray)
{
for(UILocalNotification*notiinlocalArray)
{
NSDictionary*dict = noti.userInfo;
if(dict)
{
//如果找到要取消的通知
NSString*inKey = [dictobjectForKey:@"QQ"];
if([inKeyisEqualToString:@"55555"])
{
//取消调度该通知
[[UIApplicationsharedApplication]cancelLocalNotification:noti];//②
}
}
}
}