Skip to content

Commit

Permalink
Merge pull request #1609 from OneSignal/feature/getTags
Browse files Browse the repository at this point in the history
Add public `getTags` method
  • Loading branch information
jennantilla authored Dec 5, 2023
2 parents 1320e58 + f5d1e27 commit 19d5950
Show file tree
Hide file tree
Showing 7 changed files with 36 additions and 4 deletions.
2 changes: 1 addition & 1 deletion MIGRATION_GUIDE.md
Original file line number Diff line number Diff line change
Expand Up @@ -211,7 +211,7 @@ The User namespace is accessible via `OneSignal.User` and provides access to use
| `OneSignal.User.addTags({"KEY_01": "VALUE_01", "KEY_02": "VALUE_02"})` | _Add multiple tags for the current user. Tags are key:value pairs used as building blocks for targeting specific users and/or personalizing messages. If the tag key already exists, it will be replaced with the value provided here._ |
| `OneSignal.User.removeTag("KEY")` | _Remove the data tag with the provided key from the current user._ |
| `OneSignal.User.removeTags(["KEY_01", "KEY_02"])` | _Remove multiple tags with the provided keys from the current user._ |

| `OneSignal.User.getTags()` | _Returns the local tags for the current user._|
## Push Subscription Namespace

The Push Subscription namespace is accessible via `OneSignal.User.pushSubscription` and provides access to push subscription-scoped functionality.
Expand Down
2 changes: 1 addition & 1 deletion android/build.gradle
Original file line number Diff line number Diff line change
Expand Up @@ -31,7 +31,7 @@ dependencies {

// api is used instead of implementation so the parent :app project can access any of the OneSignal Java
// classes if needed. Such as com.onesignal.NotificationExtenderService
api 'com.onesignal:OneSignal:5.0.4'
api 'com.onesignal:OneSignal:5.0.5'

testImplementation 'junit:junit:4.12'
}
Original file line number Diff line number Diff line change
Expand Up @@ -49,6 +49,8 @@ of this software and associated documentation files (the "Software"), to deal
import com.facebook.react.bridge.ReadableArray;
import com.facebook.react.bridge.ReadableMap;
import com.facebook.react.modules.core.DeviceEventManagerModule;
import com.facebook.react.bridge.WritableMap;
import com.facebook.react.bridge.Arguments;
import com.onesignal.Continue;
import com.onesignal.OneSignal;
import com.onesignal.debug.LogLevel;
Expand All @@ -73,9 +75,9 @@ of this software and associated documentation files (the "Software"), to deal
import com.onesignal.user.subscriptions.PushSubscriptionState;
import com.onesignal.user.subscriptions.PushSubscriptionChangedState;
import org.json.JSONException;
import org.json.JSONObject;

import java.util.HashMap;
import java.util.Map;

public class RNOneSignal extends ReactContextBaseJavaModule implements
IPushSubscriptionObserver,
Expand Down Expand Up @@ -573,6 +575,16 @@ public void removeTags(ReadableArray tagKeys) {
OneSignal.getUser().removeTags(RNUtils.convertReadableArrayIntoStringCollection(tagKeys));
}

@ReactMethod
public void getTags(Promise promise) {
Map<String, String> tags = OneSignal.getUser().getTags();
WritableMap writableTags = Arguments.createMap();
for (Map.Entry<String, String> entry : tags.entrySet()) {
writableTags.putString(entry.getKey(), entry.getValue());
}
promise.resolve(writableTags);
}

@ReactMethod
public void addEmail(String email, Promise promise) {
try {
Expand Down
6 changes: 6 additions & 0 deletions examples/RNOneSignalTS/src/OSButtons.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -276,6 +276,11 @@ class OSButtons extends React.Component<Props> {
OneSignal.User.removeTags(['my_tag1', 'my_tag2']);
});

const getTagsButton = renderButtonView('Get tags', async () => {
const tags = await OneSignal.User.getTags();
loggingFunction('Tags:', tags);
});

const setLanguageButton = renderButtonView('Set Language', () => {
loggingFunction(
'Attempting to set language: ',
Expand Down Expand Up @@ -346,6 +351,7 @@ class OSButtons extends React.Component<Props> {
deleteTagWithKeyButton,
addTagsButton,
removeTagsButton,
getTagsButton,
setLanguageButton,
addSmsButton,
removeSmsButton,
Expand Down
5 changes: 5 additions & 0 deletions ios/RCTOneSignal/RCTOneSignalEventEmitter.m
Original file line number Diff line number Diff line change
Expand Up @@ -357,6 +357,11 @@ + (void)sendEventWithName:(NSString *)name withBody:(NSDictionary *)body {
[OneSignal.User removeTags:keys];
}

RCT_EXPORT_METHOD(getTags:(RCTPromiseResolveBlock)resolve rejecter:(RCTPromiseRejectBlock)reject) {
NSDictionary<NSString *, NSString *> *tags = [OneSignal.User getTags];
resolve(tags);
}

RCT_EXPORT_METHOD(addAlias:(NSString *)label :(NSString *)id) {
[OneSignal.User addAliasWithLabel:label id:id];
}
Expand Down
2 changes: 1 addition & 1 deletion react-native-onesignal.podspec
Original file line number Diff line number Diff line change
Expand Up @@ -22,5 +22,5 @@ Pod::Spec.new do |s|
# pod 'React', :path => '../node_modules/react-native/'

# The Native OneSignal-iOS-SDK XCFramework from cocoapods.
s.dependency 'OneSignalXCFramework', '5.0.4'
s.dependency 'OneSignalXCFramework', '5.0.5'
end
9 changes: 9 additions & 0 deletions src/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -391,6 +391,15 @@ export namespace OneSignal {

RNOneSignal.removeTags(keys);
}

/** Returns the local tags for the current user. */
export function getTags(): Promise<{ [key: string]: string }> {
if (!isNativeModuleLoaded(RNOneSignal)) {
return Promise.reject(new Error('OneSignal native module not loaded'));
}

return RNOneSignal.getTags();
}
}

export namespace Notifications {
Expand Down

0 comments on commit 19d5950

Please sign in to comment.