|
|
@@ -9,10 +9,13 @@
|
|
|
#import <UserNotifications/UserNotifications.h>
|
|
|
#import "LoginViewController.h"
|
|
|
#import "RAHomeViewController.h"
|
|
|
+#import <CoreLocation/CoreLocation.h>
|
|
|
|
|
|
-@interface AppDelegate ()<UNUserNotificationCenterDelegate>
|
|
|
+@interface AppDelegate ()<UNUserNotificationCenterDelegate,CLLocationManagerDelegate>
|
|
|
|
|
|
@property (nonatomic,weak) UINavigationController *rootVC;
|
|
|
+@property (nonatomic,strong) CLLocationManager *locationManager;
|
|
|
+@property (nonatomic,strong) CLLocation *currentLocation;
|
|
|
|
|
|
@end
|
|
|
|
|
|
@@ -37,6 +40,38 @@
|
|
|
}
|
|
|
}
|
|
|
|
|
|
+- (void)startLocation {
|
|
|
+
|
|
|
+ if (self.locationManager) {
|
|
|
+ return;
|
|
|
+ }
|
|
|
+ self.locationManager = [[CLLocationManager alloc] init];
|
|
|
+ self.locationManager.delegate = self;
|
|
|
+ self.locationManager.allowsBackgroundLocationUpdates = YES;
|
|
|
+ self.locationManager.pausesLocationUpdatesAutomatically = NO; // 是否允许系统自动暂停定位
|
|
|
+ self.locationManager.desiredAccuracy = kCLLocationAccuracyBestForNavigation;//设置定位精度
|
|
|
+ self.locationManager.distanceFilter = kCLDistanceFilterNone; //不需要移动都可以刷新,不然不移动就不会执行定位,不定位的话,那么后台进程也就挂起了
|
|
|
+ [self.locationManager requestAlwaysAuthorization];
|
|
|
+ [self.locationManager startUpdatingLocation];
|
|
|
+}
|
|
|
+
|
|
|
+- (void)stopLocation {
|
|
|
+ if (self.locationManager == nil) {
|
|
|
+ return;
|
|
|
+ }
|
|
|
+ [self.locationManager stopUpdatingLocation];
|
|
|
+ self.locationManager = nil;
|
|
|
+}
|
|
|
+
|
|
|
+- (void)reportLocation:(CLLocation *)location {
|
|
|
+
|
|
|
+ if (location) {
|
|
|
+
|
|
|
+ } else {
|
|
|
+
|
|
|
+ }
|
|
|
+}
|
|
|
+
|
|
|
#pragma mark - AppDelegate
|
|
|
|
|
|
- (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions {
|
|
|
@@ -59,15 +94,17 @@
|
|
|
|
|
|
[self.window makeKeyAndVisible];
|
|
|
|
|
|
+ [self startLocation];
|
|
|
+
|
|
|
//消息推送注册
|
|
|
UNUserNotificationCenter * center = [UNUserNotificationCenter currentNotificationCenter];
|
|
|
[center setDelegate:self];
|
|
|
UNAuthorizationOptions type = UNAuthorizationOptionBadge|UNAuthorizationOptionSound|UNAuthorizationOptionAlert;
|
|
|
[center requestAuthorizationWithOptions:type completionHandler:^(BOOL granted, NSError * _Nullable error) {
|
|
|
if (granted) {
|
|
|
- NSLog(@"注册成功");
|
|
|
+ NSLog(@"推送通知授权成功");
|
|
|
}else{
|
|
|
- NSLog(@"注册失败");
|
|
|
+ NSLog(@"推送通知授权失败");
|
|
|
}
|
|
|
}];
|
|
|
[application registerForRemoteNotifications];
|
|
|
@@ -104,29 +141,98 @@
|
|
|
|
|
|
//处理通知
|
|
|
- (void)userNotificationCenter:(UNUserNotificationCenter *)center willPresentNotification:(UNNotification *)notification withCompletionHandler:(void (^)(UNNotificationPresentationOptions))completionHandler {
|
|
|
-
|
|
|
+
|
|
|
+ // 程序在前台收到通知
|
|
|
UNNotificationRequest *request = notification.request;
|
|
|
NSDictionary *userInfo = request.content.userInfo;
|
|
|
|
|
|
if ([request.trigger isKindOfClass:[UNPushNotificationTrigger class]]) {
|
|
|
- NSLog(@"收到了一个远程推送:%@",userInfo);
|
|
|
- }
|
|
|
- else{
|
|
|
- NSLog(@"收到了一个本地推送:%@",userInfo);
|
|
|
+ NSLog(@"收到远程通知: %@",userInfo);
|
|
|
+ } else {
|
|
|
+ NSLog(@"收到本地通知: %@",userInfo);
|
|
|
}
|
|
|
|
|
|
- completionHandler(UNNotificationPresentationOptionBadge|UNNotificationPresentationOptionSound|UNNotificationPresentationOptionAlert);
|
|
|
+ // UNNotificationPresentationOptionBadge|UNNotificationPresentationOptionSound|UNNotificationPresentationOptionAlert
|
|
|
+ completionHandler(UNNotificationPresentationOptionNone);
|
|
|
}
|
|
|
|
|
|
- (void)userNotificationCenter:(UNUserNotificationCenter *)center didReceiveNotificationResponse:(UNNotificationResponse *)response withCompletionHandler:(void (^)(void))completionHandler {
|
|
|
NSLog(@"接收到推送内容==%@", response.notification.request.content.userInfo);
|
|
|
-
|
|
|
+ // 点击通知
|
|
|
completionHandler();
|
|
|
}
|
|
|
|
|
|
- (void)application:(UIApplication *)application didReceiveRemoteNotification:(NSDictionary *)userInfo fetchCompletionHandler:(void (^)(UIBackgroundFetchResult))completionHandler {
|
|
|
|
|
|
+ /**
|
|
|
+ 静默推送alert为空
|
|
|
+ {
|
|
|
+ "aps" : {
|
|
|
+ "alert" : "",
|
|
|
+ "content-available" : 1
|
|
|
+ }
|
|
|
+ }
|
|
|
+ */
|
|
|
+
|
|
|
+ // "content-available" : 1 收到通知
|
|
|
NSLog(@"收到静默推送: %@",userInfo);
|
|
|
- completionHandler(UIBackgroundFetchResultNewData);
|
|
|
+ if (userInfo) {
|
|
|
+
|
|
|
+ NSDictionary *aps = [userInfo objectForKey:@"aps"];
|
|
|
+ NSInteger report_location = [[aps objectForKey:@"report-location"] integerValue];
|
|
|
+ if (report_location == 1) {
|
|
|
+
|
|
|
+ [self reportLocation:self.currentLocation];
|
|
|
+ completionHandler(UIBackgroundFetchResultNewData);
|
|
|
+ } else {
|
|
|
+ /**
|
|
|
+ {
|
|
|
+ "aps" : {
|
|
|
+ "alert" : {
|
|
|
+ "title" : "Notification",
|
|
|
+ "body" : "You have a new order"
|
|
|
+ },
|
|
|
+ "new-order" : 1,
|
|
|
+ "sound" : "default",
|
|
|
+ "content-available" : 1
|
|
|
+ }
|
|
|
+ }
|
|
|
+ */
|
|
|
+
|
|
|
+ NSInteger new_order = [[aps objectForKey:@"new-order"] integerValue];
|
|
|
+ if (new_order) {
|
|
|
+
|
|
|
+ [[NSNotificationCenter defaultCenter] postNotificationName:RANotificationNewOrder object:nil];
|
|
|
+ }
|
|
|
+ completionHandler(UIBackgroundFetchResultNewData);
|
|
|
+ }
|
|
|
+ } else {
|
|
|
+ completionHandler(UIBackgroundFetchResultNoData);
|
|
|
+ }
|
|
|
+}
|
|
|
+
|
|
|
+#pragma mark - LocationManager Delegate
|
|
|
+
|
|
|
+- (void)locationManager:(CLLocationManager *)manager didUpdateLocations:(NSArray<CLLocation *> *)locations {
|
|
|
+ if (locations.count) {
|
|
|
+ self.currentLocation = [locations lastObject];
|
|
|
+
|
|
|
+ NSLog(@"location: %f, %f",self.currentLocation.coordinate.latitude,self.currentLocation.coordinate.longitude);
|
|
|
+
|
|
|
+ // 省电,停止不能超过三分钟
|
|
|
+// [self performSelector:@selector(stopLocation) withObject:nil afterDelay:10]; // 获取到位置10s后关闭位置服务
|
|
|
+// [self performSelector:@selector(startLocation) withObject:nil afterDelay:120]; // 获取到位置120s后重新打开位置服务
|
|
|
+ CLLocationDistance distance = 500;
|
|
|
+ NSTimeInterval time = 60;
|
|
|
+ [manager allowDeferredLocationUpdatesUntilTraveled:distance timeout:time];
|
|
|
+ }
|
|
|
+}
|
|
|
+
|
|
|
+- (void)locationManager:(CLLocationManager *)manager didChangeAuthorizationStatus:(CLAuthorizationStatus)status {
|
|
|
+
|
|
|
+ if (status == kCLAuthorizationStatusDenied) {
|
|
|
+ self.currentLocation = nil;
|
|
|
+ }
|
|
|
}
|
|
|
+
|
|
|
@end
|