Skip to content

Commit

Permalink
Merge pull request #1696 from dombartenope/addTag-type-fix
Browse files Browse the repository at this point in the history
Add tag type fix
  • Loading branch information
jennantilla authored May 1, 2024
2 parents 8de8c15 + 37a4128 commit b6eb761
Show file tree
Hide file tree
Showing 2 changed files with 23 additions and 3 deletions.
2 changes: 1 addition & 1 deletion ios/RCTOneSignal/RCTOneSignalEventEmitter.m
Original file line number Diff line number Diff line change
Expand Up @@ -352,7 +352,7 @@ + (void)sendEventWithName:(NSString *)name withBody:(NSDictionary *)body {
[OneSignal.User removeSms:smsNumber];
}

RCT_EXPORT_METHOD(addTag:(NSString *)key value:(NSString*)value) {
RCT_EXPORT_METHOD(addTag:(NSString *)key value:(id)value) {
[OneSignal.User addTagWithKey:key value:value];
}

Expand Down
24 changes: 22 additions & 2 deletions src/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -433,11 +433,19 @@ export namespace OneSignal {
export function addTag(key: string, value: string) {
if (!isNativeModuleLoaded(RNOneSignal)) return;

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

// forces values to be string types
if (typeof value !== 'string') {
console.warn(
'OneSignal: addTag: tag value must be of type string; attempting to convert',
);
value = String(value);
}

RNOneSignal.addTag(key, value);
}

Expand All @@ -456,6 +464,18 @@ export namespace OneSignal {
return;
}

const convertedTags = tags as { [key: string]: any };
Object.keys(tags).forEach(function (key) {
if (typeof convertedTags[key] !== 'string') {
console.warn(
'OneSignal: addTags: tag value for key ' +
key +
' must be of type string; attempting to convert',
);
convertedTags[key] = String(convertedTags[key]);
}
});

RNOneSignal.addTags(tags);
}

Expand Down

0 comments on commit b6eb761

Please sign in to comment.