diff --git a/BlueShift-iOS-SDK/BatchEventEntity.m b/BlueShift-iOS-SDK/BatchEventEntity.m index f67872f5..f26043c2 100644 --- a/BlueShift-iOS-SDK/BatchEventEntity.m +++ b/BlueShift-iOS-SDK/BatchEventEntity.m @@ -48,15 +48,23 @@ - (void)insertEntryParametersList:(NSArray *)parametersArray andNextRetryTimeSta @try { if(context && [context isKindOfClass:[NSManagedObjectContext class]]) { [context performBlock:^{ - NSError *error = nil; - [context save:&error]; - if(masterContext && [masterContext isKindOfClass:[NSManagedObjectContext class]]) { - [masterContext performBlock:^{ - NSError *error = nil; - if (masterContext) { - [masterContext save:&error]; - } - }]; + @try { + NSError *error = nil; + [context save:&error]; + if(masterContext && [masterContext isKindOfClass:[NSManagedObjectContext class]]) { + [masterContext performBlock:^{ + @try { + NSError *error = nil; + if (masterContext) { + [masterContext save:&error]; + } + } @catch (NSException *exception) { + [BlueshiftLog logException:exception withDescription:nil methodName:[NSString stringWithUTF8String:__PRETTY_FUNCTION__]]; + } + }]; + } + } @catch (NSException *exception) { + [BlueshiftLog logException:exception withDescription:nil methodName:[NSString stringWithUTF8String:__PRETTY_FUNCTION__]]; } }]; } @@ -64,8 +72,6 @@ - (void)insertEntryParametersList:(NSArray *)parametersArray andNextRetryTimeSta @catch (NSException *exception) { [BlueshiftLog logException:exception withDescription:nil methodName:[NSString stringWithUTF8String:__PRETTY_FUNCTION__]]; } - } else { - return ; } } @@ -104,17 +110,22 @@ + (void *)fetchBatchesFromCoreDataWithCompletetionHandler:(void (^)(BOOL, NSArra + (void *)fetchBatchesFromCoreDataFromContext:(NSManagedObjectContext*) context request: (NSFetchRequest*)fetchRequest handler:(void (^)(BOOL, NSArray *))handler { NSNumber *currentTimeStamp = [NSNumber numberWithDouble:[[[NSDate date] dateByAddingMinutes:kRequestRetryMinutesInterval] timeIntervalSince1970]]; - NSPredicate *nextRetryTimeStampLessThanCurrentTimePredicate = [NSPredicate predicateWithFormat:@"nextRetryTimeStamp < %@", currentTimeStamp]; - [fetchRequest setPredicate:nextRetryTimeStampLessThanCurrentTimePredicate]; + NSPredicate *predicate = [NSPredicate predicateWithFormat:@"nextRetryTimeStamp < %@", currentTimeStamp]; + [fetchRequest setPredicate:predicate]; @try { if(context && [context isKindOfClass:[NSManagedObjectContext class]]) { [context performBlock:^{ - NSError *error; - NSArray *results = [[NSArray alloc]init]; - results = [context executeFetchRequest:fetchRequest error:&error]; - if (results && results.count > 0) { - handler(YES, results); - } else { + @try { + NSError *error; + NSArray *results = [[NSArray alloc]init]; + results = [context executeFetchRequest:fetchRequest error:&error]; + if (results && results.count > 0) { + handler(YES, results); + } else { + handler(NO, nil); + } + } @catch (NSException *exception) { + [BlueshiftLog logException:exception withDescription:nil methodName:[NSString stringWithUTF8String:__PRETTY_FUNCTION__]]; handler(NO, nil); } }]; @@ -124,6 +135,7 @@ + (void *)fetchBatchesFromCoreDataFromContext:(NSManagedObjectContext*) context } @catch (NSException *exception) { [BlueshiftLog logException:exception withDescription:nil methodName:[NSString stringWithUTF8String:__PRETTY_FUNCTION__]]; + handler(NO, nil); } } @@ -140,17 +152,21 @@ + (void)eraseEntityData { NSBatchDeleteRequest *deleteRequest = [[NSBatchDeleteRequest alloc] initWithFetchRequest:fetchRequest]; if([batchContext isKindOfClass:[NSManagedObjectContext class]]) { [batchContext performBlock:^{ - NSError *error = nil; - // check if there are any changes to be saved and save it - if ([batchContext hasChanges]) { + @try { + NSError *error = nil; + // check if there are any changes to be saved and save it + if ([batchContext hasChanges]) { + [batchContext save:&error]; + } + NSBatchDeleteResult* deleteResult = [batchContext executeRequest:deleteRequest error:&error]; [batchContext save:&error]; - } - NSBatchDeleteResult* deleteResult = [batchContext executeRequest:deleteRequest error:&error]; - [batchContext save:&error]; - if (error) { - [BlueshiftLog logError:error withDescription:@"Failed to save the data after deleting events." methodName:[NSString stringWithUTF8String:__PRETTY_FUNCTION__]]; - } else { - [BlueshiftLog logInfo:[NSString stringWithFormat:@"Deleted %@ records from the BatchEventEntity entity", deleteResult.result] withDetails:nil methodName:nil]; + if (error) { + [BlueshiftLog logError:error withDescription:@"Failed to save the data after deleting events." methodName:[NSString stringWithUTF8String:__PRETTY_FUNCTION__]]; + } else { + [BlueshiftLog logInfo:[NSString stringWithFormat:@"Deleted %@ records from the BatchEventEntity entity", deleteResult.result] withDetails:nil methodName:nil]; + } + } @catch (NSException *exception) { + [BlueshiftLog logException:exception withDescription:nil methodName:[NSString stringWithUTF8String:__PRETTY_FUNCTION__]]; } }]; } diff --git a/BlueShift-iOS-SDK/BlueShiftAppDelegate.m b/BlueShift-iOS-SDK/BlueShiftAppDelegate.m index 14533983..2a394af7 100755 --- a/BlueShift-iOS-SDK/BlueShiftAppDelegate.m +++ b/BlueShift-iOS-SDK/BlueShiftAppDelegate.m @@ -1129,30 +1129,36 @@ - (NSManagedObjectModel *)managedObjectModel { - (NSPersistentStoreCoordinator *)persistentStoreCoordinator { // The persistent store coordinator for the application. This implementation creates and return a coordinator, having added the store for the application to it. - if (_persistentStoreCoordinator != nil) { + NSString* key = [NSString stringWithUTF8String:__PRETTY_FUNCTION__]; + @synchronized (key) { + @try { + if (_persistentStoreCoordinator != nil) { + return _persistentStoreCoordinator; + } + + // Create the coordinator and store + _persistentStoreCoordinator = [[NSPersistentStoreCoordinator alloc] initWithManagedObjectModel:[self managedObjectModel]]; + NSURL *storeURL = [[self applicationDocumentsDirectory] URLByAppendingPathComponent:@"BlueShift-iOS-SDK.sqlite"]; + NSError *error = nil; + NSDictionary *options = @{NSMigratePersistentStoresAutomaticallyOption: @YES, NSInferMappingModelAutomaticallyOption: @YES}; + NSString *failureReason = @"There was an error creating or loading the application's saved data."; + if (![_persistentStoreCoordinator addPersistentStoreWithType:NSSQLiteStoreType configuration:nil URL:storeURL options:options error:&error]) { + // Report any error we got. + NSMutableDictionary *dict = [NSMutableDictionary dictionary]; + dict[NSLocalizedDescriptionKey] = @"Failed to initialize the application's saved data"; + dict[NSLocalizedFailureReasonErrorKey] = failureReason; + dict[NSUnderlyingErrorKey] = error; + error = [NSError errorWithDomain:@"YOUR_ERROR_DOMAIN" code:9999 userInfo:dict]; + // Replace this with code to handle the error appropriately. + // abort() causes the application to generate a crash log and terminate. You should not use this function in a shipping application, although it may be useful during development. + [BlueshiftLog logError:error withDescription:@"Unresolved error while creating persistent store coordinator" methodName:[NSString stringWithUTF8String:__PRETTY_FUNCTION__]]; + } + } @catch (NSException *exception) { + [BlueshiftLog logException:exception withDescription:nil methodName:[NSString stringWithUTF8String:__PRETTY_FUNCTION__]]; + } + return _persistentStoreCoordinator; } - - // Create the coordinator and store - - _persistentStoreCoordinator = [[NSPersistentStoreCoordinator alloc] initWithManagedObjectModel:[self managedObjectModel]]; - NSURL *storeURL = [[self applicationDocumentsDirectory] URLByAppendingPathComponent:@"BlueShift-iOS-SDK.sqlite"]; - NSError *error = nil; - NSDictionary *options = @{NSMigratePersistentStoresAutomaticallyOption: @YES, NSInferMappingModelAutomaticallyOption: @YES}; - NSString *failureReason = @"There was an error creating or loading the application's saved data."; - if (![_persistentStoreCoordinator addPersistentStoreWithType:NSSQLiteStoreType configuration:nil URL:storeURL options:options error:&error]) { - // Report any error we got. - NSMutableDictionary *dict = [NSMutableDictionary dictionary]; - dict[NSLocalizedDescriptionKey] = @"Failed to initialize the application's saved data"; - dict[NSLocalizedFailureReasonErrorKey] = failureReason; - dict[NSUnderlyingErrorKey] = error; - error = [NSError errorWithDomain:@"YOUR_ERROR_DOMAIN" code:9999 userInfo:dict]; - // Replace this with code to handle the error appropriately. - // abort() causes the application to generate a crash log and terminate. You should not use this function in a shipping application, although it may be useful during development. - [BlueshiftLog logError:error withDescription:@"Unresolved error" methodName:[NSString stringWithUTF8String:__PRETTY_FUNCTION__]]; - } - - return _persistentStoreCoordinator; } - (NSManagedObjectContext *)managedObjectContext { diff --git a/BlueShift-iOS-SDK/BlueShiftDeviceData.m b/BlueShift-iOS-SDK/BlueShiftDeviceData.m index 0f103e3b..fa1b5c02 100644 --- a/BlueShift-iOS-SDK/BlueShiftDeviceData.m +++ b/BlueShift-iOS-SDK/BlueShiftDeviceData.m @@ -79,9 +79,14 @@ - (NSString *)deviceType { } - (NSString *)networkCarrierName { - CTTelephonyNetworkInfo *netinfo = [[CTTelephonyNetworkInfo alloc] init]; - CTCarrier *carrier = [netinfo subscriberCellularProvider]; - return [carrier carrierName]; + // Skip fetching network carrier for simulator + #if TARGET_OS_SIMULATOR + return nil; + #else + CTTelephonyNetworkInfo *netinfo = [[CTTelephonyNetworkInfo alloc] init]; + CTCarrier *carrier = [netinfo subscriberCellularProvider]; + return [carrier carrierName]; + #endif } - (NSString *)operatingSystem { diff --git a/BlueShift-iOS-SDK/BlueShiftHttpRequestBatchUpload.m b/BlueShift-iOS-SDK/BlueShiftHttpRequestBatchUpload.m index 9b955c4f..ef59755e 100644 --- a/BlueShift-iOS-SDK/BlueShiftHttpRequestBatchUpload.m +++ b/BlueShift-iOS-SDK/BlueShiftHttpRequestBatchUpload.m @@ -119,25 +119,32 @@ + (void)createBatchesWithContext: (NSManagedObjectContext*)masterContext withBat } } [self createBatch:paramsArray]; - } - } - @try { - if (context && [context isKindOfClass:[NSManagedObjectContext class]]) { - [context performBlock:^{ - NSError *saveError = nil; - [context save:&saveError]; - [masterContext performBlock:^{ - NSError *saveError = nil; - if (masterContext && [masterContext isKindOfClass:[NSManagedObjectContext class]]) { - [masterContext save:&saveError]; + if (context && [context isKindOfClass:[NSManagedObjectContext class]]) { + [context performBlockAndWait:^{ + @try { + NSError *saveError = nil; + if ([context hasChanges]) { + [context save:&saveError]; + } + [masterContext performBlockAndWait:^{ + @try { + NSError *saveError = nil; + if (masterContext && [masterContext isKindOfClass:[NSManagedObjectContext class]]) { + [masterContext save:&saveError]; + } + } + @catch (NSException *exception) { + [BlueshiftLog logException:exception withDescription:nil methodName:[NSString stringWithUTF8String:__PRETTY_FUNCTION__]]; + } + }]; + } + @catch (NSException *exception) { + [BlueshiftLog logException:exception withDescription:nil methodName:[NSString stringWithUTF8String:__PRETTY_FUNCTION__]]; } }]; - }]; + } } } - @catch (NSException *exception) { - [BlueshiftLog logException:exception withDescription:nil methodName:[NSString stringWithUTF8String:__PRETTY_FUNCTION__]]; - } } @@ -216,21 +223,31 @@ + (void) deleteBatchRecords:(BatchEventEntity *)batchEvent context:(NSManagedObj @try { if(context && [context isKindOfClass:[NSManagedObjectContext class]]) { [context performBlock:^{ - [context deleteObject:batchEvent]; - [context performBlock:^{ + @try { + [context deleteObject:batchEvent]; NSError *saveError = nil; - if(context && [context isKindOfClass:[NSManagedObjectContext class]]) { + if(context) { [context save:&saveError]; } _requestQueueStatus = BlueShiftRequestQueueStatusAvailable; - }]; + handler(YES); + } @catch (NSException *exception) { + [BlueshiftLog logException:exception withDescription:nil methodName:[NSString stringWithUTF8String:__PRETTY_FUNCTION__]]; + _requestQueueStatus = BlueShiftRequestQueueStatusAvailable; + handler(NO); + } }]; } + else { + _requestQueueStatus = BlueShiftRequestQueueStatusAvailable; + handler(NO); + } } @catch (NSException *exception) { [BlueshiftLog logException:exception withDescription:nil methodName:[NSString stringWithUTF8String:__PRETTY_FUNCTION__]]; + _requestQueueStatus = BlueShiftRequestQueueStatusAvailable; + handler(NO); } - handler(YES); } + (void) handleRetryBatchUpload:(BatchEventEntity *)batchEvent context:(NSManagedObjectContext*)context requestOperation: (BlueShiftBatchRequestOperation*)requestOperation completetionHandler:(void (^)(BOOL))handler { @@ -238,10 +255,10 @@ + (void) handleRetryBatchUpload:(BatchEventEntity *)batchEvent context:(NSManage @try { if(context && [context isKindOfClass:[NSManagedObjectContext class]]) { [context performBlock:^{ - [context deleteObject:batchEvent]; - [context performBlock:^{ + @try { + [context deleteObject:batchEvent]; NSError *saveError = nil; - if(context && [context isKindOfClass:[NSManagedObjectContext class]]) { + if(context) { [context save:&saveError]; NSInteger retryAttemptsCount = requestOperation.retryAttemptsCount; requestOperation.retryAttemptsCount = retryAttemptsCount - 1; @@ -254,14 +271,23 @@ + (void) handleRetryBatchUpload:(BatchEventEntity *)batchEvent context:(NSManage } } _requestQueueStatus = BlueShiftRequestQueueStatusAvailable; - }]; + handler(YES); + } @catch (NSException *exception) { + [BlueshiftLog logException:exception withDescription:nil methodName:[NSString stringWithUTF8String:__PRETTY_FUNCTION__]]; + _requestQueueStatus = BlueShiftRequestQueueStatusAvailable; + handler(NO); + } }]; + } else { + _requestQueueStatus = BlueShiftRequestQueueStatusAvailable; + handler(NO); } } @catch (NSException *exception) { [BlueshiftLog logException:exception withDescription:nil methodName:[NSString stringWithUTF8String:__PRETTY_FUNCTION__]]; + _requestQueueStatus = BlueShiftRequestQueueStatusAvailable; + handler(NO); } - handler(NO); } diff --git a/BlueShift-iOS-SDK/BlueShiftNotificationConstants.h b/BlueShift-iOS-SDK/BlueShiftNotificationConstants.h index 70c49385..6dc01457 100755 --- a/BlueShift-iOS-SDK/BlueShiftNotificationConstants.h +++ b/BlueShift-iOS-SDK/BlueShiftNotificationConstants.h @@ -78,4 +78,15 @@ #define kBsftEncodedSpace @"%20" +#define kInAppTriggerModeNow @"now" +#define kInAppTriggerModeUpcoming @"upcoming" +#define kInAppTriggerModeEvent @"event" +#define kInAppTriggerModeNowAndUpcoming @"NowAndUpComing" + +#define kInAppPriorityMedium @"medium" +#define kInAppId @"id" + +#define kInAppStatus @"status" +#define kInAppStatusPending @"pending" + #endif diff --git a/BlueShift-iOS-SDK/BlueShiftRequestQueue.h b/BlueShift-iOS-SDK/BlueShiftRequestQueue.h index 949abdaf..ec29aec3 100644 --- a/BlueShift-iOS-SDK/BlueShiftRequestQueue.h +++ b/BlueShift-iOS-SDK/BlueShiftRequestQueue.h @@ -40,7 +40,4 @@ // Method to add Batch Request Operation to Queue .... + (void)addBatchRequestOperation:(BlueShiftBatchRequestOperation *)requestOperation; -// Method to set the request queue status explicity ... -+ (void)setRequestQueueStatus:(BlueShiftRequestQueueStatus)requestQueueStatus; - @end diff --git a/BlueShift-iOS-SDK/BlueShiftRequestQueue.m b/BlueShift-iOS-SDK/BlueShiftRequestQueue.m index 8e9e07d8..f4c070c9 100644 --- a/BlueShift-iOS-SDK/BlueShiftRequestQueue.m +++ b/BlueShift-iOS-SDK/BlueShiftRequestQueue.m @@ -160,35 +160,33 @@ + (void)processRequestsWithContext:(NSManagedObjectContext *)context forEntity:( [BlueShiftRequestQueue retryProcessRequestWithContext:context requestOperation:requestOperation forEntity:operationEntityToBeExecuted]; } }]; - } - else { + } else { @try { if(context && [context isKindOfClass:[NSManagedObjectContext class]]) { [context performBlock:^{ - [context deleteObject:operationEntityToBeExecuted]; - [context performBlock:^{ + @try { + [context deleteObject:operationEntityToBeExecuted]; NSError *saveError = nil; if(context && [context isKindOfClass:[NSManagedObjectContext class]]) { [context save:&saveError]; } - _requestQueueStatus = BlueShiftRequestQueueStatusAvailable; - [self processRequestsInQueue]; - }]; + [self setRequestQueueAvailableAndProcessRequestQueue]; + } @catch (NSException *exception) { + [BlueshiftLog logException:exception withDescription:nil methodName:[NSString stringWithUTF8String:__PRETTY_FUNCTION__]]; + [self setRequestQueueAvailableAndProcessRequestQueue]; + } }]; } else { - _requestQueueStatus = BlueShiftRequestQueueStatusAvailable; - [self processRequestsInQueue]; + [self setRequestQueueAvailableAndProcessRequestQueue]; } } @catch (NSException *exception) { [BlueshiftLog logException:exception withDescription:nil methodName:[NSString stringWithUTF8String:__PRETTY_FUNCTION__]]; - _requestQueueStatus = BlueShiftRequestQueueStatusAvailable; - [self processRequestsInQueue]; + [self setRequestQueueAvailableAndProcessRequestQueue]; } } } else { - _requestQueueStatus = BlueShiftRequestQueueStatusAvailable; - [self processRequestsInQueue]; + [self setRequestQueueAvailableAndProcessRequestQueue]; } } @@ -197,23 +195,25 @@ + (void)deleteRecords:(NSManagedObjectContext *)context forEntity:(HttpRequestOp @try { if(context && [context isKindOfClass:[NSManagedObjectContext class]]) { [context performBlock:^{ - [context deleteObject:operationEntityToBeExecuted]; - [context performBlock:^{ + @try { NSError *saveError = nil; - if(context && [context isKindOfClass:[NSManagedObjectContext class]]) { + if(context && [context respondsToSelector:@selector(deleteObject:)]) { + [context deleteObject:operationEntityToBeExecuted]; [context save:&saveError]; } - _requestQueueStatus = BlueShiftRequestQueueStatusAvailable; - [self processRequestsInQueue]; - }]; + [self setRequestQueueAvailableAndProcessRequestQueue]; + } @catch (NSException *exception) { + [BlueshiftLog logException:exception withDescription:nil methodName:[NSString stringWithUTF8String:__PRETTY_FUNCTION__]]; + [self setRequestQueueAvailableAndProcessRequestQueue]; + } }]; } else { - [self processRequestsInQueue]; + [self setRequestQueueAvailableAndProcessRequestQueue]; } } @catch (NSException *exception) { [BlueshiftLog logException:exception withDescription:nil methodName:[NSString stringWithUTF8String:__PRETTY_FUNCTION__]]; - [self processRequestsInQueue]; + [self setRequestQueueAvailableAndProcessRequestQueue]; } } @@ -226,26 +226,28 @@ + (void)retryProcessRequestWithContext:(NSManagedObjectContext *)context request @try { if(context && [context isKindOfClass:[NSManagedObjectContext class]]) { [context performBlock:^{ - [context deleteObject:operationEntityToBeExecuted]; - [context performBlock:^{ + @try { NSError *saveError = nil; - if(context && [context isKindOfClass:[NSManagedObjectContext class]]) { + if(context) { + [context deleteObject:operationEntityToBeExecuted]; [context save:&saveError]; } _requestQueueStatus = BlueShiftRequestQueueStatusAvailable; - - // request record is removed successfully from core data ... + // request record is removed successfully from core data. if (requestOperation.retryAttemptsCount > 0) { [BlueShiftRequestQueue addRequestOperation:requestOperation]; } [self processRequestsInQueue]; - }]; + } @catch (NSException *exception) { + [BlueshiftLog logException:exception withDescription:nil methodName:[NSString stringWithUTF8String:__PRETTY_FUNCTION__]]; + [self setRequestQueueAvailableAndProcessRequestQueue]; + } }]; } } @catch (NSException *exception) { [BlueshiftLog logException:exception withDescription:nil methodName:[NSString stringWithUTF8String:__PRETTY_FUNCTION__]]; - [self processRequestsInQueue]; + [self setRequestQueueAvailableAndProcessRequestQueue]; } } @@ -274,18 +276,14 @@ + (void)processRequestsInQueue { } }]; } else { - _requestQueueStatus = BlueShiftRequestQueueStatusAvailable; - [self processRequestsInQueue]; + [self setRequestQueueAvailableAndProcessRequestQueue]; } } } - -// Method to set the request queue status explicity ... -// Meant to be used by other classes ... -+ (void)setRequestQueueStatus:(BlueShiftRequestQueueStatus)requestQueueStatus { - _requestQueueStatus = requestQueueStatus; ++ (void)setRequestQueueAvailableAndProcessRequestQueue { + _requestQueueStatus = BlueShiftRequestQueueStatusAvailable; + [self processRequestsInQueue]; } - @end diff --git a/BlueShift-iOS-SDK/HttpRequestOperationEntity.m b/BlueShift-iOS-SDK/HttpRequestOperationEntity.m index 733c5eb8..57c1b98d 100644 --- a/BlueShift-iOS-SDK/HttpRequestOperationEntity.m +++ b/BlueShift-iOS-SDK/HttpRequestOperationEntity.m @@ -55,9 +55,9 @@ - (void)insertEntryWithMethod:(BlueShiftHTTPMethod)httpMethod andParameters:(NSD if(context && [context isKindOfClass:[NSManagedObjectContext class]]) { [context performBlock:^{ NSError *error = nil; - [context save:&error]; - if(masterContext && [masterContext isKindOfClass:[NSManagedObjectContext class]]) { - @try { + @try { + [context save:&error]; + if(masterContext && [masterContext isKindOfClass:[NSManagedObjectContext class]]) { [masterContext performBlock:^{ @try { NSError *error = nil; @@ -66,9 +66,9 @@ - (void)insertEntryWithMethod:(BlueShiftHTTPMethod)httpMethod andParameters:(NSD [BlueshiftLog logException:exception withDescription:nil methodName:[NSString stringWithUTF8String:__PRETTY_FUNCTION__]]; } }]; - } @catch (NSException *exception) { - [BlueshiftLog logException:exception withDescription:nil methodName:[NSString stringWithUTF8String:__PRETTY_FUNCTION__]]; } + } @catch (NSException *exception) { + [BlueshiftLog logException:exception withDescription:nil methodName:[NSString stringWithUTF8String:__PRETTY_FUNCTION__]]; } }]; } @@ -108,19 +108,26 @@ + (void *)fetchFirstRecordFromCoreDataWithCompletetionHandler:(void (^)(BOOL, Ht } if(fetchRequest.entity != nil) { NSNumber *currentTimeStamp = [NSNumber numberWithDouble:[[NSDate date] timeIntervalSinceReferenceDate] ]; - NSPredicate *nextRetryTimeStampLessThanCurrentTimePredicate = [NSPredicate predicateWithFormat:@"nextRetryTimeStamp < %@ && isBatchEvent == NO", currentTimeStamp]; - [fetchRequest setPredicate:nextRetryTimeStampLessThanCurrentTimePredicate]; + NSPredicate *predicate = [NSPredicate predicateWithFormat:@"nextRetryTimeStamp < %@ && isBatchEvent == NO", currentTimeStamp]; + [fetchRequest setPredicate:predicate]; [fetchRequest setFetchLimit:1]; @try { - if(context && [context isKindOfClass:[NSManagedObjectContext class]]) { + if(context && [context respondsToSelector:@selector(save:)]) { [context performBlock:^{ - NSArray *results = [[NSArray alloc]init]; - NSError *error; - results = [context executeFetchRequest:fetchRequest error:&error]; - if(results.count > 0) { - HttpRequestOperationEntity *operationEntityToBeExecuted = (HttpRequestOperationEntity *)[results firstObject]; - handler(YES, operationEntityToBeExecuted); - } else { + @try { + NSError *error; + if ([context hasChanges]) { + [context save:&error]; + } + NSArray *results = [context executeFetchRequest:fetchRequest error:&error]; + if(results.count > 0) { + HttpRequestOperationEntity *operationEntityToBeExecuted = (HttpRequestOperationEntity *)[results firstObject]; + handler(YES, operationEntityToBeExecuted); + } else { + handler(NO, nil); + } + } @catch (NSException *exception) { + [BlueshiftLog logException:exception withDescription:nil methodName:[NSString stringWithUTF8String:__PRETTY_FUNCTION__]]; handler(NO, nil); } }]; @@ -130,8 +137,13 @@ + (void *)fetchFirstRecordFromCoreDataWithCompletetionHandler:(void (^)(BOOL, Ht } @catch (NSException *exception) { [BlueshiftLog logException:exception withDescription:nil methodName:[NSString stringWithUTF8String:__PRETTY_FUNCTION__]]; + handler(NO, nil); } + } else { + handler(NO, nil); } + } else { + handler(NO, nil); } } else { handler(NO, nil); @@ -157,17 +169,22 @@ + (void *)fetchBatchWiseRecordFromCoreDataWithCompletetionHandler:(void (^)(BOOL } if(fetchRequest.entity != nil) { NSNumber *currentTimeStamp = [NSNumber numberWithDouble:[[[NSDate date] dateByAddingMinutes:kRequestRetryMinutesInterval] timeIntervalSince1970]]; - NSPredicate *nextRetryTimeStampLessThanCurrentTimePredicate = [NSPredicate predicateWithFormat:@"nextRetryTimeStamp < %@ && isBatchEvent == YES", currentTimeStamp]; - [fetchRequest setPredicate:nextRetryTimeStampLessThanCurrentTimePredicate]; + NSPredicate *predicate = [NSPredicate predicateWithFormat:@"nextRetryTimeStamp < %@ && isBatchEvent == YES", currentTimeStamp]; + [fetchRequest setPredicate:predicate]; @try { if(context && [context isKindOfClass:[NSManagedObjectContext class]]) { [context performBlock:^{ - NSError *error; - NSArray *results = [[NSArray alloc]init]; - results = [context executeFetchRequest:fetchRequest error:&error]; - if (results && results.count > 0) { - handler(YES, results); - } else { + @try { + NSError *error; + NSArray *results = [[NSArray alloc]init]; + results = [context executeFetchRequest:fetchRequest error:&error]; + if (results && results.count > 0) { + handler(YES, results); + } else { + handler(NO, nil); + } + } @catch (NSException *exception) { + [BlueshiftLog logException:exception withDescription:nil methodName:[NSString stringWithUTF8String:__PRETTY_FUNCTION__]]; handler(NO, nil); } }]; @@ -175,6 +192,7 @@ + (void *)fetchBatchWiseRecordFromCoreDataWithCompletetionHandler:(void (^)(BOOL } @catch (NSException *exception) { [BlueshiftLog logException:exception withDescription:nil methodName:[NSString stringWithUTF8String:__PRETTY_FUNCTION__]]; + handler(NO, nil); } } else { handler(NO, nil); @@ -204,21 +222,26 @@ + (void)eraseEntityData { [deleteRequest setResultType:NSBatchDeleteResultTypeCount]; if([realtimeContext isKindOfClass:[NSManagedObjectContext class]]) { [realtimeContext performBlock:^{ - NSError *error = nil; - // check if there are any changes for realtime events to be saved and save it - if ([realtimeContext hasChanges]) { + @try { + NSError *error = nil; + // check if there are any changes for realtime events to be saved and save it + if ([realtimeContext hasChanges]) { + [realtimeContext save:&error]; + } + // check if there are any changes for batched events to be saved and save it + if ([batchContext isKindOfClass:[NSManagedObjectContext class]] && [batchContext hasChanges]) { + [batchContext save:&error]; + } + NSBatchDeleteResult* deleteResult = [realtimeContext executeRequest:deleteRequest error:&error]; [realtimeContext save:&error]; - } - // check if there are any changes for batched events to be saved and save it - if ([batchContext isKindOfClass:[NSManagedObjectContext class]] && [batchContext hasChanges]) { - [batchContext save:&error]; - } - NSBatchDeleteResult* deleteResult = [realtimeContext executeRequest:deleteRequest error:&error]; - [realtimeContext save:&error]; - if (error) { - [BlueshiftLog logError:error withDescription:@"Failed to save the data after deleting events." methodName:[NSString stringWithUTF8String:__PRETTY_FUNCTION__]]; - } else { - [BlueshiftLog logInfo:[NSString stringWithFormat:@"Deleted %@ records from the HttpRequestOperationEntity entity", deleteResult.result] withDetails:nil methodName:nil]; + if (error) { + [BlueshiftLog logError:error withDescription:@"Failed to save the data after deleting events." methodName:[NSString stringWithUTF8String:__PRETTY_FUNCTION__]]; + } else { + [BlueshiftLog logInfo:[NSString stringWithFormat:@"Deleted %@ records from the HttpRequestOperationEntity entity", deleteResult.result] withDetails:nil methodName:nil]; + } + + } @catch (NSException *exception) { + [BlueshiftLog logException:exception withDescription:nil methodName:[NSString stringWithUTF8String:__PRETTY_FUNCTION__]]; } }]; } diff --git a/BlueShift-iOS-SDK/InApps/BlueShiftInAppNotificationManager.m b/BlueShift-iOS-SDK/InApps/BlueShiftInAppNotificationManager.m index 8d331f9d..666a805f 100755 --- a/BlueShift-iOS-SDK/InApps/BlueShiftInAppNotificationManager.m +++ b/BlueShift-iOS-SDK/InApps/BlueShiftInAppNotificationManager.m @@ -259,23 +259,25 @@ - (void)deleteNotification:(InAppNotificationEntity *)notification context:(NSMa NSManagedObject* pManagedObject = [context objectWithID: notification.objectID]; @try { [context deleteObject: pManagedObject]; - } - @catch (NSException *exception) { - [BlueshiftLog logException:exception withDescription:nil methodName:[NSString stringWithUTF8String:__PRETTY_FUNCTION__]]; - } - [context performBlock:^{ NSError *saveError = nil; - if(context && [context isKindOfClass:[NSManagedObjectContext class]]) { + if(context) { [context save:&saveError]; - if(masterContext && [masterContext isKindOfClass:[NSManagedObjectContext class]]) { [masterContext performBlock:^{ NSError *error = nil; - [masterContext save:&error]; + @try { + [masterContext save:&error]; + } + @catch (NSException *exception) { + [BlueshiftLog logException:exception withDescription:nil methodName:[NSString stringWithUTF8String:__PRETTY_FUNCTION__]]; + } }]; } } - }]; + } + @catch (NSException *exception) { + [BlueshiftLog logException:exception withDescription:nil methodName:[NSString stringWithUTF8String:__PRETTY_FUNCTION__]]; + } }]; } } @@ -294,31 +296,36 @@ - (void)fetchLastInAppMessageIDFromDB:(void (^)(BOOL, NSString *, NSString *))ha if (masterContext != nil) { NSEntityDescription *entity; - NSError *error; NSFetchRequest *fetchRequest = [[NSFetchRequest alloc] init]; @try { entity = [NSEntityDescription entityForName: kInAppNotificationEntityNameKey inManagedObjectContext:masterContext]; [fetchRequest setEntity:entity]; + if(entity != nil && fetchRequest.entity != nil) { + [masterContext performBlock:^{ + @try { + NSError *error; + NSArray *results = [masterContext executeFetchRequest: fetchRequest error:&error]; + NSArray *sortedList = [self sortedInAppMessageWithDate: results]; + if (sortedList != nil && [sortedList count] > 0 && sortedList[[sortedList count] - 1]) { + InAppNotificationEntity *notification = results[[sortedList count] -1]; + handler(YES, notification.id, notification.timestamp); + } else { + handler(NO, @"", @""); + } + } @catch (NSException *exception) { + [BlueshiftLog logException:exception withDescription:nil methodName:[NSString stringWithUTF8String:__PRETTY_FUNCTION__]]; + handler(NO, @"", @""); + } + }]; + } else { + handler(NO, @"", @""); + } } @catch (NSException *exception) { [BlueshiftLog logException:exception withDescription:nil methodName:[NSString stringWithUTF8String:__PRETTY_FUNCTION__]]; - } - - if(entity != nil && fetchRequest.entity != nil) { - NSArray *results = [masterContext executeFetchRequest: fetchRequest error:&error]; - NSArray *sortedList = [self sortedInAppMessageWithDate: results]; - if (sortedList != nil && [sortedList count] > 0 && sortedList[[sortedList count] - 1]) { - InAppNotificationEntity *notification = results[[sortedList count] -1]; - handler(YES, notification.id, notification.timestamp); - } else { - handler(YES, @"", @""); - } - } else { handler(NO, @"", @""); } } else { - NSError *error = (NSError *)@"Unable to fetch the lastTimestamp and notificationID from core data."; - [BlueshiftLog logError:error withDescription:nil methodName: [NSString stringWithUTF8String: __PRETTY_FUNCTION__]]; handler(NO, @"", @""); } } @@ -365,26 +372,26 @@ - (void)removeInAppNotificationFromDB:(NSManagedObjectID *) entityItem { if(context && [context isKindOfClass:[NSManagedObjectContext class]]) { [context performBlock:^{ NSManagedObject* pManagedObject = [context objectWithID: entityItem]; - @try { [context deleteObject: pManagedObject]; - } - @catch (NSException *exception) { - [BlueshiftLog logException:exception withDescription:nil methodName:[NSString stringWithUTF8String:__PRETTY_FUNCTION__]]; - } - [context performBlock:^{ NSError *saveError = nil; - if(context && [context isKindOfClass:[NSManagedObjectContext class]]) { + if(context) { [context save:&saveError]; - if(masterContext && [masterContext isKindOfClass:[NSManagedObjectContext class]]) { [masterContext performBlock:^{ - NSError *error = nil; - [masterContext save:&error]; + @try { + NSError *error = nil; + [masterContext save:&error]; + } @catch (NSException *exception) { + [BlueshiftLog logException:exception withDescription:nil methodName:[NSString stringWithUTF8String:__PRETTY_FUNCTION__]]; + } }]; } } - }]; + } + @catch (NSException *exception) { + [BlueshiftLog logException:exception withDescription:nil methodName:[NSString stringWithUTF8String:__PRETTY_FUNCTION__]]; + } }]; } } diff --git a/BlueShift-iOS-SDK/InApps/InAppNotificationEntity.m b/BlueShift-iOS-SDK/InApps/InAppNotificationEntity.m index 0d8115b7..259bd81a 100644 --- a/BlueShift-iOS-SDK/InApps/InAppNotificationEntity.m +++ b/BlueShift-iOS-SDK/InApps/InAppNotificationEntity.m @@ -31,9 +31,7 @@ - (void)fetchBy:(NSString *)key withValue:(NSString *)value { } + (void)fetchAll:(BlueShiftInAppTriggerMode)triggerMode forDisplayPage:(NSString *)displayOn context:(NSManagedObjectContext *)masterContext withHandler:(void (^)(BOOL, NSArray *))handler { - if (nil != masterContext) { - NSFetchRequest *fetchRequest = [[NSFetchRequest alloc] init]; @try { [fetchRequest setEntity:[NSEntityDescription entityForName: kInAppNotificationEntityNameKey inManagedObjectContext: masterContext]]; @@ -52,41 +50,42 @@ + (void)fetchAll:(BlueShiftInAppTriggerMode)triggerMode forDisplayPage:(NSString } + (void *)fetchFromCoreDataFromContext:(NSManagedObjectContext *)context forTriggerMode: (BlueShiftInAppTriggerMode) triggerMode forDisplayPage:(NSString *)displayOn request: (NSFetchRequest*)fetchRequest handler:(void (^)(BOOL, NSArray *))handler { - NSString* triggerStr; - switch (triggerMode) { case BlueShiftInAppTriggerNow: - triggerStr = @"now"; + triggerStr = kInAppTriggerModeNow; break; case BlueShiftInAppTriggerUpComing: - triggerStr = @"upcoming"; + triggerStr = kInAppTriggerModeUpcoming; break; case BlueShiftInAppTriggerEvent: - triggerStr = @"event"; + triggerStr = kInAppTriggerModeEvent; break; case BlueShiftInAppNoTriggerEvent: triggerStr = @""; break; case BlueShiftInAppTriggerNowAndUpComing: - triggerStr = @"NowAndUpComing"; + triggerStr = kInAppTriggerModeNowAndUpcoming; break; } displayOn = (displayOn ? displayOn: @""); - - NSPredicate *nextRetryTimeStampLessThanCurrentTimePredicate = [self getPredicates: triggerStr andDisplayOn: displayOn]; - [fetchRequest setPredicate:nextRetryTimeStampLessThanCurrentTimePredicate]; - + NSPredicate *predicate = [self getPredicates: triggerStr andDisplayOn: displayOn]; + [fetchRequest setPredicate:predicate]; @try { if(context && [context isKindOfClass:[NSManagedObjectContext class]]) { [context performBlock:^{ - NSError *error; - NSArray *results = [[NSArray alloc]init]; - results = [context executeFetchRequest:fetchRequest error:&error]; - if (results && results.count > 0) { - handler(YES, results); - } else { + @try { + NSError *error; + NSArray *results = [[NSArray alloc]init]; + results = [context executeFetchRequest:fetchRequest error:&error]; + if (results && results.count > 0) { + handler(YES, results); + } else { + handler(NO, nil); + } + } @catch (NSException *exception) { + [BlueshiftLog logException:exception withDescription:nil methodName:[NSString stringWithUTF8String:__PRETTY_FUNCTION__]]; handler(NO, nil); } }]; @@ -96,11 +95,12 @@ + (void *)fetchFromCoreDataFromContext:(NSManagedObjectContext *)context forTrig } @catch (NSException *exception) { [BlueshiftLog logException:exception withDescription:nil methodName:[NSString stringWithUTF8String:__PRETTY_FUNCTION__]]; + handler(NO, nil); } } + (NSPredicate *)getPredicates:(NSString *)triggerStr andDisplayOn:(NSString *)displayOn { - if ([triggerStr isEqualToString:@"NowAndUpComing"]) { + if ([triggerStr isEqualToString:kInAppTriggerModeNowAndUpcoming]) { return [NSPredicate predicateWithFormat:@"(triggerMode == %@ OR triggerMode == %@)AND status == %@ AND (displayOn == %@ OR displayOn == %@ OR displayOn == %@)", @"now",@"upcoming", @"pending", displayOn, @"", nil]; } else if (triggerStr && ![triggerStr isEqualToString: @""]) { return [NSPredicate predicateWithFormat:@"(triggerMode == %@ AND status == %@) AND (displayOn == %@ OR displayOn == %@ OR displayOn == %@)", triggerStr, @"pending", displayOn, @"", nil]; @@ -112,9 +112,7 @@ + (NSPredicate *)getPredicates:(NSString *)triggerStr andDisplayOn:(NSString *)d - (void) insert:(NSDictionary *)dictionary usingPrivateContext: (NSManagedObjectContext*)privateContext andMainContext: (NSManagedObjectContext*)masterContext handler:(void (^)(BOOL))handler { - if (nil != masterContext && nil != privateContext) { - NSManagedObjectContext *context = privateContext; context.parentContext = masterContext; // return if context is unavailable ... @@ -127,15 +125,25 @@ - (void) insert:(NSDictionary *)dictionary usingPrivateContext: (NSManagedObject @try { if(context && [context isKindOfClass:[NSManagedObjectContext class]]) { [context performBlock:^{ - NSError *error = nil; - [context save:&error]; - if(masterContext && [masterContext isKindOfClass:[NSManagedObjectContext class]]) { - [masterContext performBlock:^{ - NSError *error = nil; - [masterContext save:&error]; - handler(YES); - }]; - } else { + @try { + NSError *error = nil; + [context save:&error]; + if(masterContext && [masterContext isKindOfClass:[NSManagedObjectContext class]]) { + [masterContext performBlock:^{ + @try { + NSError *error = nil; + [masterContext save:&error]; + handler(YES); + } @catch (NSException *exception) { + [BlueshiftLog logException:exception withDescription:nil methodName:[NSString stringWithUTF8String:__PRETTY_FUNCTION__]]; + handler(NO); + } + }]; + } else { + handler(NO); + } + } @catch (NSException *exception) { + [BlueshiftLog logException:exception withDescription:nil methodName:[NSString stringWithUTF8String:__PRETTY_FUNCTION__]]; handler(NO); } }]; @@ -153,19 +161,22 @@ - (void) insert:(NSDictionary *)dictionary usingPrivateContext: (NSManagedObject } + (void)fetchNotificationByID :(NSManagedObjectContext *)context forNotificatioID: (NSString *) notificationID request: (NSFetchRequest*)fetchRequest handler:(void (^)(BOOL, NSArray *))handler{ - - NSPredicate *nextRetryTimeStampLessThanCurrentTimePredicate = [NSPredicate predicateWithFormat:@"id == %@", notificationID]; - [fetchRequest setPredicate:nextRetryTimeStampLessThanCurrentTimePredicate]; - + NSPredicate *predicate = [NSPredicate predicateWithFormat:@"id == %@", notificationID]; + [fetchRequest setPredicate:predicate]; @try { if(context && [context isKindOfClass:[NSManagedObjectContext class]]) { [context performBlock:^{ - NSError *error; - NSArray *results = [[NSArray alloc]init]; - results = [context executeFetchRequest:fetchRequest error:&error]; - if (results && results.count > 0) { - handler(YES, results); - } else { + @try { + NSError *error; + NSArray *results = [[NSArray alloc]init]; + results = [context executeFetchRequest:fetchRequest error:&error]; + if (results && results.count > 0) { + handler(YES, results); + } else { + handler(NO, nil); + } + } @catch (NSException *exception) { + [BlueshiftLog logException:exception withDescription:nil methodName:[NSString stringWithUTF8String:__PRETTY_FUNCTION__]]; handler(NO, nil); } }]; @@ -175,24 +186,28 @@ + (void)fetchNotificationByID :(NSManagedObjectContext *)context forNotificatioI } @catch (NSException *exception) { [BlueshiftLog logException:exception withDescription:nil methodName:[NSString stringWithUTF8String:__PRETTY_FUNCTION__]]; + handler(NO, nil); } } + (void)fetchInAppNotificationByStatus :(NSManagedObjectContext *)context forNotificatioID: (NSString *) status request: (NSFetchRequest*)fetchRequest handler:(void (^)(BOOL, NSArray *))handler { - - NSPredicate *nextRetryTimeStampLessThanCurrentTimePredicate = [NSPredicate predicateWithFormat:@"status == %@", status]; - [fetchRequest setPredicate:nextRetryTimeStampLessThanCurrentTimePredicate]; + NSPredicate *predicate = [NSPredicate predicateWithFormat:@"status == %@", status]; + [fetchRequest setPredicate:predicate]; [fetchRequest setFetchLimit: 10]; - @try { if(context && [context isKindOfClass:[NSManagedObjectContext class]]) { [context performBlock:^{ - NSError *error; - NSArray *results = [[NSArray alloc]init]; - results = [context executeFetchRequest:fetchRequest error:&error]; - if (results && results.count > 0) { - handler(YES, results); - } else { + @try { + NSError *error; + NSArray *results = [[NSArray alloc]init]; + results = [context executeFetchRequest:fetchRequest error:&error]; + if (results && results.count > 0) { + handler(YES, results); + } else { + handler(NO, nil); + } + } @catch (NSException *exception) { + [BlueshiftLog logException:exception withDescription:nil methodName:[NSString stringWithUTF8String:__PRETTY_FUNCTION__]]; handler(NO, nil); } }]; @@ -202,6 +217,7 @@ + (void)fetchInAppNotificationByStatus :(NSManagedObjectContext *)context forNot } @catch (NSException *exception) { [BlueshiftLog logException:exception withDescription:nil methodName:[NSString stringWithUTF8String:__PRETTY_FUNCTION__]]; + handler(NO, nil); } } @@ -211,27 +227,30 @@ + (void)updateInAppNotificationStatus:(NSManagedObjectContext *)context forNotif NSPredicate *predicate = [NSPredicate predicateWithFormat:@"id == %@", notificationID]; [fetchRequest setPredicate:predicate]; [fetchRequest setFetchLimit:1]; - NSError *error; - NSArray *arrResult = [context executeFetchRequest:fetchRequest error:&error]; - if (arrResult.count > 0) { - InAppNotificationEntity *entity = arrResult[0]; - [entity setValue: status forKey: @"status"]; - @try { - if(context && [context isKindOfClass:[NSManagedObjectContext class]]) { - [context performBlock:^{ - NSError *error = nil; - [context save:&error]; - handler(YES); - }]; - } else { - handler(NO); - } - } - @catch (NSException *exception) { - [BlueshiftLog logException:exception withDescription:nil methodName:[NSString stringWithUTF8String:__PRETTY_FUNCTION__]]; + @try { + if(context && [context isKindOfClass:[NSManagedObjectContext class]]) { + [context performBlock:^{ + @try { + NSError *error; + NSArray *arrResult = [context executeFetchRequest:fetchRequest error:&error]; + if (arrResult.count > 0) { + InAppNotificationEntity *entity = arrResult[0]; + [entity setValue: status forKey: kInAppStatus]; + error = nil; + [context save:&error]; + handler(YES); + } + } @catch (NSException *exception) { + [BlueshiftLog logException:exception withDescription:nil methodName:[NSString stringWithUTF8String:__PRETTY_FUNCTION__]]; + handler(NO); + } + }]; + } else { handler(NO); } - } else { + } + @catch (NSException *exception) { + [BlueshiftLog logException:exception withDescription:nil methodName:[NSString stringWithUTF8String:__PRETTY_FUNCTION__]]; handler(NO); } } else { @@ -239,77 +258,76 @@ + (void)updateInAppNotificationStatus:(NSManagedObjectContext *)context forNotif } } -- (void)delete { - -} - - (void)map:(NSDictionary *)dictionary { - - NSMutableDictionary *payload = [dictionary mutableCopy]; - if ([dictionary objectForKey: kInAppNotificationModalMessageUDIDKey] && - [dictionary objectForKey: kInAppNotificationModalMessageUDIDKey] != [NSNull null]) { - self.id =(NSString *)[dictionary objectForKey: kInAppNotificationModalMessageUDIDKey]; - } else { - self.id = [NSString stringWithFormat:@"%u",arc4random_uniform(99999)]; - [payload setValue:self.id forKey:@"id"]; - } - /* parse the payload and save the relevant keys related to presentation of In-App msg */ - - /* get in-app payload */ - if ([dictionary objectForKey: kSilentNotificationPayloadIdentifierKey]) { - dictionary = [dictionary objectForKey: kSilentNotificationPayloadIdentifierKey]; - } - - if ([dictionary objectForKey: kInAppNotificationModalMessageUDIDKey] && - [dictionary objectForKey: kInAppNotificationModalMessageUDIDKey] != [NSNull null]) { - self.id =(NSString *)[dictionary objectForKey: kInAppNotificationModalMessageUDIDKey]; - } - - if ([dictionary objectForKey: kInAppNotificationModalTimestampKey] && - [dictionary objectForKey: kInAppNotificationModalTimestampKey] != [NSNull null]) { - self.timestamp = (NSString *) [dictionary objectForKey: kInAppNotificationModalTimestampKey]; - } - - if ([dictionary objectForKey: kInAppNotificationKey]) { - dictionary = [dictionary objectForKey: kInAppNotificationKey]; - } - - /* get type of In-App msg */ - if ([dictionary objectForKey: kSilentNotificationPayloadTypeKey] && - [dictionary objectForKey: kSilentNotificationPayloadTypeKey] != [NSNull null]) { - self.type = [dictionary objectForKey: kSilentNotificationPayloadTypeKey]; - } - - if ([dictionary objectForKey: kInAppNotificationPayloadDisplayOnKey] && - [dictionary objectForKey: kInAppNotificationPayloadDisplayOnKey] != [NSNull null]) { - self.displayOn = [dictionary objectForKey: kInAppNotificationPayloadDisplayOnKey]; - } - - /* get start and end Time */ - if ([dictionary objectForKey: kSilentNotificationTriggerEndTimeKey] && - [dictionary objectForKey: kSilentNotificationTriggerEndTimeKey] != [NSNull null]) { - self.endTime = [NSNumber numberWithDouble: [[dictionary objectForKey: kSilentNotificationTriggerEndTimeKey] doubleValue]]; - } - - self.triggerMode = @"now"; - if ([dictionary objectForKey: kSilentNotificationTriggerKey]) { - NSString *trigger = (NSString *)[dictionary objectForKey: kSilentNotificationTriggerKey]; - if (![trigger isEqualToString:@""]) { - if ([BlueShiftInAppNotificationHelper hasDigits: trigger] == YES) { - self.triggerMode = @"upcoming"; - self.startTime = [NSNumber numberWithDouble: [trigger doubleValue]]; - } else { - self.triggerMode = trigger; + @try { + NSMutableDictionary *payload = [dictionary mutableCopy]; + if ([dictionary objectForKey: kInAppNotificationModalMessageUDIDKey] && + [dictionary objectForKey: kInAppNotificationModalMessageUDIDKey] != [NSNull null]) { + self.id =(NSString *)[dictionary objectForKey: kInAppNotificationModalMessageUDIDKey]; + } else { + self.id = [NSString stringWithFormat:@"%u",arc4random_uniform(99999)]; + [payload setValue:self.id forKey:kInAppId]; + } + /* parse the payload and save the relevant keys related to presentation of In-App msg */ + + /* get in-app payload */ + if ([dictionary objectForKey: kSilentNotificationPayloadIdentifierKey]) { + dictionary = [dictionary objectForKey: kSilentNotificationPayloadIdentifierKey]; + } + + if ([dictionary objectForKey: kInAppNotificationModalMessageUDIDKey] && + [dictionary objectForKey: kInAppNotificationModalMessageUDIDKey] != [NSNull null]) { + self.id =(NSString *)[dictionary objectForKey: kInAppNotificationModalMessageUDIDKey]; + } + + if ([dictionary objectForKey: kInAppNotificationModalTimestampKey] && + [dictionary objectForKey: kInAppNotificationModalTimestampKey] != [NSNull null]) { + self.timestamp = (NSString *) [dictionary objectForKey: kInAppNotificationModalTimestampKey]; + } + + if ([dictionary objectForKey: kInAppNotificationKey]) { + dictionary = [dictionary objectForKey: kInAppNotificationKey]; + } + + /* get type of In-App msg */ + if ([dictionary objectForKey: kSilentNotificationPayloadTypeKey] && + [dictionary objectForKey: kSilentNotificationPayloadTypeKey] != [NSNull null]) { + self.type = [dictionary objectForKey: kSilentNotificationPayloadTypeKey]; + } + + if ([dictionary objectForKey: kInAppNotificationPayloadDisplayOnKey] && + [dictionary objectForKey: kInAppNotificationPayloadDisplayOnKey] != [NSNull null]) { + self.displayOn = [dictionary objectForKey: kInAppNotificationPayloadDisplayOnKey]; + } + + /* get start and end Time */ + if ([dictionary objectForKey: kSilentNotificationTriggerEndTimeKey] && + [dictionary objectForKey: kSilentNotificationTriggerEndTimeKey] != [NSNull null]) { + self.endTime = [NSNumber numberWithDouble: [[dictionary objectForKey: kSilentNotificationTriggerEndTimeKey] doubleValue]]; + } + + self.triggerMode = kInAppTriggerModeNow; + if ([dictionary objectForKey: kSilentNotificationTriggerKey]) { + NSString *trigger = (NSString *)[dictionary objectForKey: kSilentNotificationTriggerKey]; + if (![trigger isEqualToString:@""]) { + if ([BlueShiftInAppNotificationHelper hasDigits: trigger] == YES) { + self.triggerMode = kInAppTriggerModeUpcoming; + self.startTime = [NSNumber numberWithDouble: [trigger doubleValue]]; + } else { + self.triggerMode = trigger; + } } } + + /* Other properties */ + self.priority = kInAppPriorityMedium; + self.eventName = @""; + self.status = kInAppStatusPending; + self.createdAt = [NSNumber numberWithDouble: (double)[[NSDate date] timeIntervalSince1970]]; + self.payload = [NSKeyedArchiver archivedDataWithRootObject:payload]; + } @catch (NSException *exception) { + [BlueshiftLog logException:exception withDescription:nil methodName:[NSString stringWithUTF8String:__PRETTY_FUNCTION__]]; } - - /* Other properties */ - self.priority = @"medium"; - self.eventName = @""; - self.status = @"pending"; - self.createdAt = [NSNumber numberWithDouble: (double)[[NSDate date] timeIntervalSince1970]]; - self.payload = [NSKeyedArchiver archivedDataWithRootObject:payload]; } @end