Skip to content

Commit c231bf6

Browse files
committed
[ios] support Notifications.getDevicePushTokenAsync in ExpoKit
1 parent f5b3bcc commit c231bf6

File tree

3 files changed

+25
-11
lines changed

3 files changed

+25
-11
lines changed

ios/Exponent/Kernel/Services/Notifications/EXRemoteNotificationManager.m

Lines changed: 16 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -110,13 +110,24 @@ - (void)registerAPNSToken:(nullable NSData *)token registrationError:(nullable N
110110

111111
#pragma mark - scoped module delegate
112112

113-
- (NSString *)apnsTokenStringForScopedModule:(__unused id)scopedModule
113+
- (void)getApnsTokenForScopedModule:(id)scopedModule
114+
completionHandler:(void (^)(NSString * _Nullable, NSError * _Nullable))handler
114115
{
115-
NSData *maybeToken = _currentAPNSToken ?: [NSUserDefaults.standardUserDefaults objectForKey:kEXCurrentAPNSTokenDefaultsKey];
116-
if (maybeToken) {
117-
return [maybeToken apnsTokenString];
116+
if (_currentAPNSToken) {
117+
handler([_currentAPNSToken apnsTokenString], nil);
118+
return;
118119
}
119-
return nil;
120+
121+
[_apnsTokenHandlers addObject:^(NSData * _Nullable apnsToken, NSError * _Nullable registrationError) {
122+
if (registrationError) {
123+
handler(nil, registrationError);
124+
} else {
125+
handler([apnsToken apnsTokenString], nil);
126+
}
127+
}];
128+
dispatch_async(dispatch_get_main_queue(), ^{
129+
[RCTSharedApplication() registerForRemoteNotifications];
130+
});
120131
}
121132

122133
- (void)getExpoPushTokenForScopedModule:(id)scopedModule

ios/Exponent/Versioned/Core/Api/EXNotifications.h

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -25,7 +25,8 @@ NS_ASSUME_NONNULL_BEGIN
2525

2626
@protocol EXNotificationsScopedModuleDelegate
2727

28-
- (NSString *)apnsTokenStringForScopedModule:(id)scopedModule;
28+
- (void)getApnsTokenForScopedModule:(id)scopedModule
29+
completionHandler:(void (^)(NSString * _Nullable, NSError * _Nullable))handler;
2930
- (void)getExpoPushTokenForScopedModule:(id)scopedModule
3031
completionHandler:(void (^)(NSString * _Nullable pushToken, NSError * _Nullable error))handler;
3132

ios/Exponent/Versioned/Core/Api/EXNotifications.m

Lines changed: 7 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -68,11 +68,13 @@ - (instancetype)initWithExperienceId:(NSString *)experienceId kernelServiceDeleg
6868
return reject(0, @"getDevicePushTokenAsync is only accessible within standalone applications", nil);
6969
}
7070

71-
NSString *token = [_remoteNotificationsDelegate apnsTokenStringForScopedModule:self];
72-
if (!token) {
73-
return reject(0, @"APNS token has not been set", nil);
74-
}
75-
return resolve(@{ @"type": @"apns", @"data": token });
71+
[_remoteNotificationsDelegate getApnsTokenForScopedModule:self completionHandler:^(NSString * _Nullable apnsToken, NSError * _Nullable error) {
72+
if (error) {
73+
reject(@"ERR_NOTIFICATIONS_PUSH_REGISTRATION_FAILED", error.localizedDescription, error);
74+
} else {
75+
resolve(@{ @"type": @"apns", @"data": apnsToken });
76+
}
77+
}];
7678
}
7779

7880
RCT_REMAP_METHOD(getExponentPushTokenAsync,

0 commit comments

Comments
 (0)