Skip to content

Commit

Permalink
[SDK-874] : Push notification caching, open url issue fixes after cou…
Browse files Browse the repository at this point in the history
…ntly init (#90)

* Push notification caching, open url issue fixes after countly init

* Updated SDK version to 21.11.2

* typo fixes

* Adding constants for string keys

* Adding try/catch in recordPushAction and removing extra code
  • Loading branch information
ijunaid committed Jul 15, 2022
1 parent 6cdabd5 commit 01f1a2c
Show file tree
Hide file tree
Showing 6 changed files with 64 additions and 85 deletions.
5 changes: 5 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
@@ -1,3 +1,8 @@
## 21.11.2
* Fixed iOS push notification open url issue when notification is cached.
* Underlying android SDK version is 21.11.2
* Underlying iOS SDK version is 21.11.2

## 21.11.1
* Fixed bug that caused crashes when migrating from older versions on Android devices.
* Updated underlying android SDK version to 21.11.2
Expand Down
2 changes: 1 addition & 1 deletion CountlyReactNative.podspec
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
Pod::Spec.new do |s|
s.name = 'CountlyReactNative'
s.version = '21.11.1'
s.version = '21.11.2'
s.license = {
:type => 'COMMUNITY',
:text => <<-LICENSE
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -79,7 +79,7 @@ public String toString(){
public class CountlyReactNative extends ReactContextBaseJavaModule implements LifecycleEventListener {

public static final String TAG = "CountlyRNPlugin";
private String COUNTLY_RN_SDK_VERSION_STRING = "21.11.1";
private String COUNTLY_RN_SDK_VERSION_STRING = "21.11.2";
private String COUNTLY_RN_SDK_NAME = "js-rnb-android";

private static final CountlyConfig config = new CountlyConfig();
Expand Down
2 changes: 1 addition & 1 deletion example/install.sh
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@ rm App.js
curl https://raw.githubusercontent.com/Countly/countly-sdk-react-native-bridge/master/example/App.js --output App.js
curl https://raw.githubusercontent.com/Countly/countly-sdk-react-native-bridge/master/example/Example.js --output Example.js

yarn add countly-sdk-react-native-bridge@21.11.1
yarn add countly-sdk-react-native-bridge@21.11.2

cd ./ios
pod install
Expand Down
136 changes: 55 additions & 81 deletions ios/src/CountlyReactNative.m
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,7 @@ @interface CountlyFeedbackWidget ()
+ (CountlyFeedbackWidget *)createWithDictionary:(NSDictionary *)dictionary;
@end

NSString* const kCountlyReactNativeSDKVersion = @"21.11.1";
NSString* const kCountlyReactNativeSDKVersion = @"21.11.2";
NSString* const kCountlyReactNativeSDKName = @"js-rnb-ios";

CountlyConfig* config = nil;
Expand All @@ -31,8 +31,9 @@ + (CountlyFeedbackWidget *)createWithDictionary:(NSDictionary *)dictionary;
NSMutableArray<CLYFeature>* countlyFeatures = nil;
BOOL enablePushNotifications = true;

NSDictionary* notificationCacheDictionary = nil;
NSInteger notificationCacheButtonIndex = 0;
typedef NSString* CLYUserDefaultKey NS_EXTENSIBLE_STRING_ENUM;
CLYUserDefaultKey const CLYPushDictionaryKey = @"notificationDictionaryKey";
CLYUserDefaultKey const CLYPushButtonIndexKey = @"notificationBtnIndexKey";

@implementation CountlyReactNative
NSString* const kCountlyNotificationPersistencyKey = @"kCountlyNotificationPersistencyKey";
Expand Down Expand Up @@ -73,23 +74,9 @@ @implementation CountlyReactNative
dispatch_async(dispatch_get_main_queue(), ^
{
[[Countly sharedInstance] startWithConfig:config];

[self recordPushAction];
resolve(@"Success");
// NSData* readData = [NSData dataWithContentsOfURL:[CountlyReactNative storageFileURL]];

if (notificationCacheDictionary != nil)
{
// NSDictionary* readDict = [NSKeyedUnarchiver unarchiveObjectWithData:readData];
NSDictionary* notificationDictionary = notificationCacheDictionary;// [readDict[kCountlyNotificationPersistencyKey] mutableCopy];
NSInteger buttonIndex = notificationCacheButtonIndex;
if([notificationDictionary count] > 0) {
[Countly.sharedInstance recordActionForNotification:notificationDictionary clickedButtonIndex:buttonIndex];
notificationCacheDictionary = nil;
notificationCacheButtonIndex = 0;
// [CountlyReactNative saveToFile:NSMutableDictionary.new buttonIndex:0];
}

}

});
}
});
Expand Down Expand Up @@ -228,7 +215,7 @@ @implementation CountlyReactNative

NSString* token = [arguments objectAtIndex:0];
NSString* messagingMode = @"1";
if(config.pushTestMode == nil || [config.pushTestMode isEqual: @""] || [config.pushTestMode isEqualToString:@"CLYPushTestModeTestFlightOrAdHoc"]) {
if(config.pushTestMode == nil || [config.pushTestMode isEqual: @""] || [config.pushTestMode isEqualToString:CLYPushTestModeTestFlightOrAdHoc]) {
messagingMode = @"0";
}
NSString *urlString = [ @"" stringByAppendingFormat:@"%@?device_id=%@&app_key=%@&token_session=1&test_mode=%@&ios_token=%@", config.host, [Countly.sharedInstance deviceID], config.appKey, messagingMode, token];
Expand All @@ -246,10 +233,10 @@ @implementation CountlyReactNative
config.sendPushTokenAlways = YES;
NSString* tokenType = [arguments objectAtIndex:0];
if([tokenType isEqualToString: @"1"]){
config.pushTestMode = @"CLYPushTestModeDevelopment";
config.pushTestMode = CLYPushTestModeDevelopment;
}
else if([tokenType isEqualToString: @"2"]){
config.pushTestMode = @"CLYPushTestModeTestFlightOrAdHoc";
else if([tokenType isEqualToString: @"2"] || [tokenType isEqualToString: @"0"]){
config.pushTestMode = CLYPushTestModeTestFlightOrAdHoc;
}else{
}
});
Expand Down Expand Up @@ -1151,6 +1138,11 @@ + (void)onNotification:(NSDictionary *)notificationMessage buttonIndex:(NSIntege
}else{
lastStoredNotification = notificationMessage;
}
if(!CountlyCommon.sharedInstance.hasStarted) {
[[NSUserDefaults standardUserDefaults] setObject:notificationMessage forKey:CLYPushDictionaryKey];
[[NSUserDefaults standardUserDefaults] setInteger:btnIndex forKey:CLYPushButtonIndexKey];
[[NSUserDefaults standardUserDefaults] synchronize];
}
if(notificationMessage){
if(notificationIDs == nil){
notificationIDs = [[NSMutableArray alloc] init];
Expand All @@ -1173,75 +1165,57 @@ +(void)onNotificationResponse:(UNNotificationResponse *)response
{
buttonIndex = [[response.actionIdentifier stringByReplacingOccurrencesOfString:kCountlyActionIdentifier withString:@""] integerValue];
}
if(!CountlyCommon.sharedInstance.hasStarted) {
notificationCacheDictionary = notificationDictionary;
notificationCacheButtonIndex = buttonIndex;
// [CountlyReactNative saveToFile:notificationDictionary buttonIndex:buttonIndex];
}
[CountlyReactNative onNotification:notificationDictionary buttonIndex:buttonIndex];

}
+ (NSURL *)storageDirectoryURL
{
static NSURL* URL = nil;

static dispatch_once_t onceToken;
dispatch_once(&onceToken, ^
{
#if (TARGET_OS_TV)
NSSearchPathDirectory directory = NSCachesDirectory;
#else
NSSearchPathDirectory directory = NSApplicationSupportDirectory;
#endif
URL = [[NSFileManager.defaultManager URLsForDirectory:directory inDomains:NSUserDomainMask] lastObject];

#if (TARGET_OS_OSX)
URL = [URL URLByAppendingPathComponent:NSBundle.mainBundle.bundleIdentifier];
#endif
NSError *error = nil;

if (![NSFileManager.defaultManager fileExistsAtPath:URL.path])
- (void) recordPushAction {
@try{
NSDictionary* responseDictionary = [[NSUserDefaults standardUserDefaults] dictionaryForKey:CLYPushDictionaryKey];
NSInteger responseBtnIndex = [[NSUserDefaults standardUserDefaults] integerForKey:CLYPushButtonIndexKey];
if (responseDictionary != nil)
{
[NSFileManager.defaultManager createDirectoryAtURL:URL withIntermediateDirectories:YES attributes:nil error:&error];
if (error)
{
COUNTLY_RN_LOG(@"Application Support directory can not be created: \n%@", error);
if([responseDictionary count] > 0) {

NSDictionary* countlyPayload = responseDictionary[kCountlyPNKeyCountlyPayload];
NSString* URL = @"";
if (responseBtnIndex == 0)
{
URL = countlyPayload[kCountlyPNKeyDefaultURL];
}
else
{
URL = countlyPayload[kCountlyPNKeyButtons][responseBtnIndex - 1][kCountlyPNKeyActionButtonURL];
}

[Countly.sharedInstance recordActionForNotification:responseDictionary clickedButtonIndex:responseBtnIndex];
[[NSUserDefaults standardUserDefaults] removeObjectForKey:CLYPushDictionaryKey];
[[NSUserDefaults standardUserDefaults] removeObjectForKey:CLYPushButtonIndexKey];
[[NSUserDefaults standardUserDefaults] synchronize];

[self openURL:URL];
}

}
});

return URL;
}
@catch(NSException *exception){
COUNTLY_RN_LOG(@"Exception Occurred while recording push action: %@", exception);
}
}

+ (NSURL *)storageFileURL
- (void)openURL:(NSString *)URLString
{
NSString* const kCountlyPersistencyFileName = @"CountlyBridge.dat";

static NSURL* URL = nil;
if (!URLString)
return;

static dispatch_once_t onceToken;
dispatch_once(&onceToken, ^
dispatch_after(dispatch_time(DISPATCH_TIME_NOW, (int64_t)(0.01 * NSEC_PER_SEC)), dispatch_get_main_queue(), ^
{
URL = [[CountlyReactNative storageDirectoryURL] URLByAppendingPathComponent:kCountlyPersistencyFileName];
#if (TARGET_OS_IOS)
[UIApplication.sharedApplication openURL:[NSURL URLWithString:URLString] options:@{} completionHandler:nil];
// Removing this line because currently we are not supporting OSX
//#elif (TARGET_OS_OSX)
// [NSWorkspace.sharedWorkspace openURL:[NSURL URLWithString:URLString]];
#endif
});

return URL;
}

+ (void)saveToFile:(NSDictionary *)notificationMessage buttonIndex:(NSInteger)btnIndex
{
NSData* saveData;

@synchronized (self)
{
saveData = [NSKeyedArchiver archivedDataWithRootObject:@{kCountlyNotificationPersistencyKey: notificationMessage}];

}

#pragma clang diagnostic push
#pragma clang diagnostic ignored "-Wunused-variable"

BOOL writeResult = [saveData writeToFile:[CountlyReactNative storageFileURL].path atomically:YES];
COUNTLY_RN_LOG(@"Result of writing data to file: %d", writeResult);
}
@end
@end
2 changes: 1 addition & 1 deletion package.json
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
{
"name": "countly-sdk-react-native-bridge",
"version": "21.11.1",
"version": "21.11.2",
"author": "Countly <hello@count.ly> (https://count.ly/)",
"bugs": {
"url": "https://github.com/Countly/countly-sdk-react-native-bridge/issues"
Expand Down

0 comments on commit 01f1a2c

Please sign in to comment.