ios - GPS位置的本地推送通知

标签 ios gps push-notification core-location

我有一个关于iOS,GPS位置和推送通知的简单问题。

当设备靠近特定的GPS位置时,iOS中是否可以发送本地推送通知?

最佳答案

以下ViewController.h文件的代码

#import <UIKit/UIKit.h>
#import <CoreLocation/CoreLocation.h>

@interface ViewController : UIViewController <CLLocationManagerDelegate>{
    CLLocationManager *locationManager;
    NSString* lastNotification;
}

@end

以下ViewController.m文件的代码
#import "ViewController.h"

@interface ViewController ()

@end

@implementation ViewController

- (void)viewDidLoad
{
    [super viewDidLoad];
    lastNotification = @"";
    // Do any additional setup after loading the view, typically from a nib.
}

- (void)viewDidAppear:(BOOL)animated{
    [super viewDidAppear:animated];
}

- (void)didReceiveMemoryWarning
{
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}

#pragma mark -User Actions
- (IBAction)startLocaingMe:(id)sender{
    [self startLocationReporting];
}

#pragma mark - Location Metods

- (void)startLocationReporting {
    locationManager = [[CLLocationManager alloc] init];
    locationManager.delegate = self;//or whatever class you have for managing location
    locationManager.desiredAccuracy = kCLLocationAccuracyNearestTenMeters;
    [locationManager startUpdatingLocation];
}

// Delegate method from the CLLocationManagerDelegate protocol.
- (void)locationManager:(CLLocationManager *)manager didUpdateLocations:(NSArray *)locations {
    // If it's a relatively recent event, turn off updates to save power
    CLLocation* location = [locations lastObject];
    NSDate* eventDate = location.timestamp;
    NSTimeInterval howRecent = [eventDate timeIntervalSinceNow];
    if (abs(howRecent) < 15.0) {
        [self showNotificationIfDistanceIs100:location];
    }
}

// this delegate method is called if an error occurs in locating your current location
- (void)locationManager:(CLLocationManager *)manager didFailWithError:(NSError *)error
{
    NSLog(@"locationManager:%@ didFailWithError:%@", manager, error);
}

- (CLLocationDistance)distanceBetweenTwoPoints:(CLLocation*) location1 andSecond:(CLLocation*) location2{
    CLLocationDistance distance = [location1 distanceFromLocation:location2];
    return distance;
}

-(CLLocation*)House{
    CLLocation *loc = [[CLLocation alloc] initWithLatitude:23.030064 longitude:72.546193];
    return loc;
}

-(CLLocation*)passportOffice{
    CLLocation *loc = [[CLLocation alloc] initWithLatitude:23.032034 longitude:72.549999];
    return loc;
}

-(CLLocation*)ldCollageBusStand{
    CLLocation *loc = [[CLLocation alloc] initWithLatitude:23.032515 longitude:72.549307];
    return loc;
}

-(void)showNotificationIfDistanceIs100:(CLLocation*) location{
    if ([self distanceBetweenTwoPoints:location andSecond:[self House]] <= 100) {
        [self setLocalNotificaion:@"Your are 100 meter form House"];
    }else if ([self distanceBetweenTwoPoints:location andSecond:[self passportOffice]] <= 100){
        [self setLocalNotificaion:@"Your are 100 meter form Passport Office"];
    }else if ([self distanceBetweenTwoPoints:location andSecond:[self ldCollageBusStand]] <= 100){
        [self setLocalNotificaion:@"Your are 100 meter form Ld Collage Bus Stand"];
    }

}

-(void)setLocalNotificaion:(NSString*)msg{
    if ([lastNotification isEqualToString:msg] != YES) {
        lastNotification = msg;
        UILocalNotification *futureAlert;
        futureAlert = [[UILocalNotification alloc] init];
        [futureAlert setAlertBody:msg];
        futureAlert.fireDate = [NSDate dateWithTimeIntervalSinceNow:0];
        futureAlert.timeZone = [NSTimeZone defaultTimeZone];
        [[UIApplication sharedApplication] scheduleLocalNotification:futureAlert];
    }
}

@end

您可以更改Long / Lati的位置并获取Localnotification

可能对您有所帮助

-------编辑---------

关于ios - GPS位置的本地推送通知,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/22322828/

相关文章:

ios - Swift - UIButton 改变宽度

java - Android locationManager 空指针异常? (如何传递上下文?)

css - 在具有 GPS 和/或定位功能的设备上显示或隐藏 div

android - Android 中的 Firebase 通知

ios - 在iOS中的推送通知中振动取决于客户端或有效负载

ios - 使用新的 iOS 11 API 重新排序 tableview 时动画不正确

ios - 可以不在自定义 init 方法中调用 [super init] 吗?

ios - iOS 中不区分大小写的本地化

java - 安卓位置错误

javascript - Web 推送通知的权限更改是否有回调?