Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

some updates from community #3

Open
wants to merge 5 commits into
base: updates-and-fixes
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -5,8 +5,11 @@
import android.app.NotificationManager;
import android.content.Context;
import android.content.Intent;
import android.content.pm.ApplicationInfo;
import android.content.pm.PackageManager;
import android.os.Build;
import android.os.Bundle;
import android.util.Log;

import androidx.annotation.Nullable;
import androidx.core.app.NotificationCompat;
Expand All @@ -17,32 +20,68 @@
import com.facebook.react.bridge.Arguments;
import com.facebook.react.jstasks.HeadlessJsTaskConfig;

import java.util.Map;
import java.util.Random;

import static com.eddieowens.RNBoundaryModule.TAG;

public class BoundaryEventHeadlessTaskService extends HeadlessJsTaskService {
public static final String NOTIFICATION_CHANNEL_ID = "com.eddieowens.GEOFENCE_SERVICE_CHANNEL";
private static final String KEY_NOTIFICATION_TITLE = "rnboundary.notification_title";
private static final String KEY_NOTIFICATION_TEXT = "rnboundary.notification_text";
private static final String KEY_NOTIFICATION_ICON = "rnboundary.notification_icon";

@Nullable
protected HeadlessJsTaskConfig getTaskConfig(Intent intent) {
Bundle extras = intent.getExtras();
return new HeadlessJsTaskConfig(
"OnBoundaryEvent",
extras != null ? Arguments.fromBundle(extras) : null,
5000,
true);
"OnBoundaryEvent",
extras != null ? Arguments.fromBundle(extras) : null,
5000,
true);
}

public NotificationCompat.Builder getNotificationBuilder() {
Context context = getApplicationContext();
String title = "Geofencing in progress";
String text = "You're close to the configured location";
int iconResource = -1;

try {
ApplicationInfo ai = context.getPackageManager().getApplicationInfo(context.getPackageName(), PackageManager.GET_META_DATA);
Bundle bundle = ai.metaData;
title = bundle.getString(KEY_NOTIFICATION_TITLE, title);
text = bundle.getString(KEY_NOTIFICATION_TEXT, text);
iconResource = bundle.getInt(KEY_NOTIFICATION_ICON, -1);
} catch (Exception e) {
Log.e(TAG, "Cannot get application Bundle " + e.toString());
}


// Notification for the foreground service
NotificationCompat.Builder builder = new NotificationCompat.Builder(context, NOTIFICATION_CHANNEL_ID)
.setContentTitle(title)
.setContentText(text)
.setOngoing(true)
.setColor(ContextCompat.getColor(context, R.color.accent_material_light));

if (iconResource > -1) {
builder.setSmallIcon(iconResource);
}

return builder;
}

@Override
public void onCreate() {
super.onCreate();

startForegroundServiceNotification();
}

@Override
public int onStartCommand(Intent intent, int flags, int startId) {
int result = super.onStartCommand(intent, flags, startId);

startForegroundServiceNotification();

return result;
}

Expand All @@ -52,15 +91,13 @@ private void startForegroundServiceNotification() {
// Channel for the foreground service notification
createChannel(context);

// Notification for the foreground service
NotificationCompat.Builder builder = new NotificationCompat.Builder(context, NOTIFICATION_CHANNEL_ID)
.setSmallIcon(R.drawable.common_google_signin_btn_icon_light_normal)
.setContentTitle("Geofence Service")
.setContentText("You're close to the configured location.")
.setOngoing(true)
.setColor(ContextCompat.getColor(context, R.color.accent_material_light));
NotificationCompat.Builder builder = getNotificationBuilder();
Notification notification = builder.build();
startForeground(999999999, notification);

Random rand = new Random();
int notificationId = rand.nextInt(100000);

startForeground(notificationId, notification);
HeadlessJsTaskService.acquireWakeLockNow(context);
}

Expand All @@ -79,4 +116,4 @@ private void createChannel(Context context) {
notificationManager.createNotificationChannel(channel);
}
}
}
}
11 changes: 10 additions & 1 deletion ios/RNBoundary.h
Original file line number Diff line number Diff line change
Expand Up @@ -12,5 +12,14 @@
- (bool) removeBoundary:(NSString *)boundaryId;
- (void) removeAllBoundaries;
@property (strong, nonatomic) CLLocationManager *locationManager;
@property (strong, nonatomic) NSMutableSet *queuedEvents;
@property (assign, nonatomic) bool hasListeners;
@end


@interface GeofenceEvent : NSObject
- (id) initWithId:(NSString*)geofenceId forEvent:(NSString *)eventName;
@property (strong, nonatomic) NSString *geofenceId;
@property (strong, nonatomic) NSString *name;
@property (strong, nonatomic) NSDate* date;
@end

69 changes: 65 additions & 4 deletions ios/RNBoundary.m
Original file line number Diff line number Diff line change
@@ -1,6 +1,29 @@

#import "RNBoundary.h"

@implementation GeofenceEvent
- (id)initWithId:(NSString*)geofenceId forEvent:(NSString *)name {
self = [super init];
if (self) {
self.geofenceId = geofenceId;
self.name = name;
self.date = [NSDate date];
}

return self;
}

- (BOOL)isEqual:(id)anObject
{
return [self.geofenceId isEqual:((GeofenceEvent *)anObject).geofenceId];
}

- (NSUInteger)hash
{
return self.geofenceId;
}
@end


@implementation RNBoundary

RCT_EXPORT_MODULE()
Expand All @@ -11,6 +34,8 @@ -(instancetype)init
if (self) {
self.locationManager = [[CLLocationManager alloc] init];
self.locationManager.delegate = self;

self.queuedEvents = [[NSMutableSet alloc] init];
}

return self;
Expand Down Expand Up @@ -62,6 +87,7 @@ - (void) removeAllBoundaries
for(CLRegion *region in [self.locationManager monitoredRegions]) {
[self.locationManager stopMonitoringForRegion:region];
}
[self.queuedEvents removeAllObjects];
}

- (bool) removeBoundary:(NSString *)boundaryId
Expand All @@ -83,13 +109,49 @@ - (bool) removeBoundary:(NSString *)boundaryId
- (void)locationManager:(CLLocationManager *)manager didEnterRegion:(CLRegion *)region
{
NSLog(@"didEnter : %@", region);
[self sendEventWithName:@"onEnter" body:region.identifier];
if (self.hasListeners) {
[self sendEventWithName:@"onEnter" body:region.identifier];
} else {
GeofenceEvent *event = [[GeofenceEvent alloc] initWithId:region.identifier forEvent:@"onEnter" ];
[self.queuedEvents addObject:event];
}
}

- (void)locationManager:(CLLocationManager *)manager didExitRegion:(CLRegion *)region
{
NSLog(@"didExit : %@", region);
[self sendEventWithName:@"onExit" body:region.identifier];
if (self.hasListeners) {
[self sendEventWithName:@"onExit" body:region.identifier];
} else {
GeofenceEvent *event = [[GeofenceEvent alloc] initWithId:region.identifier forEvent:@"onExit" ];
[self.queuedEvents addObject:event];
}
}

- (void)startObserving {
self.hasListeners = YES;
if ([self.queuedEvents count] > 0) {
for(GeofenceEvent *event in self.queuedEvents) {
NSTimeInterval interval = [[NSDate date] timeIntervalSinceDate:[event date]];
double minutesDiff = interval / 60.f;
// if the app was not open
// within 2 minutes of storing the event
// we discard it
if (minutesDiff < 2) {
// dispatch after 1 second
// as both events are not registered at the same time
dispatch_after(dispatch_time(DISPATCH_TIME_NOW, 1 * NSEC_PER_SEC), dispatch_get_main_queue(), ^{
[self sendEventWithName:[event name] body:[event geofenceId]];
});
}
}
[self.queuedEvents removeAllObjects];
}
}

- (void)stopObserving {
self.hasListeners = NO;
[self.queuedEvents removeAllObjects];
}

+ (BOOL)requiresMainQueueSetup
Expand All @@ -98,4 +160,3 @@ + (BOOL)requiresMainQueueSetup
}

@end

4 changes: 2 additions & 2 deletions ios/RNBoundary.podspec
Original file line number Diff line number Diff line change
Expand Up @@ -9,10 +9,10 @@ Pod::Spec.new do |s|
s.license = package['license']

s.authors = package['author']
s.homepage = "https://github.com/woffu/react-native-boundary#readme"
s.homepage = "https://github.com/eddieowens/react-native-boundary#readme"
s.platform = :ios, "9.0"

s.source = { :git => "https://github.com/woffu/react-native-boundary.git", :tag => "#{s.version}" }
s.source = { :git => "https://github.com/eddieowens/react-native-boundary.git", :tag => "#{s.version}" }
s.source_files = "*.{h,m}"
s.requires_arc = true

Expand Down
10 changes: 5 additions & 5 deletions package.json
Original file line number Diff line number Diff line change
@@ -1,14 +1,14 @@
{
"name": "react-native-boundary-woffu",
"version": "1.1.2",
"description": "native geofencing and region monitoring, works with React Native >= 0.60 and both iOS and Android including latest versions (8, 9, 10+).",
"name": "react-native-boundary",
"version": "1.2.0",
"description": "native geofencing and region monitoring",
"main": "index.js",
"scripts": {
"test": "echo \"Error: no test specified\" && exit 1"
},
"repository": {
"type": "git",
"url": "git+https://github.com/woffu/react-native-boundary.git"
"url": "git+https://github.com/eddieowens/react-native-boundary.git"
},
"typings": "typings/index.d.ts",
"keywords": [
Expand All @@ -20,7 +20,7 @@
"location",
"region monitoring"
],
"author": "Woffu -> Forked from Edward Owens",
"author": "Edward Owens",
"license": "Apache-2.0",
"peerDependencies": {
"react-native": ">0.49.0"
Expand Down