Skip to content

Commit

Permalink
Merge pull request OneSignal#1253 from OneSignal/feat/add-set-language
Browse files Browse the repository at this point in the history
Added methods for setLanguage feature
  • Loading branch information
nan-li authored Jul 27, 2021
2 parents 9095f5a + 6a3671b commit 5b05140
Show file tree
Hide file tree
Showing 5 changed files with 32 additions and 0 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -254,6 +254,11 @@ public void getDeviceState(Promise promise) {
promise.resolve(RNUtils.jsonToWritableMap(state.toJSONObject()));
}

@ReactMethod
public void setLanguage(String language) {
OneSignal.setLanguage(language);
}

@ReactMethod
public void disablePush(boolean disable) {
OneSignal.disablePush(disable);
Expand Down
10 changes: 10 additions & 0 deletions examples/RNOneSignalTS/src/OSButtons.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -135,6 +135,15 @@ class OSButtons extends React.Component<Props, State> {
loggingFunction(`Device State: ${JSON.stringify(deviceState)}`);
})

const setLanguageButton = renderButtonView(
"Set Language",
color,
() => {
loggingFunction('Attempting to set language: ', this.props.inputFieldValue);
OneSignal.setLanguage(this.props.inputFieldValue);
}
);

const requireUserProvideConsent = renderButtonView(
this.state.requireUserConsent ? "Remove User Privacy Consent Requirement" : "Require User Privacy Consent",
color,
Expand Down Expand Up @@ -167,6 +176,7 @@ class OSButtons extends React.Component<Props, State> {

elements.push(
deviceStateButton,
setLanguageButton,
requireUserProvideConsent,
appRequireUserProvideConsent,
provideUserConsentButton,
Expand Down
4 changes: 4 additions & 0 deletions ios/RCTOneSignal/RCTOneSignalEventEmitter.m
Original file line number Diff line number Diff line change
Expand Up @@ -199,6 +199,10 @@ + (void)sendEventWithName:(NSString *)name withBody:(NSDictionary *)body {
}
}

RCT_EXPORT_METHOD(setLanguage:(NSString *)language) {
[OneSignal setLanguage:language];
}

RCT_EXPORT_METHOD(setNotificationOpenedHandler) {
[OneSignal setNotificationOpenedHandler:^(OSNotificationOpenedResult *result) {
[RCTOneSignalEventEmitter sendEventWithName:@"OneSignal-remoteNotificationOpened" withBody:[result jsonRepresentation]];
Expand Down
7 changes: 7 additions & 0 deletions src/index.d.ts
Original file line number Diff line number Diff line change
Expand Up @@ -234,6 +234,13 @@ declare module 'react-native-onesignal' {
*/
getDeviceState(): Promise<DeviceState>;

/**
* Allows you to set the app defined language with the OneSignal SDK.
* @param {string} language
* @returns void
*/
setLanguage(language: string): void;

/**
* Tag a user based on an app event of your choosing so they can be targeted later via segments.
* @param {string} key
Expand Down
6 changes: 6 additions & 0 deletions src/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -160,6 +160,12 @@ export default class OneSignal {
return deviceState;
}

static setLanguage(language) {
if (!isObjectNonNull(RNOneSignal)) return;

RNOneSignal.setLanguage(language);
}

/* T A G S */

static sendTag(key, value) {
Expand Down

0 comments on commit 5b05140

Please sign in to comment.