IOS本地通知

发送通知:  

  UILocalNotification *newNotification = [[UILocalNotification alloc] init];

    if (newNotification) {

//时区

        newNotification.timeZone=[NSTimeZone defaultTimeZone];

//推送事件---10秒后

        newNotification.fireDate=[[NSDate datedateByAddingTimeInterval: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:dicUserInfoforKey:@"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 userInfoobjectForKey:@"key"] intValue]==@"字典值") {

            [[UIApplication sharedApplicationcancelLocalNotification: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];

}

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


  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值