Skip to content

Commit

Permalink
Merge pull request #66 from emartech/MV-303-fix-setpushtoken
Browse files Browse the repository at this point in the history
MV-303 fix setPushToken
  • Loading branch information
eduzatoni authored Jul 5, 2022
2 parents 0604c60 + 3d7f1d4 commit 0002743
Showing 1 changed file with 18 additions and 3 deletions.
21 changes: 18 additions & 3 deletions ios/RNEmarsysWrapper.m
Original file line number Diff line number Diff line change
Expand Up @@ -122,9 +122,10 @@ - (void)resolveProducts:(NSArray * _Nonnull)products resolver:(RCTPromiseResolve
}
}

RCT_EXPORT_METHOD(setPushToken:(NSData * _Nonnull)deviceToken resolver:(RCTPromiseResolveBlock)resolve rejecter:(RCTPromiseRejectBlock)reject ) {
RCT_EXPORT_METHOD(setPushToken:(NSString * _Nonnull)deviceToken resolver:(RCTPromiseResolveBlock)resolve rejecter:(RCTPromiseRejectBlock)reject ) {
@try {
[Emarsys.push setPushToken:deviceToken completionBlock:^(NSError *error) {
NSData *pushTokenData = [self dataWithDeviceToken:deviceToken];
[Emarsys.push setPushToken:pushTokenData completionBlock:^(NSError *error) {
if (NULL != error) {
reject(@"RNEmarsysWrapper", @"setPushToken: ", error);
} else {
Expand All @@ -137,6 +138,20 @@ - (void)resolveProducts:(NSArray * _Nonnull)products resolver:(RCTPromiseResolve
}
}

- (NSData *)dataWithDeviceToken:(NSString *)deviceToken {
deviceToken = [deviceToken stringByReplacingOccurrencesOfString:@" " withString:@""];
NSMutableData *data = [[NSMutableData alloc] init];
unsigned char whole_byte;
char byte_chars[3] = {'\0','\0','\0'};
for (int i = 0; i < ([deviceToken length] / 2); i++) {
byte_chars[0] = [deviceToken characterAtIndex:i*2];
byte_chars[1] = [deviceToken characterAtIndex:i*2+1];
whole_byte = strtol(byte_chars, NULL, 16);
[data appendBytes:&whole_byte length:1];
}
return data;
}

RCT_EXPORT_METHOD(clearPushToken:(RCTPromiseResolveBlock)resolve rejecter:(RCTPromiseRejectBlock)reject ) {
@try {
[Emarsys.push clearPushTokenWithCompletionBlock:^(NSError *error) {
Expand Down Expand Up @@ -578,7 +593,7 @@ - (NSString *)stringWithDeviceToken:(NSData *)deviceToken {
const char *data = [deviceToken bytes];
NSMutableString *token = [NSMutableString string];

for (NSUInteger i = 0; i < [deviceToken length]; i++) {
for (int i = 0; i < [deviceToken length]; i++) {
[token appendFormat:@"%02.2hhX", data[i]];
}

Expand Down

0 comments on commit 0002743

Please sign in to comment.