Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add public getTags method #1609

Merged
merged 5 commits into from
Dec 5, 2023
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
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
Loading