Skip to content

Commit 19d5950

Browse files
authored
Merge pull request #1609 from OneSignal/feature/getTags
Add public `getTags` method
2 parents 1320e58 + f5d1e27 commit 19d5950

File tree

7 files changed

+36
-4
lines changed

7 files changed

+36
-4
lines changed

MIGRATION_GUIDE.md

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -211,7 +211,7 @@ The User namespace is accessible via `OneSignal.User` and provides access to use
211211
| `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._ |
212212
| `OneSignal.User.removeTag("KEY")` | _Remove the data tag with the provided key from the current user._ |
213213
| `OneSignal.User.removeTags(["KEY_01", "KEY_02"])` | _Remove multiple tags with the provided keys from the current user._ |
214-
214+
| `OneSignal.User.getTags()` | _Returns the local tags for the current user._|
215215
## Push Subscription Namespace
216216

217217
The Push Subscription namespace is accessible via `OneSignal.User.pushSubscription` and provides access to push subscription-scoped functionality.

android/build.gradle

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -31,7 +31,7 @@ dependencies {
3131

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

3636
testImplementation 'junit:junit:4.12'
3737
}

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

Lines changed: 13 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -49,6 +49,8 @@ of this software and associated documentation files (the "Software"), to deal
4949
import com.facebook.react.bridge.ReadableArray;
5050
import com.facebook.react.bridge.ReadableMap;
5151
import com.facebook.react.modules.core.DeviceEventManagerModule;
52+
import com.facebook.react.bridge.WritableMap;
53+
import com.facebook.react.bridge.Arguments;
5254
import com.onesignal.Continue;
5355
import com.onesignal.OneSignal;
5456
import com.onesignal.debug.LogLevel;
@@ -73,9 +75,9 @@ of this software and associated documentation files (the "Software"), to deal
7375
import com.onesignal.user.subscriptions.PushSubscriptionState;
7476
import com.onesignal.user.subscriptions.PushSubscriptionChangedState;
7577
import org.json.JSONException;
76-
import org.json.JSONObject;
7778

7879
import java.util.HashMap;
80+
import java.util.Map;
7981

8082
public class RNOneSignal extends ReactContextBaseJavaModule implements
8183
IPushSubscriptionObserver,
@@ -573,6 +575,16 @@ public void removeTags(ReadableArray tagKeys) {
573575
OneSignal.getUser().removeTags(RNUtils.convertReadableArrayIntoStringCollection(tagKeys));
574576
}
575577

578+
@ReactMethod
579+
public void getTags(Promise promise) {
580+
Map<String, String> tags = OneSignal.getUser().getTags();
581+
WritableMap writableTags = Arguments.createMap();
582+
for (Map.Entry<String, String> entry : tags.entrySet()) {
583+
writableTags.putString(entry.getKey(), entry.getValue());
584+
}
585+
promise.resolve(writableTags);
586+
}
587+
576588
@ReactMethod
577589
public void addEmail(String email, Promise promise) {
578590
try {

examples/RNOneSignalTS/src/OSButtons.tsx

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -276,6 +276,11 @@ class OSButtons extends React.Component<Props> {
276276
OneSignal.User.removeTags(['my_tag1', 'my_tag2']);
277277
});
278278

279+
const getTagsButton = renderButtonView('Get tags', async () => {
280+
const tags = await OneSignal.User.getTags();
281+
loggingFunction('Tags:', tags);
282+
});
283+
279284
const setLanguageButton = renderButtonView('Set Language', () => {
280285
loggingFunction(
281286
'Attempting to set language: ',
@@ -346,6 +351,7 @@ class OSButtons extends React.Component<Props> {
346351
deleteTagWithKeyButton,
347352
addTagsButton,
348353
removeTagsButton,
354+
getTagsButton,
349355
setLanguageButton,
350356
addSmsButton,
351357
removeSmsButton,

ios/RCTOneSignal/RCTOneSignalEventEmitter.m

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -357,6 +357,11 @@ + (void)sendEventWithName:(NSString *)name withBody:(NSDictionary *)body {
357357
[OneSignal.User removeTags:keys];
358358
}
359359

360+
RCT_EXPORT_METHOD(getTags:(RCTPromiseResolveBlock)resolve rejecter:(RCTPromiseRejectBlock)reject) {
361+
NSDictionary<NSString *, NSString *> *tags = [OneSignal.User getTags];
362+
resolve(tags);
363+
}
364+
360365
RCT_EXPORT_METHOD(addAlias:(NSString *)label :(NSString *)id) {
361366
[OneSignal.User addAliasWithLabel:label id:id];
362367
}

react-native-onesignal.podspec

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -22,5 +22,5 @@ Pod::Spec.new do |s|
2222
# pod 'React', :path => '../node_modules/react-native/'
2323

2424
# The Native OneSignal-iOS-SDK XCFramework from cocoapods.
25-
s.dependency 'OneSignalXCFramework', '5.0.4'
25+
s.dependency 'OneSignalXCFramework', '5.0.5'
2626
end

src/index.ts

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -391,6 +391,15 @@ export namespace OneSignal {
391391

392392
RNOneSignal.removeTags(keys);
393393
}
394+
395+
/** Returns the local tags for the current user. */
396+
export function getTags(): Promise<{ [key: string]: string }> {
397+
if (!isNativeModuleLoaded(RNOneSignal)) {
398+
return Promise.reject(new Error('OneSignal native module not loaded'));
399+
}
400+
401+
return RNOneSignal.getTags();
402+
}
394403
}
395404

396405
export namespace Notifications {

0 commit comments

Comments
 (0)