diff --git a/ios/RNVoipPushNotification/RNVoipPushNotificationManager.h b/ios/RNVoipPushNotification/RNVoipPushNotificationManager.h index 19f4bb4..ea116a8 100644 --- a/ios/RNVoipPushNotification/RNVoipPushNotificationManager.h +++ b/ios/RNVoipPushNotification/RNVoipPushNotificationManager.h @@ -17,6 +17,7 @@ typedef void (^RNVoipPushNotificationCompletion)(void); @property (nonatomic, strong) NSMutableDictionary *completionHandlers; ++ (void)voipRegistration; + (void)didUpdatePushCredentials:(PKPushCredentials *)credentials forType:(NSString *)type; + (void)didReceiveIncomingPushWithPayload:(PKPushPayload *)payload forType:(NSString *)type; + (void)addCompletionHandler:(NSString *)uuid completionHandler:(RNVoipPushNotificationCompletion)completionHandler; diff --git a/ios/RNVoipPushNotification/RNVoipPushNotificationManager.m b/ios/RNVoipPushNotification/RNVoipPushNotificationManager.m index 0e76d8f..2699a77 100644 --- a/ios/RNVoipPushNotification/RNVoipPushNotificationManager.m +++ b/ios/RNVoipPushNotification/RNVoipPushNotificationManager.m @@ -28,6 +28,7 @@ @implementation RNVoipPushNotificationManager RCT_EXPORT_MODULE(); +static bool _isVoipRegistered = NO; static NSMutableDictionary *completionHandlers = nil; @@ -118,22 +119,29 @@ - (void)sendEventWithNameWrapper:(NSString *)name body:(id)body { } } -// --- register voip token -- (void)voipRegistration +// --- register delegate for PushKit to delivery credential and remote voip push to your delegate +// --- this usually register once and ASAP after your app launch ++ (void)voipRegistration { + if (_isVoipRegistered) { #ifdef DEBUG - RCTLog(@"[RNVoipPushNotificationManager] voipRegistration"); + RCTLog(@"[RNVoipPushNotificationManager] voipRegistration is already registered"); #endif - - dispatch_queue_t mainQueue = dispatch_get_main_queue(); - dispatch_async(mainQueue, ^{ - // --- Create a push registry object - PKPushRegistry * voipRegistry = [[PKPushRegistry alloc] initWithQueue: mainQueue]; - // --- Set the registry's delegate to AppDelegate - voipRegistry.delegate = (RNVoipPushNotificationManager *)RCTSharedApplication().delegate; - // --- Set the push type to VoIP - voipRegistry.desiredPushTypes = [NSSet setWithObject:PKPushTypeVoIP]; - }); + } else { + _isVoipRegistered = YES; +#ifdef DEBUG + RCTLog(@"[RNVoipPushNotificationManager] voipRegistration enter"); +#endif + dispatch_queue_t mainQueue = dispatch_get_main_queue(); + dispatch_async(mainQueue, ^{ + // --- Create a push registry object + PKPushRegistry * voipRegistry = [[PKPushRegistry alloc] initWithQueue: mainQueue]; + // --- Set the registry's delegate to AppDelegate + voipRegistry.delegate = (RNVoipPushNotificationManager *)RCTSharedApplication().delegate; + // --- Set the push type to VoIP + voipRegistry.desiredPushTypes = [NSSet setWithObject:PKPushTypeVoIP]; + }); + } } // --- should be called from `AppDelegate.didUpdatePushCredentials` @@ -200,7 +208,7 @@ + (void)removeCompletionHandler:(NSString *)uuid return; } dispatch_async(dispatch_get_main_queue(), ^{ - [self voipRegistration]; + [RNVoipPushNotificationManager voipRegistration]; }); }