Skip to content

Commit

Permalink
Merge pull request #398 from Nightsd01/master
Browse files Browse the repository at this point in the history
• Adds Email to the react-native SDK.
• Fixes `setLogLevel()` in Android
• Updates the SDK to use a subclass of RCTEventEmitter in the iOS target
  • Loading branch information
Nightsd01 committed Feb 22, 2018
2 parents 9690b71 + 8acf1b8 commit a037801
Show file tree
Hide file tree
Showing 14 changed files with 976 additions and 309 deletions.
36 changes: 30 additions & 6 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -335,14 +335,12 @@ export default class App extends Component {
componentWillMount() {
OneSignal.addEventListener('received', this.onReceived);
OneSignal.addEventListener('opened', this.onOpened);
OneSignal.addEventListener('registered', this.onRegistered);
OneSignal.addEventListener('ids', this.onIds);
}

componentWillUnmount() {
OneSignal.removeEventListener('received', this.onReceived);
OneSignal.removeEventListener('opened', this.onOpened);
OneSignal.removeEventListener('registered', this.onRegistered);
OneSignal.removeEventListener('ids', this.onIds);
}

Expand All @@ -357,10 +355,6 @@ export default class App extends Component {
console.log('openResult: ', openResult);
}

onRegistered(notifData) {
console.log("Device had been registered for push notifications!", notifData);
}

onIds(device) {
console.log('Device info: ', device);
}
Expand Down Expand Up @@ -435,6 +429,36 @@ Sync hashed email if you have a login system or collect it. Will be used to reac
OneSignal.syncHashedEmail("test@domain.com");
```

### Using Email Features

OneSignal now allows you to send emails to your userbase. This email can be set using the OneSignal react-native SDK.

To set the email:

```javascript
let emailAuthCode = ""; //Your email auth code should be securely generated by your backend server

OneSignal.setEmail("test@test.com", emailAuthCode, {(error) => {
//handle error if it occurred
}});
```

If you don't want to implement email auth hashing on your backend (which is heavily recommended), you can still use the OneSignal email feature in an unauthenticated state like this:

```javascript
OneSignal.setEmail("test@test.com", {(error) => {
//handle error if it occurred
}});
```

If your application implements logout functionality, you can logout of the OneSignal email for this user using the logout function:

```javascript
OneSignal.logoutEmail({(error) => {
//handle error if it occurred
}});
```

### Getting Player ID and Push Token

We exposed the idsAvailable API of OneSignal (both Android & iOS) as an event.
Expand Down
4 changes: 2 additions & 2 deletions android/build.gradle
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@ buildscript {
}

dependencies {
classpath 'com.android.tools.build:gradle:2.1.0'
classpath 'com.android.tools.build:gradle:2.2.2'
}
}

Expand Down Expand Up @@ -36,9 +36,9 @@ android {
dependencies {
compile fileTree(include: ['*.jar'], dir: 'libs')
compile 'com.facebook.react:react-native:+'
compile 'com.onesignal:OneSignal:3.+@aar'
compile 'com.google.android.gms:play-services-gcm:+'
compile 'com.google.android.gms:play-services-analytics:+'
compile 'com.google.android.gms:play-services-location:+'
testCompile 'junit:junit:4.12'
compile 'com.onesignal:OneSignal:3.+@aar'
}
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,8 @@
import com.onesignal.OSPermissionSubscriptionState;
import com.onesignal.OSSubscriptionState;
import com.onesignal.OneSignal;
import com.onesignal.OneSignal.EmailUpdateHandler;
import com.onesignal.OneSignal.EmailUpdateError;

import org.json.JSONObject;
import org.json.JSONException;
Expand Down Expand Up @@ -53,6 +55,7 @@ public RNOneSignal(ReactApplicationContext reactContext) {
// However it seems it is also to soon to call getCurrentActivity() from the reactContext as well.
// This will normally succeed when onHostResume fires instead.
private void initOneSignal() {

Activity activity = getCurrentActivity();
if (activity == null || oneSignalInitDone)
return;
Expand Down Expand Up @@ -98,6 +101,66 @@ public void tagsAvailable(JSONObject tags) {
});
}

@ReactMethod
public void setUnauthenticatedEmail(String email, final Callback callback) {
OneSignal.setEmail(email, null, new OneSignal.EmailUpdateHandler() {
@Override
public void onSuccess() {
callback.invoke();
}

@Override
public void onFailure(EmailUpdateError error) {
try {
JSONObject errorObject = new JSONObject("{'error' : '" + error.getMessage() + "'}");
callback.invoke(RNUtils.jsonToWritableMap(errorObject));
} catch (JSONException exception) {
exception.printStackTrace();
}
}
});
}

@ReactMethod
public void setEmail(String email, String emailAuthToken, final Callback callback) {
OneSignal.setEmail(email, emailAuthToken, new EmailUpdateHandler() {
@Override
public void onSuccess() {
callback.invoke();
}

@Override
public void onFailure(EmailUpdateError error) {
try {
JSONObject errorObject = new JSONObject("{'error' : '" + error.getMessage() + "'}");
callback.invoke(RNUtils.jsonToWritableMap(errorObject));
} catch (JSONException exception) {
exception.printStackTrace();
}
}
});
}

@ReactMethod
public void logoutEmail(final Callback callback) {
OneSignal.logoutEmail(new EmailUpdateHandler() {
@Override
public void onSuccess() {
callback.invoke();
}

@Override
public void onFailure(EmailUpdateError error) {
try {
JSONObject errorObject = new JSONObject("{'error' : '" + error.getMessage() + "'}");
callback.invoke(RNUtils.jsonToWritableMap(errorObject));
} catch (JSONException exception) {
exception.printStackTrace();
}
}
});
}

@ReactMethod
public void configure() {
OneSignal.idsAvailable(new OneSignal.IdsAvailableHandler() {
Expand Down Expand Up @@ -178,7 +241,7 @@ public void syncHashedEmail(String email) {
}

@ReactMethod
public void setlogLevel(int logLevel, int visualLogLevel) {
public void setLogLevel(int logLevel, int visualLogLevel) {
OneSignal.setLogLevel(logLevel, visualLogLevel);
}

Expand Down
Loading

0 comments on commit a037801

Please sign in to comment.