ios-实现本地通知(UILocalNotification)

本文深入探讨了iOS应用中的本地通知机制,包括其基本概念、实现方式以及复杂通知的运用,通过具体代码实例展示了如何在应用中实现定时通知与自定义通知功能。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

ios应用App如果在后台执行,ios系统允许它在受限的时间内运行,从而给App推送本地通知。

1、本地通知介绍:

local notification,用于基于时间行为的通知,比如有关日历或者todo列表的小应用。另外,应用如果在后台执行,ios允许它在受限的时间内运行。比如,一个应用,在后台运行,向应用的服务器端获取消息,当消息到达时,比如下载更新版本的提示消息,通过本地通知机制通知用户。

本地通知是UILocalNotification的实例,主要有三类属性:

1、scheduled time,时间周期,用来指定iOS系统发送通知的日期和时间;

2、notification type,通知类型,包括警告信息、动作按钮的标题、应用图标上的badge(数字标记)和播放的声音;

3、自定义数据,本地通知可以包含一个dictionary类型的本地数据。

4、对本地通知的数量限制,ios最多允许最近本地通知数量是64个,超过限制的本地通知将被ios忽略。


2、实际运用,写个定时App,代码实现如下:

/*启动应用后,就发出一个定时通知。这时如果按Home键退出,过10秒后就会启动通知*/
- (void)setLocalNotification
{
    UILocalNotification *notification=[[[UILocalNotification alloc] init] autorelease];
    if (notification!=nil)
    {
        NSLog(@">>支持本地通知!");
        NSDate *now = [NSDate new];
        notification.fireDate = [now dateByAddingTimeInterval:5];  // addTimeInterval ios4.0以下版本
        notification.timeZone = [NSTimeZone defaultTimeZone];
        notification.alertBody = @"该去吃晚饭了!";
        notification.soundName = UILocalNotificationDefaultSoundName;
        [[UIApplication sharedApplication] scheduleLocalNotification:notification];
    }
}


至此,简单的本地通知已经介绍完毕,效果图如下:

3、复杂的本地通知,代码实现如下:

- (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions
{
    // 首先取消通知次数
    application.applicationIconBadgeNumber = 0;
    return YES;
}


<pre name="code" class="cpp">// 本地通知实现
- (void)clickSwt:(id)sender
{
    UISwitch *swt = (UISwitch *)sender;
    int tag = swt.tag - 200;
    
    if (swt.on)
    {
        UILocalNotification *notification = [[[UILocalNotification alloc] init] autorelease];
        NSDate *now = [NSDate date];
        notification.timeZone = [NSTimeZone defaultTimeZone];
        notification.repeatInterval = 0;
        notification.alertAction = @"显示";
        notification.soundName = UILocalNotificationDefaultSoundName;
        NSDictionary *dict = [NSDictionary dictionaryWithObjectsAndKeys:[NSString stringWithFormat:@"%d",tag], @"key1", nil];
        [notification setUserInfo:dict];
        switch (tag)
        {
            case 0:
            {
                notification.fireDate=[now dateByAddingTimeInterval:5];
                notification.applicationIconBadgeNumber = 1;
                notification.alertBody = @"通知一";
            }
                break;
            case 1:
            {
                notification.fireDate=[now dateByAddingTimeInterval:6];
                notification.applicationIconBadgeNumber = 2;
                notification.alertBody = @"通知二";
            }
                break;
            case 2:
            {
                notification.fireDate=[now dateByAddingTimeInterval:7];
                notification.applicationIconBadgeNumber = 3;
                notification.alertBody = @"通知三";
            }
                break;
            default:
                break;
        }
        
        [[UIApplication sharedApplication] scheduleLocalNotification:notification];
    }
    else
    {
        NSArray *myArray=[[UIApplication sharedApplication] scheduledLocalNotifications];
        for (int i=0; i<[myArray count]; i++)
        {
            UILocalNotification *myUILocalNotification=[myArray objectAtIndex:i];
            if ([[[myUILocalNotification userInfo] objectForKey:@"key1"] intValue] == tag)
            {
                [[UIApplication sharedApplication] cancelLocalNotification:myUILocalNotification];
            }
        }
    }
}


至此,复杂的本地通知已经介绍完毕,效果如下:
      


评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值