iOS开发之本地通知UILocalNotification

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

  • scheduled time:时间周期,用来指定iOS系统发送通知的日期和时间;
  • notification type:通知类型,包括警告信息、动作按钮的标题、应用图标上的badge(数字标记)和播放的声音;
  • 自定义数据:本地通知可以包含一个dictionary类型的本地数据。

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

本地通知的使用:注册通知,初始化通知,配置通知,添加通知 。

注册通知:

[[UIApplication sharedApplication] registerUserNotificationSettings:[UIUserNotificationSettings settingsForTypes:UIUserNotificationTypeBadge|UIUserNotificationTypeSound|UIUserNotificationTypeAlert categories:nil]];
// 初始化本地通知对象
UILocalNotification *notification = [[UILocalNotification alloc] init];
if (notification) {
    // 设置通知的提醒时间
    NSDate *currentDate   = [NSDate date];
    notification.timeZone = [NSTimeZone defaultTimeZone]; 
    notification.fireDate = [currentDate dateByAddingTimeInterval:5.0];

    // 设置重复间隔
    notification.repeatInterval = kCFCalendarUnitDay;

    // 设置提醒的文字内容
    notification.alertBody   = @"Wake up, man";
    notification.alertAction = NSLocalizedString(@"起床了", nil);

    // 通知提示音 使用默认的
    notification.soundName= UILocalNotificationDefaultSoundName;

    // 设置应用程序右上角的提醒个数
    notification.applicationIconBadgeNumber++;

    // 设定通知的userInfo,用来标识该通知
    NSMutableDictionary *aUserInfo = [[NSMutableDictionary alloc] init];
    aUserInfo[kLocalNotificationID] = @"LocalNotificationID";
    notification.userInfo = aUserInfo;

    // 将通知添加到系统中
    [[UIApplication sharedApplication] scheduleLocalNotification:notification];
}

接收通知:

- (void)application:(UIApplication *)application didReceiveLocalNotification:(UILocalNotification *)notification {
    UIAlertController *alertController = [UIAlertController alertControllerWithTitle:@"提示" message:notification.alertBody preferredStyle:UIAlertControllerStyleAlert];
    [alertController addAction:[UIAlertAction actionWithTitle:@"确定" style:UIAlertActionStyleDefault handler:nil]];
    [_window.rootViewController presentViewController:alertController animated:YES completion:nil];
}

通知的另外一种注册方式:

- (void)application:(UIApplication *)application didRegisterUserNotificationSettings:(UIUserNotificationSettings *)notificationSettings {
    [application registerUserNotificationSettings:notificationSettings];
}

取消本地通知:

//取消某一个通知
    NSArray *notificaitons = [[UIApplication sharedApplication] scheduledLocalNotifications];
    //获取当前所有的本地通知
    if (!notificaitons || notificaitons.count <= 0) {
        return;
    }
    for (UILocalNotification *notify in notificaitons) {
        if ([[notify.userInfo objectForKey:@"id"] isEqualToString:LOCAL_NOTIFY_SCHEDULE_ID]) {
            //取消一个特定的通知
            [[UIApplication sharedApplication] cancelLocalNotification:notify];
            break;
        }
    }

    //取消所有的本地通知
    [[UIApplication sharedApplication] cancelAllLocalNotifications];

本地通知的响应:
如果已经注册了本地通知,当客户端响应通知时:
1、应用程序在后台的时候,本地通知会给设备送达一个和远程通知一样的提醒,提醒的样式由用户在手机设置中设置;
2、应用程序正在运行中,则设备不会收到提醒,但是会走应用程序delegate中的方法。

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

} 

如果想实现程序在后台时候的提醒效果,可以在上面这个方法中添加相关代码,示例代码:

if ([[notification.userInfo objectForKey:@"id"] isEqualToString:@"affair.schedule"]) {  
       UIAlertView *alert = [[UIAlertView alloc] initWithTitle:@"test" message:notification.alertBody delegate:nil cancelButtonTitle:@"关闭" otherButtonTitles:notification.alertAction, nil nil];  
       [alert show];  
   } 

需要注意的是,在情况1中,如果用户点击提醒进入应用程序,也会执行收到本地通知的回调方法,这种情况下如果添加了上面那段代码,则会出现连续出现两次提示,为了解决这个问题,修改代码如下:

if ([[notification.userInfo objectForKey:@"id"] isEqualToString:@"affair.schedule"]) {  
       //判断应用程序当前的运行状态,如果是激活状态,则进行提醒,否则不提醒  
       if (application.applicationState == UIApplicationStateActive) {  
           UIAlertView *alert = [[UIAlertView alloc] initWithTitle:@"test" message:notification.alertBody delegate:nil cancelButtonTitle:@"关闭" otherButtonTitles:notification.alertAction, nil nil];  
           [alert show];  
       }  
   } 
  • 1
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 1
    评论

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

打赏作者

╰つ栺尖篴夢ゞ

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

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

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

打赏作者

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

抵扣说明:

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

余额充值