From afb3b11a87425b38688564eedf5ff38f2e3e5be2 Mon Sep 17 00:00:00 2001 From: Roman Melnyk Date: Wed, 12 May 2021 14:02:31 +0300 Subject: [PATCH 1/3] #86 Fix singletone class initialisation. Handle bundle stob start observing issue. --- .../RNVoipPushNotificationManager.m | 34 +++++++++++++------ 1 file changed, 23 insertions(+), 11 deletions(-) diff --git a/ios/RNVoipPushNotification/RNVoipPushNotificationManager.m b/ios/RNVoipPushNotification/RNVoipPushNotificationManager.m index d84e59a..8797825 100644 --- a/ios/RNVoipPushNotification/RNVoipPushNotificationManager.m +++ b/ios/RNVoipPushNotification/RNVoipPushNotificationManager.m @@ -30,27 +30,32 @@ @implementation RNVoipPushNotificationManager static NSString *_lastVoipToken = @""; static NSMutableDictionary *completionHandlers = nil; - // ===== // ===== RN Module Configure and Override ===== // ===== +-(instancetype) init { + return [[self class] sharedInstance]; +} -- (instancetype)init -{ - if (self = [super init]) { +-(instancetype) initPrivate { + self = [super init]; + if (self) { + // Initialization code here. _delayedEvents = [NSMutableArray array]; } return self; } -+ (id)allocWithZone:(NSZone *)zone { - static RNVoipPushNotificationManager *sharedInstance = nil; +// Singletone implementation based on https://stackoverflow.com/q/5720029/3686678 and https://stackoverflow.com/a/7035136/3686678 ++(instancetype) sharedInstance { + static RNVoipPushNotificationManager *sharedVoipPushManager = nil; static dispatch_once_t onceToken; dispatch_once(&onceToken, ^{ - sharedInstance = [super allocWithZone:zone]; + sharedVoipPushManager = [[self alloc] initPrivate]; }); - return sharedInstance; + + return sharedVoipPushManager; } // --- clean observer and completionHandlers when app close @@ -90,12 +95,18 @@ - (void)startObserving { _hasListeners = YES; if ([_delayedEvents count] > 0) { + // RCTLog(@"[RNVoipPushNotificationManager] sendEventWithName _hasListeners = %d name = %@", _hasListeners, RNVoipPushDidLoadWithEvents); [self sendEventWithName:RNVoipPushDidLoadWithEvents body:_delayedEvents]; } } - (void)stopObserving { +#ifdef DEBUG + // RCTLog(@"[RNVoipPushNotificationManager] skip stopObserving because in debug mode startObserving is not called on reload, but stopObserving is called before reload"); + return; +#endif + _hasListeners = NO; } @@ -107,6 +118,7 @@ - (void)stopObserving // --- send directly if has listeners, cache it otherwise - (void)sendEventWithNameWrapper:(NSString *)name body:(id)body { + // RCTLog(@"[RNVoipPushNotificationManager] sendEventWithNameWrapper _hasListeners = %d name = %@", _hasListeners, name); if (_hasListeners) { [self sendEventWithName:name body:body]; } else { @@ -126,7 +138,7 @@ + (void)voipRegistration #ifdef DEBUG RCTLog(@"[RNVoipPushNotificationManager] voipRegistration is already registered. return _lastVoipToken = %@", _lastVoipToken); #endif - RNVoipPushNotificationManager *voipPushManager = [RNVoipPushNotificationManager allocWithZone: nil]; + RNVoipPushNotificationManager *voipPushManager = [RNVoipPushNotificationManager sharedInstance]; [voipPushManager sendEventWithNameWrapper:RNVoipPushRemoteNotificationsRegisteredEvent body:_lastVoipToken]; } else { _isVoipRegistered = YES; @@ -164,7 +176,7 @@ + (void)didUpdatePushCredentials:(PKPushCredentials *)credentials forType:(NSStr _lastVoipToken = [hexString copy]; - RNVoipPushNotificationManager *voipPushManager = [RNVoipPushNotificationManager allocWithZone: nil]; + RNVoipPushNotificationManager *voipPushManager = [RNVoipPushNotificationManager sharedInstance]; [voipPushManager sendEventWithNameWrapper:RNVoipPushRemoteNotificationsRegisteredEvent body:_lastVoipToken]; } @@ -175,7 +187,7 @@ + (void)didReceiveIncomingPushWithPayload:(PKPushPayload *)payload forType:(NSSt RCTLog(@"[RNVoipPushNotificationManager] didReceiveIncomingPushWithPayload payload.dictionaryPayload = %@, type = %@", payload.dictionaryPayload, type); #endif - RNVoipPushNotificationManager *voipPushManager = [RNVoipPushNotificationManager allocWithZone: nil]; + RNVoipPushNotificationManager *voipPushManager = [RNVoipPushNotificationManager sharedInstance]; [voipPushManager sendEventWithNameWrapper:RNVoipPushRemoteNotificationReceivedEvent body:payload.dictionaryPayload]; } From 27a11075d43e3f53eccbc22335618e96f6c361a7 Mon Sep 17 00:00:00 2001 From: Roman Melnyk Date: Wed, 19 May 2021 10:22:58 +0300 Subject: [PATCH 2/3] #86 Applying review changes. --- .../RNVoipPushNotificationManager.m | 15 ++------------- 1 file changed, 2 insertions(+), 13 deletions(-) diff --git a/ios/RNVoipPushNotification/RNVoipPushNotificationManager.m b/ios/RNVoipPushNotification/RNVoipPushNotificationManager.m index 8797825..a1ba330 100644 --- a/ios/RNVoipPushNotification/RNVoipPushNotificationManager.m +++ b/ios/RNVoipPushNotification/RNVoipPushNotificationManager.m @@ -39,12 +39,8 @@ -(instancetype) init { } -(instancetype) initPrivate { - self = [super init]; - if (self) { - // Initialization code here. - _delayedEvents = [NSMutableArray array]; - } - return self; + _delayedEvents = [NSMutableArray array]; + return [super init]; } // Singletone implementation based on https://stackoverflow.com/q/5720029/3686678 and https://stackoverflow.com/a/7035136/3686678 @@ -95,18 +91,12 @@ - (void)startObserving { _hasListeners = YES; if ([_delayedEvents count] > 0) { - // RCTLog(@"[RNVoipPushNotificationManager] sendEventWithName _hasListeners = %d name = %@", _hasListeners, RNVoipPushDidLoadWithEvents); [self sendEventWithName:RNVoipPushDidLoadWithEvents body:_delayedEvents]; } } - (void)stopObserving { -#ifdef DEBUG - // RCTLog(@"[RNVoipPushNotificationManager] skip stopObserving because in debug mode startObserving is not called on reload, but stopObserving is called before reload"); - return; -#endif - _hasListeners = NO; } @@ -118,7 +108,6 @@ - (void)stopObserving // --- send directly if has listeners, cache it otherwise - (void)sendEventWithNameWrapper:(NSString *)name body:(id)body { - // RCTLog(@"[RNVoipPushNotificationManager] sendEventWithNameWrapper _hasListeners = %d name = %@", _hasListeners, name); if (_hasListeners) { [self sendEventWithName:name body:body]; } else { From e84e5f828f133b3fdabfd156d81fd713ab980496 Mon Sep 17 00:00:00 2001 From: Roman Melnyk Date: Wed, 19 May 2021 10:26:35 +0300 Subject: [PATCH 3/3] Update RNVoipPushNotificationManager.m --- ios/RNVoipPushNotification/RNVoipPushNotificationManager.m | 5 +++++ 1 file changed, 5 insertions(+) diff --git a/ios/RNVoipPushNotification/RNVoipPushNotificationManager.m b/ios/RNVoipPushNotification/RNVoipPushNotificationManager.m index a1ba330..756d27d 100644 --- a/ios/RNVoipPushNotification/RNVoipPushNotificationManager.m +++ b/ios/RNVoipPushNotification/RNVoipPushNotificationManager.m @@ -97,6 +97,11 @@ - (void)startObserving - (void)stopObserving { +// Skip stopObserving because in debug mode startObserving is not called on reload, but stopObserving is called before reload +#ifdef DEBUG + return; +#endif + _hasListeners = NO; }