Skip to content

Commit c66074e

Browse files
committed
Fix setEmail Callback Issue
• setEmail() was meant to accept callback and emailAuthToken as optional parameters • However it was crashing if no callback was passed in because React-Native requires it to be non-nil • Fixed the function to create a default callback if none is passed in • Also resolved errors not being correctly serialized for RN to use.
1 parent 61299ef commit c66074e

File tree

3 files changed

+29
-21
lines changed

3 files changed

+29
-21
lines changed

index.js

Lines changed: 6 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -200,17 +200,17 @@ export default class OneSignal {
200200
}
201201

202202
static setEmail(email, emailAuthCode, callback) {
203-
204-
if (callback == undefined && typeof emailAuthCode == 'function') {
203+
if (emailAuthCode == undefined) {
205204
//emailAuthCode is an optional parameter
206205
//since JS does not support function overloading,
207206
//unauthenticated setEmail calls will have emailAuthCode as the callback
208207

209-
var callback = emailAuthCode;
210-
211-
RNOneSignal.setUnauthenticatedEmail(email, callback);
208+
RNOneSignal.setUnauthenticatedEmail(email, function(){});
209+
} else if (callback == undefined && typeof emailAuthCode == 'function') {
210+
RNOneSignal.setUnauthenticatedEmail(email, emailAuthCode);
211+
} else if (callback == undefined) {
212+
RNOneSignal.setEmail(email, emailAuthCode, function(){});
212213
} else {
213-
214214
RNOneSignal.setEmail(email, emailAuthCode, callback);
215215
}
216216
}

ios/RCTOneSignal/RCTOneSignalEventEmitter.m

Lines changed: 23 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -149,11 +149,10 @@ + (void)sendEventWithName:(NSString *)name withBody:(NSDictionary *)body {
149149

150150
RCT_EXPORT_METHOD(setEmail :(NSString *)email withAuthHash:(NSString *)authHash withResponse:(RCTResponseSenderBlock)callback) {
151151
// Auth hash token created on server and sent to client.
152-
153152
[OneSignal setEmail:email withEmailAuthHashToken:authHash withSuccess:^{
154153
callback(@[]);
155154
} withFailure:^(NSError *error) {
156-
callback(@[error]);
155+
callback(@[error.userInfo[@"error"] ?: error.localizedDescription]);
157156
}];
158157
}
159158

@@ -162,15 +161,15 @@ + (void)sendEventWithName:(NSString *)name withBody:(NSDictionary *)body {
162161
[OneSignal setEmail:email withSuccess:^{
163162
callback(@[]);
164163
} withFailure:^(NSError *error) {
165-
callback(@[error]);
164+
callback(@[error.userInfo[@"error"] ?: error.localizedDescription]);
166165
}];
167166
}
168167

169168
RCT_EXPORT_METHOD(logoutEmail:(RCTResponseSenderBlock)callback) {
170169
[OneSignal logoutEmailWithSuccess:^{
171170
callback(@[]);
172171
} withFailure:^(NSError *error) {
173-
callback(@[error]);
172+
callback(@[error.userInfo[@"error"] ?: error.localizedDescription]);
174173
}];
175174
}
176175

@@ -284,24 +283,33 @@ + (void)sendEventWithName:(NSString *)name withBody:(NSDictionary *)body {
284283
[OneSignal promptLocation];
285284
}
286285

287-
RCT_EXPORT_METHOD(postNotification:(NSDictionary *)contents data:(NSDictionary *)data player_id:(NSString*)player_id other_parameters:(NSDictionary *)other_parameters) {
288-
NSDictionary * additionalData = @{@"p2p_notification": data};
286+
// The post notification endpoint accepts four parameters.
287+
RCT_EXPORT_METHOD(postNotification:(NSDictionary *)contents data:(NSDictionary *)data player_id:(id)player_ids other_parameters:(NSDictionary *)other_parameters) {
288+
NSDictionary * additionalData = data ? @{@"p2p_notification": data} : @{};
289289

290290
NSMutableDictionary * extendedData = [additionalData mutableCopy];
291-
BOOL isHidden = [[other_parameters objectForKey:@"hidden"] boolValue];
291+
BOOL isHidden = [[other_parameters ?: @{} objectForKey:@"hidden"] boolValue];
292292
if (isHidden) {
293293
[extendedData setObject:[NSNumber numberWithBool:YES] forKey:@"hidden"];
294294
}
295295

296-
NSDictionary *notification = @{
297-
@"contents" : contents,
298-
@"data" : extendedData,
299-
@"include_player_ids": @[player_id]
300-
};
301-
NSMutableDictionary * extendedNotification = [notification mutableCopy];
302-
[extendedNotification addEntriesFromDictionary: other_parameters];
296+
NSMutableDictionary *notification = [NSMutableDictionary new];
297+
notification[@"contents"] = contents;
298+
notification[@"data"] = extendedData;
299+
300+
if (player_ids && [player_ids isKindOfClass:[NSArray class]]) {
301+
//array of player ids
302+
notification[@"include_player_ids"] = (NSArray<NSString *> *)player_ids;
303+
} else if (player_ids && [player_ids isKindOfClass:[NSString class]]) {
304+
//individual player id
305+
notification[@"include_player_ids"] = @[(NSString *)player_ids];
306+
}
307+
308+
if (other_parameters) {
309+
[notification addEntriesFromDictionary:other_parameters];
310+
}
303311

304-
[OneSignal postNotification:extendedNotification];
312+
[OneSignal postNotification:notification];
305313
}
306314

307315
RCT_EXPORT_METHOD(syncHashedEmail:(NSString*)email) {

ios/libOneSignal.a

727 KB
Binary file not shown.

0 commit comments

Comments
 (0)