UILocalNotification本地通知的使用方法

UILocalNotification一般做为定时器使用,可以定时提醒,定时唤醒。

1.对象的创建

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

    NSDateFormatter *formatter = [[NSDateFormatter alloc] init];

    NSString *noticeDate = @"2012-07-30 12:00:00";
    formatter.dateFormat = @"yyyy-MM-dd HH:mm";
    //设置触发时间
    notification.fireDate = [formatter dateFromString:noticeDate];
    [formatter release];

    //触发频率,0表示只有一次,频率不能为自定义的

     notification.repeatInterval = 0;

     notification.repeatInterval = kCFCalendarUnitDay;

      notification.repeatInterval = kCFCalendarUnitWeek;

    //用户信息,为字典类型,内容可以自己设定,这里存放了唯一的key做为标记

        NSDictionary *dict = [NSDictionary dictionaryWithObjectsAndKeys:
                              [NSString stringWithFormat:@"%d", noticeInfo.notifyID],KEY,nil];
        [notification setUserInfo:dict];

   //提醒内容

   notification.alertBody = @“HELLO”;

  //提醒铃音必须为默认的或声音文件

   notification.soundName = UILocalNotificationDefaultSoundName;//@"ping.caf";//

2.对象的管理

   //获取当前所有提醒,内容为UILocalNotification数组

   [[UIApplication sharedApplication] scheduledLocalNotifications];

  //添加提醒

  UIApplication *app = [UIApplication sharedApplication];

  [app scheduleLocalNotification:notification];

  //取消提醒,遍历所有提醒,从中找到需要取消的那个

      NSArray *myArray=[[UIApplication sharedApplication] scheduledLocalNotifications];
    for (UILocalNotification *myUILocalNotification in myArray) {
        if ([[[myUILocalNotification userInfo] objectForKey:KEY] intValue] == value) {
            [[UIApplication sharedApplication] cancelLocalNotification:myUILocalNotification];
            return;
        }
    }

  //取消所有提醒

  UIApplication *app = [UIApplication sharedApplication];

  [app cancelAllLocalNotifications];

3.提醒的处理,提醒触发时会回调appdelegate的如下方法

  - (void)application:(UIApplication *)app didReceiveLocalNotification:(UILocalNotification *)notif
{
    MessageBox *messageBox = [MessageBox sharedInstance];
    [messageBox showMessageBoxWithTitle:@"本地提醒"
                                message:notif.alertBody
                               delegate:self
                      cancelButtonTitle:@"OK"
                             msgBoxType:E_TYPE_OK];
}

  
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

ChatGPT攻城狮

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值