IOS本地通知

来源:互联网 发布:单片机堆栈空间 编辑:程序博客网 时间:2024/06/03 21:52
原文地址:IOS本地通知作者:simon

发送通知:  

  UILocalNotification*newNotification = [[UILocalNotificationalloc] init];

    if (newNotification) {

//时区

      newNotification.timeZone=[NSTimeZonedefaultTimeZone];

//推送事件---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 = [NSDictionarydictionaryWithObject:dicUserInfoforKey:@"dictionary"];

       [dicUserInfo release];

      [[UIApplicationsharedApplication]scheduleLocalNotification:newNotification];

    }

   NSLog(@"Post new localNotification:%@",newNotification);

   [newNotification release];

    [poolrelease];


取消通知:

通知完一定要取消,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]==@"字典值") {

          [[UIApplicationsharedApplication] cancelLocalNotification:myUILocalNotification];

       }

    }


通知执行完调用的方法 AppDelegate.m类里面

//推送完 执行的事件

-(void)application:(UIApplication *)applicationdidReceiveLocalNotification:(UILocalNotification*)notification{

//notification是发送通知时传入的字典信息

UIAlertView *alert = [[UIAlertView alloc]initWithTitle:@"标题" message:notification.alertBody delegate:nilcancelButtonTitle:@"确定"otherButtonTitles:nil];

       [alertshow];

[alert release];

}

最后还有一个地方:执行通知一定要退出应用才能收到通知。

0 0