@radhya/mach-push-react-native 0.1.3 → 0.1.4

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
@@ -5,8 +5,13 @@ import UserNotifications
5
5
 
6
6
  @objc(MachPush)
7
7
  final class MachPush: RCTEventEmitter {
8
- private var nativeTokenResolvers: [RCTPromiseResolveBlock] = []
9
- private var nativeTokenRejecters: [RCTPromiseRejectBlock] = []
8
+ private struct PendingNativeTokenRequest {
9
+ let id: UUID
10
+ let resolve: RCTPromiseResolveBlock
11
+ let reject: RCTPromiseRejectBlock
12
+ }
13
+
14
+ private var nativeTokenRequests: [PendingNativeTokenRequest] = []
10
15
  private var hasEventListeners = false
11
16
 
12
17
  override init() {
@@ -108,11 +113,23 @@ final class MachPush: RCTEventEmitter {
108
113
  resolve(token)
109
114
  return
110
115
  }
111
- nativeTokenResolvers.append(resolve)
112
- nativeTokenRejecters.append(reject)
116
+ let request = PendingNativeTokenRequest(id: UUID(), resolve: resolve, reject: reject)
117
+ nativeTokenRequests.append(request)
113
118
  DispatchQueue.main.async {
114
119
  UIApplication.shared.registerForRemoteNotifications()
115
120
  }
121
+ DispatchQueue.main.asyncAfter(deadline: .now() + 20) { [weak self] in
122
+ guard let self else { return }
123
+ guard let index = self.nativeTokenRequests.firstIndex(where: { $0.id == request.id }) else {
124
+ return
125
+ }
126
+ let pending = self.nativeTokenRequests.remove(at: index)
127
+ pending.reject(
128
+ "MACH_PUSH_APNS_TOKEN_TIMEOUT",
129
+ "Timed out waiting for an APNs device token. Confirm the iOS build has the Push Notifications capability / aps-environment entitlement and that the app is installed on a physical device.",
130
+ nil
131
+ )
132
+ }
116
133
  }
117
134
 
118
135
  @objc(getDeviceInfo:rejecter:)
@@ -162,19 +179,17 @@ final class MachPush: RCTEventEmitter {
162
179
 
163
180
  @objc private func didRegisterToken(_ notification: Notification) {
164
181
  guard let token = notification.userInfo?["token"] as? String else { return }
165
- let resolvers = nativeTokenResolvers
166
- nativeTokenResolvers.removeAll()
167
- nativeTokenRejecters.removeAll()
168
- resolvers.forEach { $0(token) }
182
+ let requests = nativeTokenRequests
183
+ nativeTokenRequests.removeAll()
184
+ requests.forEach { $0.resolve(token) }
169
185
  }
170
186
 
171
187
  @objc private func didFailRegistration(_ notification: Notification) {
172
188
  let error = notification.userInfo?["error"] as? NSError
173
- let rejecters = nativeTokenRejecters
174
- nativeTokenResolvers.removeAll()
175
- nativeTokenRejecters.removeAll()
176
- rejecters.forEach {
177
- $0(
189
+ let requests = nativeTokenRequests
190
+ nativeTokenRequests.removeAll()
191
+ requests.forEach {
192
+ $0.reject(
178
193
  "MACH_PUSH_REGISTRATION",
179
194
  error?.localizedDescription ?? "APNs registration failed.",
180
195
  error
@@ -5,6 +5,26 @@
5
5
  NSString * const MachPushDidRegisterTokenNotification = @"MachPushDidRegisterTokenNotification";
6
6
  NSString * const MachPushDidFailRegistrationNotification = @"MachPushDidFailRegistrationNotification";
7
7
 
8
+ static void MachPushPostDeviceToken(NSData *deviceToken) {
9
+ const unsigned char *bytes = deviceToken.bytes;
10
+ NSMutableString *token = [NSMutableString stringWithCapacity:deviceToken.length * 2];
11
+ for (NSUInteger index = 0; index < deviceToken.length; index++) {
12
+ [token appendFormat:@"%02x", bytes[index]];
13
+ }
14
+ [[NSUserDefaults standardUserDefaults] setObject:token forKey:@"mach.push.nativeToken"];
15
+ [[NSNotificationCenter defaultCenter]
16
+ postNotificationName:MachPushDidRegisterTokenNotification
17
+ object:nil
18
+ userInfo:@{ @"token": token }];
19
+ }
20
+
21
+ static void MachPushPostRegistrationError(NSError *error) {
22
+ [[NSNotificationCenter defaultCenter]
23
+ postNotificationName:MachPushDidFailRegistrationNotification
24
+ object:nil
25
+ userInfo:@{ @"error": error }];
26
+ }
27
+
8
28
  static BOOL MachPushSwizzle(Class cls, SEL original, SEL replacement) {
9
29
  Method replacementMethod = class_getInstanceMethod([NSObject class], replacement);
10
30
  if (!replacementMethod) return NO;
@@ -79,18 +99,19 @@ static void MachPushInstallInterceptorIfNeeded(void) {
79
99
  });
80
100
  }
81
101
 
102
+ - (void)application:(UIApplication *)application
103
+ didRegisterForRemoteNotificationsWithDeviceToken:(NSData *)deviceToken {
104
+ MachPushPostDeviceToken(deviceToken);
105
+ }
106
+
107
+ - (void)application:(UIApplication *)application
108
+ didFailToRegisterForRemoteNotificationsWithError:(NSError *)error {
109
+ MachPushPostRegistrationError(error);
110
+ }
111
+
82
112
  - (void)mach_application:(UIApplication *)application
83
113
  didRegisterForRemoteNotificationsWithDeviceToken:(NSData *)deviceToken {
84
- const unsigned char *bytes = deviceToken.bytes;
85
- NSMutableString *token = [NSMutableString stringWithCapacity:deviceToken.length * 2];
86
- for (NSUInteger index = 0; index < deviceToken.length; index++) {
87
- [token appendFormat:@"%02x", bytes[index]];
88
- }
89
- [[NSUserDefaults standardUserDefaults] setObject:token forKey:@"mach.push.nativeToken"];
90
- [[NSNotificationCenter defaultCenter]
91
- postNotificationName:MachPushDidRegisterTokenNotification
92
- object:nil
93
- userInfo:@{ @"token": token }];
114
+ MachPushPostDeviceToken(deviceToken);
94
115
  if (MachPushHadRegisterHandler) {
95
116
  [self mach_application:application didRegisterForRemoteNotificationsWithDeviceToken:deviceToken];
96
117
  }
@@ -98,10 +119,7 @@ static void MachPushInstallInterceptorIfNeeded(void) {
98
119
 
99
120
  - (void)mach_application:(UIApplication *)application
100
121
  didFailToRegisterForRemoteNotificationsWithError:(NSError *)error {
101
- [[NSNotificationCenter defaultCenter]
102
- postNotificationName:MachPushDidFailRegistrationNotification
103
- object:nil
104
- userInfo:@{ @"error": error }];
122
+ MachPushPostRegistrationError(error);
105
123
  if (MachPushHadFailureHandler) {
106
124
  [self mach_application:application didFailToRegisterForRemoteNotificationsWithError:error];
107
125
  }
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@radhya/mach-push-react-native",
3
- "version": "0.1.3",
3
+ "version": "0.1.4",
4
4
  "description": "MACH Push for React Native and Expo development builds",
5
5
  "license": "MIT",
6
6
  "repository": {