标签:
发送通知:
UILocalNotification *newNotification = [[UILocalNotification alloc] init];
if (newNotification) {
//时区
newNotification.timeZone=[NSTimeZone defaultTimeZone];
//推送事件---10秒后
newNotification.fireDate=[[NSDate date] dateByAddingTimeInterval:10];
//推送内容
newNotification.alertBody = @"信号报警";
//应用右上角红色图标数字
newNotification.applicationIconBadgeNumber = 1;
注:
//1:格式一定要支持播放,常用的格式caf
//2:音频播放时间不能大于30秒
//3:在Resource里要找到音频文件,倒入时最好能点项目名称右键add导入
newNotification.soundName = @"jingBao2.caf";
//设置按钮
newNotification.alertAction = @"关闭";
//判断重复与否
newNotification.repeatInterval = NSWeekCalendarUnit;
//存入的字典,用于传入数据,区分多个通知
NSMutableDictionary *dicUserInfo = [[NSMutableDictionary alloc] init];
[dicUserInfo setValue:@"" forKey:@"clockID"];
float floatHeng = userLocation.location.coordinate.latitude;
float floatShu = userLocation.location.coordinate.longitude;
[dicUserInfo setValue:[NSString stringWithFormat:@"%f",strX] forKey:@"heng"];
[dicUserInfo setValue:[NSString stringWithFormat:@"%f",strY] forKey:@"shu"];
newNotification.userInfo = [NSDictionary dictionaryWithObject:dicUserInfo forKey:@"dictionary"];
[dicUserInfo release];
[[UIApplication sharedApplication] scheduleLocalNotification:newNotification];
}
NSLog(@"Post new localNotification:%@", newNotification);
[newNotification release];
[pool release];
取消通知:
通知完一定要取消,IOS最多允许最近本地通知数量是64个,超过限制的本地通知将被忽略。
1:删除应用所有通知
[[UIApplication sharedApplication] cancelAllLocalNotifications];
2:根据字典删除个别通知
key:发送通知时候传入的字典值来判断是哪个推送
for (int i=0; i<[myArray count]; i++) {
UILocalNotification *myUILocalNotification=[myArray objectAtIndex:i];
if ([[[myUILocalNotification userInfo] objectForKey:@"key"] intValue]==@"字典值") {
[[UIApplication sharedApplication] cancelLocalNotification:myUILocalNotification];
}
}
通知执行完调用的方法 AppDelegate.m类里面
//推送完 执行的事件
-(void)application:(UIApplication *)application didReceiveLocalNotification:(UILocalNotification *)notification{
//notification是发送通知时传入的字典信息
UIAlertView *alert = [[UIAlertView alloc] initWithTitle:@"标题" message:notification.alertBody delegate:nil cancelButtonTitle:@"确定" otherButtonTitles:nil];
[alert show];
[alert release];
}
最后还有一个地方:执行通知一定要退出应用才能收到通知。
标签:
原文地址:http://www.cnblogs.com/wq-gril/p/4578950.html