iOS UILocationNotification 本地通知

1.注册本地通知

/** 注册本地通知 */
- (void)registerUserNotification {
    // 判读系统版本是否是“iOS 8.0”以上
    if ([[[UIDevice currentDevice] systemVersion] floatValue] >= 8.0 ||
        [UIApplication instancesRespondToSelector:@selector(registerUserNotificationSettings:)]) {

        // 定义用户通知类型(Remote.远程 - Badge.标记 Alert.提示 Sound.声音)
        UIUserNotificationType types = UIUserNotificationTypeAlert | UIUserNotificationTypeBadge | UIUserNotificationTypeSound;

        // 定义用户通知设置
        UIUserNotificationSettings *settings = [UIUserNotificationSettings settingsForTypes:types categories:nil];

        // 注册用户通知 - 根据用户通知设置
        [[UIApplication sharedApplication] registerUserNotificationSettings:settings];
    }
}

2.创建一个本地通知

NSInteger time = _textField.text.integerValue;
    UILocalNotification *localNotification = [[UILocalNotification alloc] init];
    if (localNotification) {
        // 使用本地时区
        localNotification.timeZone = [NSTimeZone defaultTimeZone];
        //设置触发通知时间
        localNotification.fireDate = [NSDate dateWithTimeIntervalSinceNow:time];
        //通知参数
        localNotification.userInfo = @{@"type":[NSString stringWithFormat:@"test:%ld",(long)time],@"time":@(time)};;
        //通知声音
        localNotification.soundName = UILocalNotificationDefaultSoundName;
        //通知内容
        localNotification.alertBody = @"test";

        localNotification.repeatInterval = 0;//不重复
        //注册通知
        [[UIApplication sharedApplication] scheduleLocalNotification:localNotification];

    }

3.收到本地通知

  1. 如果应用在前台,会在- (void)application:(UIApplication )application didReceiveLocalNotification:(UILocalNotification )notification 中收到通知。
  2. 如果应用是关闭或者在后台,会在状态栏收到通知。
    通过点击状态栏通知打开应用,在- (BOOL)application:(UIApplication )application didFinishLaunchingWithOptions:(NSDictionary )launchOptions 中,通过
    UILocalNotification *localNotification = [launchOptions objectForKey:UIApplicationLaunchOptionsLocalNotificationKey];

    可以获取本地通知信息。
- (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions {
    // Override point for customization after application launch.

    [self registerUserNotification];
    [[UIApplication sharedApplication] setApplicationIconBadgeNumber:0];

    //本地通知处理
    UILocalNotification *localNotification = [launchOptions objectForKey:UIApplicationLaunchOptionsLocalNotificationKey];
    if ([localNotification isKindOfClass:[UILocalNotification class]]) {
    //---1
        NSLog(@"通知内容1%@",localNotification);
//        [[UIApplication sharedApplication] scheduleLocalNotification:localNotification];
    }

    [self p_showBtnAction];

    return YES;
}

- (void)application:(UIApplication *)application didReceiveLocalNotification:(UILocalNotification *)notification {
    if ([UIApplication sharedApplication].applicationState == UIApplicationStateInactive) {
    //-----2
        NSLog(@"收到本地通知");
    }else if ([UIApplication sharedApplication].applicationState == UIApplicationStateActive){
    //----3
        AudioServicesPlaySystemSound(1007);
    }

    NSString * msg = [[NSString alloc] initWithFormat:@"%@-%lu-%@",notification.userInfo,(unsigned long)notification.repeatInterval,notification.repeatCalendar ];
    UIAlertView * alertView = [[UIAlertView alloc] initWithTitle:@"通知" message:msg delegate:nil cancelButtonTitle:@"确定" otherButtonTitles:nil, nil];
    [alertView show];
    NSLog(@"收到通知:%@",notification);

    NSLog(@"=============");
    [self p_showBtnAction];

}

在1 的地方可以处理本地通知,进行页面跳转等操作;

如果不在1的地方处理,可以调用[[UIApplication sharedApplication] scheduleLocalNotification:localNotification]; 这样didReceiveLocalNotification会被调用。

在2的地方表示点击了状态栏的通知消息,可在此进行操作。

在3的地方添加声音提示,应用在前台时收到本地通知会被调用。

需要注意的地方

  1. for (UILocalNotification* noti in [UIApplication sharedApplication].scheduledLocalNotifications) {
    NSLog(@"通知:%@",noti);
    }

    可遍历所有本地通知。

  2. 如果 localNotification.fireDate = [NSDate dateWithTimeIntervalSinceNow:0];立即通知的,通知不会加入到 [UIApplication sharedApplication].scheduledLocalNotifications数组中。

  3. 走到didReceiveLocalNotification的通知,会自动从[UIApplication sharedApplication].scheduledLocalNotifications数组中删除。
  4. 4.
已标记关键词 清除标记
©️2020 CSDN 皮肤主题: 编程工作室 设计师:CSDN官方博客 返回首页