objective-c - 当我收到通知时,有没有办法获取当前位置?
问题描述
当我收到推送通知时,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。在此先感谢。
解决方案
推荐阅读
- chronicle - 追加回调时如何正确关闭编年史资源?
- netsuite - RANK 或 ROW_NUMBER 能否用于汇总的 Netsuite 搜索结果?
- rest - 如何在发布定义管道中创建/更新环境?
- c# - 单元测试:使用谓词对列表执行 FindAll 时出现空引用异常
- excel - 在 Excel 中查找部分字符串之间的匹配项
- parsing - 解析 802.11 动作无 ack 帧
- python - matplotlib,在另一个子图之上放置一个透明子图
- javascript - 完成所有函数语句后的打字稿调用语句
- bootstrap-4 - 我们如何在 Bootstrap 4 中创建一个可滚动的列,并且内容保留在视口中?
- c# - 如何删除卸载安装程序后创建的文件夹?