zoukankan      html  css  js  c++  java
  • iOS UILocalNotification定时提醒

         在iOS中有两类信息提示推送方式,一类是远程服务器推送(APNS),还有一类就是本地通知UILocalNotification,今天就简要的记录一下UILocalNotification的使用,详情如下:

     UILocalNotification *notifity=[[UILocalNotification alloc] init];
            NSDateFormatter *formattr=[[NSDateFormatter alloc] init];
    //格式化时间 [formattr setDateFormat:@"HH:mm"]; //触发通知时间 NSDate *now=[formattr dateFromString:[NSString stringWithFormat:@"%@",strTimer]]; notifity.fireDate=now; //时区 notifity.timeZone=[NSTimeZone defaultTimeZone]; //通知重复提示的单位,可以是周(NSWeekdayCalendarUnit)分钟(NSMinuteCalendarUnit)秒(NSSecondCalendarUnit)月(NSMonthCalendarUnit)年(NSYearCalendarUnit)

    notifity.repeatInterval=NSDayCalendarUnit;
            //通知内容
            notifity.alertBody=@"这是一个通知";
            //通知触发时播放的声音
            notifity.soundName=UILocalNotificationDefaultSoundName;
            //执行通知注册
            [[UIApplication sharedApplication] scheduleLocalNotification:notifity];
    

        如果要在通知栏中携带参数信息,可以使用下面的方式:

       NSDictionary *dic = [NSDictionary dictionaryWithObject:@"name" forKey:@"key"];  
    
        notification.userInfo = dic;

        如果软件是在运行中,则可以通过AppDelegate中的回调方法获取并处理参数信息:

    -(void)application:(UIApplication *)application didReceiveLocalNotification:(UILocalNotification *)notification
    {
        if (notification) {
            NSDictionary *userInfo =  notification.userInfo;
            NSString *obj = [userInfo objectForKey:@"key"];
            NSLog(@"%@",obj);
        }
    }
    

    另外,可以通过两种方式取消注册的本地通知,一种是取消指定的通知,第二种是取消所有的注册通知:

    [[UIApplication sharedApplication] cancelLocalNotification:localNotification];
     [[UIApplication sharedApplication] cancelAllLocalNotification];
    

    以上就是我今天所学的知识,在此处记录并且与大家分享,如有问题请指出,共同探讨学习!

  • 相关阅读:
    依赖注入和控制反转概念及目的(新手必读)
    电商秒杀系统可能遇到的坑及思路
    Java中的ReentrantLock和synchronized两种锁定机制的对比
    Java集合---HashMap源码剖析
    Java中的字符串常量池
    redhat7:用户、组和权限
    redhat7下对用户账户的管理
    通过Tacker将NFV引入OpenStack
    github中的一个快捷键
    关于
  • 原文地址:https://www.cnblogs.com/boyuanmeng/p/3830082.html
Copyright © 2011-2022 走看看