实用知识:基于监听位置的本地通知 使用方法

#import "AppDelegate.h"
#import <CoreLocation/CoreLocation.h>

@interface AppDelegate () <CLLocationManagerDelegate>

@property (strong, nonatomic) CLLocationManager *manager;

@end

@implementation AppDelegate


- (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions {

    /*================= 通知权限 =================*/
    UIUserNotificationType type = UIUserNotificationTypeBadge | UIUserNotificationTypeSound | UIUserNotificationTypeAlert;

    // UIUserNotificationSettings 表示App可以使用的通知的展示类型(声音, 边缘数字, 弹窗)
    // category 是用来配置 按钮的
    UIUserNotificationSettings *settings = [UIUserNotificationSettings settingsForTypes:type categories:nil];

    // 注册用户通知的配置(通知使用的类型), (会弹出授权窗口来请求)
    [[UIApplication sharedApplication] registerUserNotificationSettings:settings];

    /*================= 定位权限 =================*/
    _manager = [[CLLocationManager alloc] init];
    _manager.delegate = self;

//    [_manager requestWhenInUseAuthorization];
    [_manager requestAlwaysAuthorization];

    return YES;
}

#pragma mark - CLLocationManagerDelegate

//- (void)locationManager:(CLLocationManager *)manager didChangeAuthorizationStatus:(CLAuthorizationStatus)status
//{
//    
//}

//- (void)locationManager:(CLLocationManager *)manager didFailWithError:(NSError *)error
//{
//    
//}

@end
#import "ViewController.h"
#import <CoreLocation/CoreLocation.h>

@interface ViewController ()

@end

@implementation ViewController

- (void)touchesBegan:(NSSet<UITouch *> *)touches withEvent:(UIEvent *)event
{
    NSLog(@"点击了");

    /*================= 召召路过云水瑶, 得到通知提醒 =================*/

    // 1. 基于位置触发的本地通知
    UILocalNotification *notification = [[UILocalNotification alloc] init];

    // 2. 参数
//    notification.fireDate
    notification.alertBody = @"欢迎召召, 请上3楼!";

    // 触发通知的区域 (区域监听)   // When-In-Use与Always都可以使用
    // 默认只执行一次
    notification.region = [[CLCircularRegion alloc] initWithCenter:CLLocationCoordinate2DMake(23.133916, 113.395557) radius:500 identifier:@"YunShuiYao"];
    // 基于区域的通知是否只执行一次
    notification.regionTriggersOnce = NO;

    // 3.添加到调度池中等待调度
    [[UIApplication sharedApplication] scheduleLocalNotification:notification];
}

@end
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值