收到通知后,是否可以获取当前位置?

时间:2019-03-29 18:27:50

标签: objective-c push-notification cllocationmanager unnotificationserviceextension

当我收到推送通知时,iOS中是否可以获取我的当前位置?

就像,在o收到oneSignal推送和其他东西时触发它...

所以我已经尝试了:

OneSignal通知服务内部的CLLocationManager OneSignal Notification Service中的react-native-background-geolocation getCurrentPosition(本机方法)。

NotificationService.c类:

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

#import "NotificationService.h"

@interface NotificationService ()

@property (nonatomic, strong) void (^contentHandler)(UNNotificationContent *contentToDeliver);
@property (nonatomic, strong) UNNotificationRequest *receivedRequest;
@property (nonatomic, strong) UNMutableNotificationContent *bestAttemptContent;
//@property (nonatomic, strong) TSLocationManager *locationManager;
//@property (nonatomic, strong) TSCurrentPositionRequest *cpr;

@end

@implementation NotificationService


- (void)didReceiveNotificationRequest:(UNNotificationRequest *)request withContentHandler:(void (^)(UNNotificationContent * _Nonnull))contentHandler {
  self.receivedRequest = request;
  self.contentHandler = contentHandler;
  self.bestAttemptContent = [request.content mutableCopy];

  [OneSignal didReceiveNotificationExtensionRequest:self.receivedRequest withMutableNotificationContent:self.bestAttemptContent];
  self.contentHandler(self.bestAttemptContent);

  // DEBUGGING: Uncomment the 2 lines below and comment out the one above to ensure this extension is excuting
  //            Note, this extension only runs when mutable-content is set
  //            Setting an attachment or action buttons automatically adds this
  //NSLog(@"Running NotificationServiceExtension");
 // self.bestAttemptContent.body = [@"[Modified] " stringByAppendingString:self.bestAttemptContent.body];

  // Magic
  NSLog(@"get current position");
  locationManager = [[CLLocationManager alloc] init];
  locationManager.delegate = self;
  locationManager.distanceFilter = kCLDistanceFilterNone;
  locationManager.desiredAccuracy = kCLLocationAccuracyBest;

  if ([[[UIDevice currentDevice] systemVersion] floatValue] >= 8.0)
    [locationManager requestWhenInUseAuthorization];

  [locationManager startUpdatingLocation];

  [locationManager stopUpdatingLocation];

//  _locationManager = [TSLocationManager sharedInstance];
//  _cpr = [[TSCurrentPositionRequest alloc] initWithSuccess:^(TSLocation *location) {
//  } failure:^(NSError *error) {}];
//  [self.locationManager getCurrentPosition:self.cpr];



}

- (void)serviceExtensionTimeWillExpire {
  // Called just before the extension will be terminated by the system.
  // Use this as an opportunity to deliver your "best attempt" at modified content, otherwise the original push payload will be used.

  [OneSignal serviceExtensionTimeWillExpireRequest:self.receivedRequest withMutableNotificationContent:self.bestAttemptContent];

  self.contentHandler(self.bestAttemptContent);
}

- (void)locationManager:(CLLocationManager *)manager didUpdateToLocation:(NSArray *)locations {
  NSLog([locations lastObject]);
  NSLog(@"Rapaz, tem uma localizacao aqui");
}




@end

NotificationService.h类:

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

    #if __has_include("RCTEventEmitter.h")
    #import "RCTEventEmitter.h"
    #else
    #import <React/RCTEventEmitter.h>
    #endif

    #if __has_include("RCTInvalidating.h")
    #import "RCTInvalidating.h"
    #else
    #import <React/RCTInvalidating.h>
    #endif

    @interface NotificationService : UNNotificationServiceExtension <CLLocationManagerDelegate> {
      CLLocationManager *locationManager;
    }

    @end

对Objective-C不太满意,但我知道C。预先感谢。

0 个答案:

没有答案