Skip to content
This repository was archived by the owner on Jul 28, 2022. It is now read-only.

Commit 7f105a9

Browse files
tpucciMinishlink
authored andcommitted
Add setAttribute and trackLocation methods (#4)
* feat(package): Add setAttribute and trackLocation methods * fix(module): Correction after Minishlink review * 🐛 (android) Remove non necessary Altitude, Accuracy and Speed keys to match ios implementation
1 parent 171cd20 commit 7f105a9

File tree

3 files changed

+61
-4
lines changed

3 files changed

+61
-4
lines changed

README.md

Lines changed: 29 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,5 @@
11
# react-native-batch-push
2+
23
> React Native integration of Batch.com push notifications SDK
34
45
## Getting started
@@ -16,11 +17,13 @@ react-native link react-native-batch-push
1617
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.
1718

1819
In your `Podfile`, add:
20+
1921
```
2022
pod 'Batch', '~> 1.10'
2123
```
2224

2325
Then:
26+
2427
```bash
2528
cd ios
2629
pod repo update # optional and can be very long
@@ -45,7 +48,9 @@ defaultConfig {
4548
Note that you can also customize the keys depending on your product flavor or build type.
4649

4750
##### Mobile landings and in-app messaging
51+
4852
If you set a custom `launchMode` in your `AndroidManifest.xml`, add in your `MainActivity.java`:
53+
4954
```java
5055
// import android.content.Intent;
5156
// import com.batch.android.Batch;
@@ -72,6 +77,7 @@ Then, in `Info.plist`, provide:
7277
## Usage
7378

7479
### Enabling push notifications
80+
7581
```js
7682
import BatchPush from 'react-native-batch-push';
7783

@@ -83,13 +89,32 @@ BatchPush.loginUser('theUserId'); // add Platform.OS if you want to target a spe
8389
BatchPush.logoutUser(); // when the user logs out
8490
```
8591

92+
### Custom User Attribute
93+
94+
```js
95+
import BatchPush from 'react-native-batch-push';
96+
97+
// if you want to set a user attribute, use setAttribute (takes two string arguments)
98+
BatchPush.setAttribute('age', '23');
99+
```
100+
101+
### Track User Location
102+
103+
```js
104+
import BatchPush from 'react-native-batch-push';
105+
106+
// if you want to track the user's location
107+
BatchPush.trackLocation({ latitude: 48, longitude: 2.3 });
108+
```
109+
86110
### Inbox
111+
87112
```js
88113
import BatchPush from 'react-native-batch-push';
89114

90115
BatchPush.fetchNewNotifications('theUserId', 'authKey')
91-
.then(notifications => {
92-
// notifications is Array<{ title: string, body: string, timestamp: number, payload: Object }>
93-
})
94-
.catch(e => console.warn('BatchPush error', e));
116+
.then(notifications => {
117+
// notifications is Array<{ title: string, body: string, timestamp: number, payload: Object }>
118+
})
119+
.catch(e => console.warn('BatchPush error', e));
95120
```

android/src/main/java/tech/bam/RNBatchPush/RNBatchPushModule.java

Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,7 @@
55
import android.os.Bundle;
66
import android.support.annotation.NonNull;
77
import android.util.Log;
8+
import android.location.Location;
89

910
import com.batch.android.Batch;
1011
import com.batch.android.BatchInboxFetcher;
@@ -16,6 +17,7 @@
1617
import com.facebook.react.bridge.ReactApplicationContext;
1718
import com.facebook.react.bridge.ReactContextBaseJavaModule;
1819
import com.facebook.react.bridge.ReactMethod;
20+
import com.facebook.react.bridge.ReadableMap;
1921
import com.facebook.react.bridge.WritableArray;
2022
import com.facebook.react.bridge.WritableMap;
2123

@@ -74,6 +76,21 @@ public void logoutUser() {
7476
.save();
7577
}
7678

79+
@ReactMethod
80+
public void setAttribute(String key, String value) {
81+
Batch.User.editor()
82+
.setAttribute(key, value)
83+
.save();
84+
}
85+
86+
@ReactMethod
87+
public void trackLocation(ReadableMap locationMap) {
88+
Location location = new Location("reactNative");
89+
location.setLatitude(locationMap.getDouble("latitude"));
90+
location.setLongitude(locationMap.getDouble("longitude"));
91+
Batch.User.trackLocation(location);
92+
}
93+
7794
@ReactMethod
7895
public void fetchNewNotifications(String userID, String authKey, final Promise promise) {
7996
try {

ios/RNBatchPush.m

Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,4 @@
1+
#import <React/RCTConvert.h>
12
#import "RNBatchPush.h"
23

34
@implementation RNBatchPush
@@ -39,6 +40,20 @@ - (id)init {
3940
[editor save];
4041
}
4142

43+
RCT_EXPORT_METHOD(setAttribute:(NSString*)key value:(NSString*)value)
44+
{
45+
BatchUserDataEditor *editor = [BatchUser editor];
46+
[editor setAttribute:key forKey:value];
47+
[editor save];
48+
}
49+
50+
RCT_EXPORT_METHOD(trackLocation:(NSDictionary*)locationDictionary){
51+
CLLocationDegrees latitude = [RCTConvert double:locationDictionary[@"latitude"]];
52+
CLLocationDegrees longitude = [RCTConvert double:locationDictionary[@"longitude"]];
53+
CLLocation *location = [[CLLocation alloc] initWithLatitude:latitude longitude:longitude];
54+
[BatchUser trackLocation:location];
55+
}
56+
4257
RCT_REMAP_METHOD(fetchNewNotifications,
4358
fetchNewNotificationsWithUserID:(NSString*)userID authKey:(NSString*)authKey resolver:(RCTPromiseResolveBlock)resolve rejecter:(RCTPromiseRejectBlock)reject)
4459
{

0 commit comments

Comments
 (0)