Skip to content

Commit

Permalink
Update tagging methods to pass string value to native bridge
Browse files Browse the repository at this point in the history
  • Loading branch information
jennantilla committed Apr 30, 2024
1 parent c8be2f7 commit 243d2d9
Show file tree
Hide file tree
Showing 2 changed files with 17 additions and 9 deletions.
7 changes: 1 addition & 6 deletions ios/RCTOneSignal/RCTOneSignalEventEmitter.m
Original file line number Diff line number Diff line change
Expand Up @@ -353,14 +353,9 @@ + (void)sendEventWithName:(NSString *)name withBody:(NSDictionary *)body {
}

RCT_EXPORT_METHOD(addTag:(NSString *)key value:(id)value) {
if([value isKindOfClass:[NSNumber class]]) {
//It is a number, convert to a string
value = [value stringValue];
}
[OneSignal.User addTagWithKey:key value:value];
[OneSignal.User addTagWithKey:key value:value];
}


RCT_EXPORT_METHOD(addTags:(NSDictionary *)tags) {
[OneSignal.User addTags:tags];
}
Expand Down
19 changes: 16 additions & 3 deletions src/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -430,15 +430,20 @@ export namespace OneSignal {
* Add a tag 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.
*/
export function addTag(key: string, value: string | number | boolean) {
export function addTag(key: string, value: string) {
if (!isNativeModuleLoaded(RNOneSignal)) return;

if (!key || (!value && value !== '')) {
if (!key || !value) {
console.error('OneSignal: sendTag: must include a key and a value');
return;
}

RNOneSignal.addTag(key, value.toString());
// forces values to be string types
if (typeof value !== 'string') {
value = String(value);
}

RNOneSignal.addTag(key, value);
}

/**
Expand All @@ -456,6 +461,14 @@ export namespace OneSignal {
return;
}

const convertedTags = tags as { [key: string]: any };
Object.keys(tags).forEach(function (key) {
// forces values to be string types
if (typeof convertedTags[key] !== 'string') {
convertedTags[key] = JSON.stringify(convertedTags[key]);
}
});

RNOneSignal.addTags(tags);
}

Expand Down

0 comments on commit 243d2d9

Please sign in to comment.