Skip to content

Commit eda5288

Browse files
committed
support store completion for invoke later
1 parent e3974c4 commit eda5288

File tree

3 files changed

+66
-0
lines changed

3 files changed

+66
-0
lines changed

index.js

Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -135,6 +135,21 @@ export default class RNVoipPushNotification {
135135
RNVoipPushNotificationManager.registerVoipToken();
136136
}
137137

138+
/**
139+
* When you have processed necessary initialization for voip push, tell ios completed.
140+
* This is mainly for ios 11+, which apple required us to execute `complete()` when we finished.
141+
* If you want to use this function, make sure you call `[RNVoipPushNotificationManager addCompletionHandler:uuid completionHandler:completion];`
142+
* in `didReceiveIncomingPushWithPayload` in your AppDelegate.m
143+
*
144+
* @static
145+
* @memberof RNVoipPushNotification
146+
*
147+
* uuid:
148+
*/
149+
static onVoipNotificationCompleted(uuid) {
150+
RNVoipPushNotificationManager.onVoipNotificationCompleted(uuid);
151+
}
152+
138153
/**
139154
* You will never need to instantiate `RNVoipPushNotification` yourself.
140155
* Listening to the `notification` event and invoking

ios/RNVoipPushNotification/RNVoipPushNotificationManager.h

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -12,11 +12,17 @@
1212

1313
@interface RNVoipPushNotificationManager : NSObject <RCTBridgeModule>
1414

15+
typedef void (^RNVoipPushNotificationCompletion)(void);
16+
17+
@property (nonatomic, strong) NSMutableDictionary<NSString *, RNVoipPushNotificationCompletion> *completionHandlers;
18+
1519
- (void)voipRegistration;
1620
- (void)registerUserNotification:(NSDictionary *)permissions;
1721
- (NSDictionary *)checkPermissions;
1822
+ (void)didUpdatePushCredentials:(PKPushCredentials *)credentials forType:(NSString *)type;
1923
+ (void)didReceiveIncomingPushWithPayload:(PKPushPayload *)payload forType:(NSString *)type;
2024
+ (NSString *)getCurrentAppBackgroundState;
25+
+ (void)addCompletionHandler:(NSString *)uuid completionHandler:(RNVoipPushNotificationCompletion)completionHandler;
26+
+ (void)removeCompletionHandler:(NSString *)uuid;
2127

2228
@end

ios/RNVoipPushNotification/RNVoipPushNotificationManager.m

Lines changed: 45 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -60,10 +60,30 @@ @implementation RNVoipPushNotificationManager
6060
RCT_EXPORT_MODULE();
6161

6262
@synthesize bridge = _bridge;
63+
static NSMutableDictionary<NSString *, RNVoipPushNotificationCompletion> *completionHandlers = nil;
64+
65+
+ (NSMutableDictionary *)completionHandlers {
66+
if (completionHandlers == nil) {
67+
completionHandlers = [NSMutableDictionary new];
68+
}
69+
return completionHandlers;
70+
}
6371

6472
- (void)dealloc
6573
{
6674
[[NSNotificationCenter defaultCenter] removeObserver:self];
75+
76+
// --- invoke complete() and remove for all completionHanders
77+
for (NSString *uuid in [RNVoipPushNotificationManager completionHandlers]) {
78+
RNVoipPushNotificationCompletion completion = [[RNVoipPushNotificationManager completionHandlers] objectForKey:uuid];
79+
if (completion) {
80+
dispatch_async(dispatch_get_main_queue(), ^{
81+
completion();
82+
});
83+
}
84+
}
85+
86+
[[RNVoipPushNotificationManager completionHandlers] removeAllObjects];
6787
}
6888

6989
- (void)setBridge:(RCTBridge *)bridge
@@ -190,6 +210,31 @@ - (void)handleRemoteNotificationReceived:(NSNotification *)notification
190210
body:notification.userInfo];
191211
}
192212

213+
+ (void)addCompletionHandler:(NSString *)uuid completionHandler:(RNVoipPushNotificationCompletion)completionHandler
214+
{
215+
self.completionHandlers[uuid] = completionHandler;
216+
}
217+
218+
+ (void)removeCompletionHandler:(NSString *)uuid
219+
{
220+
self.completionHandlers[uuid] = nil;
221+
[self.completionHandlers removeObjectForKey:uuid];
222+
}
223+
224+
RCT_EXPORT_METHOD(onVoipNotificationCompleted:(NSString *)uuid)
225+
{
226+
RNVoipPushNotificationCompletion completion = [[RNVoipPushNotificationManager completionHandlers] objectForKey:uuid];
227+
if (completion) {
228+
[RNVoipPushNotificationManager removeCompletionHandler: uuid];
229+
dispatch_async(dispatch_get_main_queue(), ^{
230+
NSLog(@"[RNVoipPushNotificationManager] onVoipNotificationCompleted() complete(). uuid = %@", uuid);
231+
completion();
232+
});
233+
} else {
234+
NSLog(@"[RNVoipPushNotificationManager] onVoipNotificationCompleted() not found. uuid = %@", uuid);
235+
}
236+
}
237+
193238
RCT_EXPORT_METHOD(requestPermissions:(NSDictionary *)permissions)
194239
{
195240
if (RCTRunningInAppExtension()) {

0 commit comments

Comments
 (0)