From c5a3f4497f289dc3111eca1254f30771c3d29fc5 Mon Sep 17 00:00:00 2001 From: Thomas Pucci Date: Fri, 5 Jan 2018 11:36:46 +0100 Subject: [PATCH 1/3] feat(package): Add setAttribute and trackLocation methods --- README.md | 33 ++++++++++++++++--- .../bam/RNBatchPush/RNBatchPushModule.java | 20 +++++++++++ ios/RNBatchPush.m | 15 +++++++++ 3 files changed, 64 insertions(+), 4 deletions(-) diff --git a/README.md b/README.md index 78d09e0..54afc70 100644 --- a/README.md +++ b/README.md @@ -1,4 +1,5 @@ # react-native-batch-push + > React Native integration of Batch.com push notifications SDK ## Getting started @@ -16,11 +17,13 @@ react-native link react-native-batch-push If you don't have a Podfile or are unsure on how to proceed, see the [CocoaPods](http://guides.cocoapods.org/using/using-cocoapods.html) usage guide. In your `Podfile`, add: + ``` pod 'Batch', '~> 1.10' ``` Then: + ```bash cd ios pod repo update # optional and can be very long @@ -45,7 +48,9 @@ defaultConfig { Note that you can also customize the keys depending on your product flavor or build type. ##### Mobile landings and in-app messaging + If you set a custom `launchMode` in your `AndroidManifest.xml`, add in your `MainActivity.java`: + ```java // import android.content.Intent; // import com.batch.android.Batch; @@ -72,6 +77,7 @@ Then, in `Info.plist`, provide: ## Usage ### Enabling push notifications + ```js import BatchPush from 'react-native-batch-push'; @@ -83,13 +89,32 @@ BatchPush.loginUser('theUserId'); // add Platform.OS if you want to target a spe BatchPush.logoutUser(); // when the user logs out ``` +### Custom User Attribute + +```js +import BatchPush from 'react-native-batch-push'; + +// if you want to set a user attribute, use setAttribute (takes two string arguments) +BatchPush.setAttribute('age', '23'); +``` + +### Track User Location + +```js +import BatchPush from 'react-native-batch-push'; + +// if you want to track one user's location +BatchPush.trackLocation({ latitude: 48, longitude: 2.3 }); +``` + ### Inbox + ```js import BatchPush from 'react-native-batch-push'; BatchPush.fetchNewNotifications('theUserId', 'authKey') -.then(notifications => { - // notifications is Array<{ title: string, body: string, timestamp: number, payload: Object }> -}) -.catch(e => console.warn('BatchPush error', e)); + .then(notifications => { + // notifications is Array<{ title: string, body: string, timestamp: number, payload: Object }> + }) + .catch(e => console.warn('BatchPush error', e)); ``` diff --git a/android/src/main/java/tech/bam/RNBatchPush/RNBatchPushModule.java b/android/src/main/java/tech/bam/RNBatchPush/RNBatchPushModule.java index 8fcfe19..5791e24 100644 --- a/android/src/main/java/tech/bam/RNBatchPush/RNBatchPushModule.java +++ b/android/src/main/java/tech/bam/RNBatchPush/RNBatchPushModule.java @@ -5,6 +5,7 @@ import android.os.Bundle; import android.support.annotation.NonNull; import android.util.Log; +import android.location.Location; import com.batch.android.Batch; import com.batch.android.BatchInboxFetcher; @@ -16,6 +17,7 @@ import com.facebook.react.bridge.ReactApplicationContext; import com.facebook.react.bridge.ReactContextBaseJavaModule; import com.facebook.react.bridge.ReactMethod; +import com.facebook.react.bridge.ReadableMap; import com.facebook.react.bridge.WritableArray; import com.facebook.react.bridge.WritableMap; @@ -74,6 +76,24 @@ public void logoutUser() { .save(); } + @ReactMethod + public void setAttribute(String key, String value) { + Batch.User.editor() + .setAttribute(key, value) + .save(); + } + + @ReactMethod + public void trackLocation(ReadableMap locationMap) { + Location location = new Location("reactNative"); + location.setAccuracy((float) locationMap.getDouble("accuracy")); + location.setAltitude(locationMap.getDouble("altitude")); + location.setLatitude(locationMap.getDouble("latitude")); + location.setLongitude(locationMap.getDouble("longitude")); + location.setSpeed((float) locationMap.getDouble("speed")); + Batch.User.trackLocation(location); + } + @ReactMethod public void fetchNewNotifications(String userID, String authKey, final Promise promise) { try { diff --git a/ios/RNBatchPush.m b/ios/RNBatchPush.m index edbb03c..9eeed62 100644 --- a/ios/RNBatchPush.m +++ b/ios/RNBatchPush.m @@ -1,4 +1,5 @@ #import "RNBatchPush.h" +#import @implementation RNBatchPush @@ -39,6 +40,20 @@ - (id)init { [editor save]; } +RCT_EXPORT_METHOD(setAttribute:(NSString*)key value:(NSString*)value) +{ + BatchUserDataEditor *editor = [BatchUser editor]; + [editor setAttribute:key forKey:value]; + [editor save]; +} + +RCT_EXPORT_METHOD(trackLocation:(NSDictionary*)locationDictionary){ + CLLocationDegrees latitude = [RCTConvert double:locationDictionary[@"latitude"]]; + CLLocationDegrees longitude = [RCTConvert double:locationDictionary[@"longitude"]]; + CLLocation *location = [[CLLocation alloc] initWithLatitude:latitude longitude:longitude]; + [BatchUser trackLocation:location]; +} + RCT_REMAP_METHOD(fetchNewNotifications, fetchNewNotificationsWithUserID:(NSString*)userID authKey:(NSString*)authKey resolver:(RCTPromiseResolveBlock)resolve rejecter:(RCTPromiseRejectBlock)reject) { From 75cec8f41f65822a5e21c3fa992e6070b1a00ffb Mon Sep 17 00:00:00 2001 From: Thomas Pucci Date: Wed, 10 Jan 2018 14:18:23 +0100 Subject: [PATCH 2/3] fix(module): Correction after Minishlink review --- README.md | 2 +- ios/RNBatchPush.m | 2 +- 2 files changed, 2 insertions(+), 2 deletions(-) diff --git a/README.md b/README.md index 54afc70..6bb4a63 100644 --- a/README.md +++ b/README.md @@ -103,7 +103,7 @@ BatchPush.setAttribute('age', '23'); ```js import BatchPush from 'react-native-batch-push'; -// if you want to track one user's location +// if you want to track the user's location BatchPush.trackLocation({ latitude: 48, longitude: 2.3 }); ``` diff --git a/ios/RNBatchPush.m b/ios/RNBatchPush.m index 9eeed62..ac20a0b 100644 --- a/ios/RNBatchPush.m +++ b/ios/RNBatchPush.m @@ -1,5 +1,5 @@ -#import "RNBatchPush.h" #import +#import "RNBatchPush.h" @implementation RNBatchPush From bcb956396482fbc597313b2b554b8967f2db60ed Mon Sep 17 00:00:00 2001 From: Thomas Pucci Date: Thu, 18 Jan 2018 09:55:15 +0100 Subject: [PATCH 3/3] :bug: (android) Remove non necessary Altitude, Accuracy and Speed keys to match ios implementation --- .../src/main/java/tech/bam/RNBatchPush/RNBatchPushModule.java | 3 --- 1 file changed, 3 deletions(-) diff --git a/android/src/main/java/tech/bam/RNBatchPush/RNBatchPushModule.java b/android/src/main/java/tech/bam/RNBatchPush/RNBatchPushModule.java index 5791e24..b5d22f7 100644 --- a/android/src/main/java/tech/bam/RNBatchPush/RNBatchPushModule.java +++ b/android/src/main/java/tech/bam/RNBatchPush/RNBatchPushModule.java @@ -86,11 +86,8 @@ public void setAttribute(String key, String value) { @ReactMethod public void trackLocation(ReadableMap locationMap) { Location location = new Location("reactNative"); - location.setAccuracy((float) locationMap.getDouble("accuracy")); - location.setAltitude(locationMap.getDouble("altitude")); location.setLatitude(locationMap.getDouble("latitude")); location.setLongitude(locationMap.getDouble("longitude")); - location.setSpeed((float) locationMap.getDouble("speed")); Batch.User.trackLocation(location); }