Skip to content

Commit a037801

Browse files
authored
Merge pull request #398 from Nightsd01/master
• Adds Email to the react-native SDK. • Fixes `setLogLevel()` in Android • Updates the SDK to use a subclass of RCTEventEmitter in the iOS target
2 parents 9690b71 + 8acf1b8 commit a037801

File tree

14 files changed

+976
-309
lines changed

14 files changed

+976
-309
lines changed

README.md

Lines changed: 30 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -335,14 +335,12 @@ export default class App extends Component {
335335
componentWillMount() {
336336
OneSignal.addEventListener('received', this.onReceived);
337337
OneSignal.addEventListener('opened', this.onOpened);
338-
OneSignal.addEventListener('registered', this.onRegistered);
339338
OneSignal.addEventListener('ids', this.onIds);
340339
}
341340

342341
componentWillUnmount() {
343342
OneSignal.removeEventListener('received', this.onReceived);
344343
OneSignal.removeEventListener('opened', this.onOpened);
345-
OneSignal.removeEventListener('registered', this.onRegistered);
346344
OneSignal.removeEventListener('ids', this.onIds);
347345
}
348346

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

360-
onRegistered(notifData) {
361-
console.log("Device had been registered for push notifications!", notifData);
362-
}
363-
364358
onIds(device) {
365359
console.log('Device info: ', device);
366360
}
@@ -435,6 +429,36 @@ Sync hashed email if you have a login system or collect it. Will be used to reac
435429
OneSignal.syncHashedEmail("[email protected]");
436430
```
437431

432+
### Using Email Features
433+
434+
OneSignal now allows you to send emails to your userbase. This email can be set using the OneSignal react-native SDK.
435+
436+
To set the email:
437+
438+
```javascript
439+
let emailAuthCode = ""; //Your email auth code should be securely generated by your backend server
440+
441+
OneSignal.setEmail("[email protected]", emailAuthCode, {(error) => {
442+
//handle error if it occurred
443+
}});
444+
```
445+
446+
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:
447+
448+
```javascript
449+
OneSignal.setEmail("[email protected]", {(error) => {
450+
//handle error if it occurred
451+
}});
452+
```
453+
454+
If your application implements logout functionality, you can logout of the OneSignal email for this user using the logout function:
455+
456+
```javascript
457+
OneSignal.logoutEmail({(error) => {
458+
//handle error if it occurred
459+
}});
460+
```
461+
438462
### Getting Player ID and Push Token
439463

440464
We exposed the idsAvailable API of OneSignal (both Android & iOS) as an event.

android/build.gradle

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,7 @@ buildscript {
55
}
66

77
dependencies {
8-
classpath 'com.android.tools.build:gradle:2.1.0'
8+
classpath 'com.android.tools.build:gradle:2.2.2'
99
}
1010
}
1111

@@ -36,9 +36,9 @@ android {
3636
dependencies {
3737
compile fileTree(include: ['*.jar'], dir: 'libs')
3838
compile 'com.facebook.react:react-native:+'
39-
compile 'com.onesignal:OneSignal:3.+@aar'
4039
compile 'com.google.android.gms:play-services-gcm:+'
4140
compile 'com.google.android.gms:play-services-analytics:+'
4241
compile 'com.google.android.gms:play-services-location:+'
4342
testCompile 'junit:junit:4.12'
43+
compile 'com.onesignal:OneSignal:3.+@aar'
4444
}

android/src/main/java/com/geektime/rnonesignalandroid/RNOneSignal.java

Lines changed: 64 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -24,6 +24,8 @@
2424
import com.onesignal.OSPermissionSubscriptionState;
2525
import com.onesignal.OSSubscriptionState;
2626
import com.onesignal.OneSignal;
27+
import com.onesignal.OneSignal.EmailUpdateHandler;
28+
import com.onesignal.OneSignal.EmailUpdateError;
2729

2830
import org.json.JSONObject;
2931
import org.json.JSONException;
@@ -53,6 +55,7 @@ public RNOneSignal(ReactApplicationContext reactContext) {
5355
// However it seems it is also to soon to call getCurrentActivity() from the reactContext as well.
5456
// This will normally succeed when onHostResume fires instead.
5557
private void initOneSignal() {
58+
5659
Activity activity = getCurrentActivity();
5760
if (activity == null || oneSignalInitDone)
5861
return;
@@ -98,6 +101,66 @@ public void tagsAvailable(JSONObject tags) {
98101
});
99102
}
100103

104+
@ReactMethod
105+
public void setUnauthenticatedEmail(String email, final Callback callback) {
106+
OneSignal.setEmail(email, null, new OneSignal.EmailUpdateHandler() {
107+
@Override
108+
public void onSuccess() {
109+
callback.invoke();
110+
}
111+
112+
@Override
113+
public void onFailure(EmailUpdateError error) {
114+
try {
115+
JSONObject errorObject = new JSONObject("{'error' : '" + error.getMessage() + "'}");
116+
callback.invoke(RNUtils.jsonToWritableMap(errorObject));
117+
} catch (JSONException exception) {
118+
exception.printStackTrace();
119+
}
120+
}
121+
});
122+
}
123+
124+
@ReactMethod
125+
public void setEmail(String email, String emailAuthToken, final Callback callback) {
126+
OneSignal.setEmail(email, emailAuthToken, new EmailUpdateHandler() {
127+
@Override
128+
public void onSuccess() {
129+
callback.invoke();
130+
}
131+
132+
@Override
133+
public void onFailure(EmailUpdateError error) {
134+
try {
135+
JSONObject errorObject = new JSONObject("{'error' : '" + error.getMessage() + "'}");
136+
callback.invoke(RNUtils.jsonToWritableMap(errorObject));
137+
} catch (JSONException exception) {
138+
exception.printStackTrace();
139+
}
140+
}
141+
});
142+
}
143+
144+
@ReactMethod
145+
public void logoutEmail(final Callback callback) {
146+
OneSignal.logoutEmail(new EmailUpdateHandler() {
147+
@Override
148+
public void onSuccess() {
149+
callback.invoke();
150+
}
151+
152+
@Override
153+
public void onFailure(EmailUpdateError error) {
154+
try {
155+
JSONObject errorObject = new JSONObject("{'error' : '" + error.getMessage() + "'}");
156+
callback.invoke(RNUtils.jsonToWritableMap(errorObject));
157+
} catch (JSONException exception) {
158+
exception.printStackTrace();
159+
}
160+
}
161+
});
162+
}
163+
101164
@ReactMethod
102165
public void configure() {
103166
OneSignal.idsAvailable(new OneSignal.IdsAvailableHandler() {
@@ -178,7 +241,7 @@ public void syncHashedEmail(String email) {
178241
}
179242

180243
@ReactMethod
181-
public void setlogLevel(int logLevel, int visualLogLevel) {
244+
public void setLogLevel(int logLevel, int visualLogLevel) {
182245
OneSignal.setLogLevel(logLevel, visualLogLevel);
183246
}
184247

0 commit comments

Comments
 (0)