首页 > 解决方案 > 当我收到通知时,有没有办法获取当前位置?

问题描述

当我收到推送通知时,iOS 中有没有办法获取我的当前位置?

就像,当收到一个信号推送和东西时触发它......

所以我已经尝试过:

OneSignal 通知服务中的 CLLocationManager OneSignal 通知服务中的 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。在此先感谢。

标签: objective-cpush-notificationcllocationmanagerunnotificationserviceextension

解决方案


推荐阅读