Skip to content
This repository was archived by the owner on Jul 28, 2022. It is now read-only.
Merged
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
33 changes: 29 additions & 4 deletions README.md
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
# react-native-batch-push

> React Native integration of Batch.com push notifications SDK

## Getting started
Expand All @@ -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
Expand All @@ -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;
Expand All @@ -72,6 +77,7 @@ Then, in `Info.plist`, provide:
## Usage

### Enabling push notifications

```js
import BatchPush from 'react-native-batch-push';

Expand All @@ -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 the 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));
```
17 changes: 17 additions & 0 deletions android/src/main/java/tech/bam/RNBatchPush/RNBatchPushModule.java
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand All @@ -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;

Expand Down Expand Up @@ -74,6 +76,21 @@ 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.setLatitude(locationMap.getDouble("latitude"));
location.setLongitude(locationMap.getDouble("longitude"));
Batch.User.trackLocation(location);
}

@ReactMethod
public void fetchNewNotifications(String userID, String authKey, final Promise promise) {
try {
Expand Down
15 changes: 15 additions & 0 deletions ios/RNBatchPush.m
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
#import <React/RCTConvert.h>
#import "RNBatchPush.h"

@implementation RNBatchPush
Expand Down Expand Up @@ -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)
{
Expand Down