Skip to content

Commit

Permalink
Merge pull request #207 from blueshift-labs/fix/inApp-enhancements-2
Browse files Browse the repository at this point in the history
Fix/in app enhancements 2
  • Loading branch information
ketanshikharebsft committed Jun 14, 2021
2 parents 5e8be16 + 530bba4 commit 3ee760e
Show file tree
Hide file tree
Showing 16 changed files with 141 additions and 56 deletions.
2 changes: 2 additions & 0 deletions BlueShift-iOS-Extension-SDK/BlueShiftPushAnalytics.m
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,8 @@ + (void)sendPushAnalytics:(NSString *)type withParams:(NSDictionary *)userInfo {
[parameterMutableDictionary addEntriesFromDictionary:pushTrackParameterDictionary];
}
[parameterMutableDictionary setObject:type forKey:@"a"];
NSString *browserPlatform = [NSString stringWithFormat:@"%@ %@", kiOS, [[UIDevice currentDevice] systemVersion]];
[parameterMutableDictionary setObject:browserPlatform forKey:kBrowserPlatform];
NSString *url = [NSString stringWithFormat:@"%@%@", kBaseURL, kPushEventsUploadURL];
[self fireAPICallWithURL:url data:parameterMutableDictionary andRetryCount:3];
}
Expand Down
2 changes: 1 addition & 1 deletion BlueShift-iOS-Extension-SDK/BlueShiftPushNotification.m
Original file line number Diff line number Diff line change
Expand Up @@ -46,7 +46,7 @@ - (NSArray *)integratePushNotificationWithMediaAttachementsForRequest:(UNNotific
NSLog(@"[Blueshift] Error - Please set the api key in the Notification Service Extension, otherwise push notification delivered events will not reflect on the dashboard");
}

dispatch_async(dispatch_get_global_queue(DISPATCH_QUEUE_PRIORITY_DEFAULT, 0), ^{
dispatch_async(dispatch_get_global_queue(DISPATCH_QUEUE_PRIORITY_HIGH, 0), ^{
[self trackPushViewedWithRequest:request];
});

Expand Down
2 changes: 2 additions & 0 deletions BlueShift-iOS-Extension-SDK/BlueshiftExtensionConstants.h
Original file line number Diff line number Diff line change
Expand Up @@ -51,5 +51,7 @@

#define kAppName @"app_name"
#define kDeviceID @"device_id"
#define kBrowserPlatform @"browser_platform"
#define kiOS @"iOS"

#endif
7 changes: 5 additions & 2 deletions BlueShift-iOS-SDK/BlueShift.m
Original file line number Diff line number Diff line change
Expand Up @@ -110,10 +110,11 @@ - (void) setupWithConfiguration:(BlueShiftConfig *)config {
}
if (config.enablePushNotification == YES) {
[blueShiftAppDelegate registerForNotification];
[blueShiftAppDelegate handleRemoteNotificationOnLaunchWithLaunchOptions:config.applicationLaunchOptions];
} else if (config.enableSilentPushNotification == YES) {
[blueShiftAppDelegate registerForSilentPushNotification];
}
[blueShiftAppDelegate handleRemoteNotificationOnLaunchWithLaunchOptions:config.applicationLaunchOptions];

// Initialize In App Manager
_inAppNotificationMananger = [[BlueShiftInAppNotificationManager alloc] init];
if (config.inAppNotificationDelegate) {
Expand Down Expand Up @@ -666,7 +667,9 @@ - (void)performRequestQueue:(NSMutableDictionary *)parameters canBatchThisEvent:
if([self validateSDKTrackingRequirements] == false) {
return;
}


parameters[kBrowserPlatform] = [BlueShiftDeviceData currentDeviceData].operatingSystem;

if (parameters != nil) {
NSString *url = [NSString stringWithFormat:@"%@%@", kBaseURL, kPushEventsUploadURL];
BlueShiftRequestOperation *requestOperation = [[BlueShiftRequestOperation alloc] initWithRequestURL:url andHttpMethod:BlueShiftHTTPMethodGET andParameters:[parameters copy] andRetryAttemptsCount:kRequestTryMaximumLimit andNextRetryTimeStamp:0 andIsBatchEvent:isBatchEvent];
Expand Down
27 changes: 20 additions & 7 deletions BlueShift-iOS-SDK/BlueShiftAppDelegate.m
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,9 @@

#define SYSTEM_VERSION_GRATERTHAN_OR_EQUALTO(v) ([[[UIDevice currentDevice] systemVersion] compare:v options:NSNumericSearch] != NSOrderedAscending)

@implementation BlueShiftAppDelegate
@implementation BlueShiftAppDelegate {
NSString *lastProcessedPushNotificationUUID;
}

- (id) init {
self = [super init];
Expand Down Expand Up @@ -78,12 +80,11 @@ - (void)registerForSilentPushNotification {

// Handles the push notification payload when the app is killed and lauched from push notification tray ...
- (BOOL)handleRemoteNotificationOnLaunchWithLaunchOptions:(NSDictionary *)launchOptions {
NSDictionary *userInfo = [launchOptions objectForKey:UIApplicationLaunchOptionsRemoteNotificationKey];

if (userInfo) {
// Handling the push notification if we get the userInfo from launchOptions ...
// It's the only way to track notification payload while app is on launch (i.e after the app is killed) ...
[self handleRemoteNotification:userInfo];
if (launchOptions) {
NSDictionary *userInfo = [launchOptions objectForKey:UIApplicationLaunchOptionsRemoteNotificationKey];
if (userInfo) {
[self handleRemoteNotification:userInfo];
}
}

return YES;
Expand Down Expand Up @@ -378,7 +379,12 @@ - (void)handleRemoteNotification:(NSDictionary *)userInfo {
if ([BlueshiftEventAnalyticsHelper isSilenPushNotificationPayload: userInfo]) {
[[BlueShift sharedInstance] handleSilentPushNotification: userInfo forApplicationState: UIApplicationStateActive];
} else {
NSString *pushUUID = [userInfo valueForKey:kInAppNotificationModalMessageUDIDKey];
NSString *pushCategory = [[userInfo objectForKey: kNotificationAPSIdentifierKey] objectForKey: kNotificationCategoryIdentifierKey];
if ([pushCategory isEqualToString:kNotificationCategorySilentPushIdentifier] || [pushUUID isEqualToString:lastProcessedPushNotificationUUID]) {
[BlueshiftLog logInfo:@"Skipped processing notification due to one of the following reasons." withDetails:@"1. The push notification is silent push notification 2. The push notification click is already processed." methodName:nil];
return;
}
self.pushAlertDictionary = [userInfo objectForKey: kNotificationAPSIdentifierKey];
self.userInfo = userInfo;
NSDictionary *pushTrackParameterDictionary = [BlueshiftEventAnalyticsHelper pushTrackParameterDictionaryForPushDetailsDictionary:userInfo];
Expand Down Expand Up @@ -416,6 +422,8 @@ - (void)setupPushNotificationDeeplink:(NSDictionary *)userInfo {
[[[BlueShift sharedInstance].config blueShiftPushDelegate] pushNotificationDidClick:userInfo];
}

lastProcessedPushNotificationUUID = [userInfo valueForKey:kInAppNotificationModalMessageUDIDKey];

[self trackAppOpenWithParameters:userInfo];

if (userInfo != nil && ([userInfo objectForKey: kPushNotificationDeepLinkURLKey] || [userInfo objectForKey: kNotificationURLElementKey])) {
Expand Down Expand Up @@ -473,6 +481,11 @@ - (void)handleRemoteNotification:(NSDictionary *)userInfo forApplicationState:(U
} else if([BlueshiftEventAnalyticsHelper isSchedulePushNotification:userInfo]) {
[self validateAndScheduleLocalNotification:userInfo];
} else {
NSString *pushUUID = [userInfo valueForKey:kInAppNotificationModalMessageUDIDKey];
if ([pushCategory isEqualToString:kNotificationCategorySilentPushIdentifier] || [pushUUID isEqualToString:lastProcessedPushNotificationUUID]) {
[BlueshiftLog logInfo:@"Skipped processing notification due to following reasons" withDetails:@"1. The push notification is silent push notification 2. The push notification click is already processed." methodName:nil];
return;
}
// Handle push notification when the app is in inactive or background state ...
if ([pushCategory isEqualToString:kNotificationCategoryBuyIdentifier]) {
[self handleCategoryForBuyUsingPushDetailsDictionary:userInfo];
Expand Down
1 change: 1 addition & 0 deletions BlueShift-iOS-SDK/BlueShiftNotificationConstants.h
Original file line number Diff line number Diff line change
Expand Up @@ -33,6 +33,7 @@
#define kNotificationCategoryViewCartIdentifier @"view_cart"
#define kNotificationActionOpenCartIdentifier @"open_cart"

#define kNotificationCategorySilentPushIdentifier @"silent_push"
#define kNotificationCategoryOfferIdentifier @"promotion"
#define kNotificationProductIDIdenfierKey @"product_id"
#define kNotificationSelectedIndexKey @"selected_index"
Expand Down
3 changes: 2 additions & 1 deletion BlueShift-iOS-SDK/BlueShiftUserInfo.h
Original file line number Diff line number Diff line change
Expand Up @@ -31,7 +31,8 @@
@property (nonatomic, strong) NSString *facebookID;

/// Unsubscribe from the push notifications.
@property BOOL unsubscribed;
/// Set this flag to true if you want to stop receiving push notifications for that user.
@property NSNumber* unsubscribed;

/// The data stored in the additionalUserInfo will be populated on server with `additional_user_info__` prefix to every key name.
/// If key is stored as `profession`, then server will popluate it as `additional_user_info__profession` in the events.
Expand Down
8 changes: 5 additions & 3 deletions BlueShift-iOS-SDK/BlueShiftUserInfo.m
Original file line number Diff line number Diff line change
Expand Up @@ -58,7 +58,9 @@ - (NSMutableDictionary *)convertToDictionary {
}
[BlueshiftEventAnalyticsHelper addToDictionary:sharedUserInfoMutableDictionary key:kBSUserFacebookId value:self.facebookID];
[BlueshiftEventAnalyticsHelper addToDictionary:sharedUserInfoMutableDictionary key:kBSUserEducation value:self.education];
[BlueshiftEventAnalyticsHelper addToDictionary:sharedUserInfoMutableDictionary key:kBSUserUnsubscribedPush value:[NSNumber numberWithBool:self.unsubscribed]];
if (self.unsubscribed != nil) {
[BlueshiftEventAnalyticsHelper addToDictionary:sharedUserInfoMutableDictionary key:kBSUserUnsubscribedPush value:self.unsubscribed];
}
[BlueshiftEventAnalyticsHelper addToDictionary:sharedUserInfoMutableDictionary key:kBSUserAdditionalInfo value:self.additionalUserInfo];
if (self.dateOfBirth) {
NSNumber *dateOfBirthTimeStamp = [NSNumber numberWithDouble:[self.dateOfBirth timeIntervalSinceReferenceDate]];
Expand Down Expand Up @@ -136,8 +138,8 @@ + (BlueShiftUserInfo *)parseUserInfoDictionary:(NSDictionary *)currentUserInfoDi
blueShiftUserInfo.education = [currentUserInfoDictionary objectForKey:kBSUserEducation];
blueShiftUserInfo.facebookID = [currentUserInfoDictionary objectForKey:kBSUserFacebookId];
blueShiftUserInfo.gender = [currentUserInfoDictionary objectForKey:kBSUserGender];
if([currentUserInfoDictionary objectForKey:kBSUserUnsubscribedPush]) {
blueShiftUserInfo.unsubscribed = [[currentUserInfoDictionary objectForKey:kBSUserUnsubscribedPush] boolValue];
if([currentUserInfoDictionary objectForKey:kBSUserUnsubscribedPush] != nil) {
blueShiftUserInfo.unsubscribed = (NSNumber*)[currentUserInfoDictionary objectForKey:kBSUserUnsubscribedPush];
}
NSTimeInterval joinedAtTimeStamp = [[currentUserInfoDictionary objectForKey:kBSUserJoinedAt] doubleValue];

Expand Down
1 change: 1 addition & 0 deletions BlueShift-iOS-SDK/BlueshiftConstants.h
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,7 @@
#define kDeviceID @"device_id"
#define kApple @"apple"
#define kiOS @"iOS"
#define kBrowserPlatform @"browser_platform"

//App Data
#define kEnablePush @"enable_push"
Expand Down
3 changes: 3 additions & 0 deletions BlueShift-iOS-SDK/InApps/BlueShiftInAppNotificationHelper.h
Original file line number Diff line number Diff line change
Expand Up @@ -48,6 +48,9 @@ NS_ASSUME_NONNULL_BEGIN
/// @warning In the sceneDelegate enabled apps, In order to access multiple windows to find the keyWindow, this function needs to be executed on the main thread
+ (UIWindow *)getApplicationKeyWindow;

/// Returns MD5 hash for the given string
+ (NSString *)getMD5ForString:(NSString*)string;

@end

NS_ASSUME_NONNULL_END
15 changes: 15 additions & 0 deletions BlueShift-iOS-SDK/InApps/BlueShiftInAppNotificationHelper.m
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@
//
// Created by shahas kp on 11/07/19.
//
#import <CommonCrypto/CommonDigest.h>

#import "BlueShiftInAppNotificationHelper.h"
#import "BlueShiftInAppNotificationConstant.h"
Expand Down Expand Up @@ -168,4 +169,18 @@ + (BOOL)isIpadDevice {
return NO;
}

+ (NSString *)getMD5ForString:(NSString*)string {
const char *cStr = [string UTF8String];
unsigned char result[CC_MD5_DIGEST_LENGTH];
CC_MD5( cStr, (CC_LONG)strlen(cStr), result );

return [NSString stringWithFormat:
@"%02X%02X%02X%02X%02X%02X%02X%02X%02X%02X%02X%02X%02X%02X%02X%02X",
result[0], result[1], result[2], result[3],
result[4], result[5], result[6], result[7],
result[8], result[9], result[10], result[11],
result[12], result[13], result[14], result[15]
];
}

@end
18 changes: 12 additions & 6 deletions BlueShift-iOS-SDK/InApps/BlueShiftInAppNotificationManager.m
Original file line number Diff line number Diff line change
Expand Up @@ -613,14 +613,13 @@ -(void)processSlideInBannerNotification:(BlueShiftInAppNotification*)notificatio
BOOL isSlideInIconImagePresent = [notificationVC isSlideInIconImagePresent:notification];
BOOL isBackgroundImagePresent = [notificationVC isBackgroundImagePresentForNotification:notification];
if (isSlideInIconImagePresent || isBackgroundImagePresent) {
dispatch_async(dispatch_get_global_queue(DISPATCH_QUEUE_PRIORITY_DEFAULT, 0), ^{
dispatch_async(dispatch_get_global_queue(DISPATCH_QUEUE_PRIORITY_HIGH, 0), ^{
if(isSlideInIconImagePresent) {
[notificationVC loadAndCacheImageForURLString:notification.notificationContent.iconImage];
}
if (isBackgroundImagePresent) {
[notificationVC loadAndCacheImageForURLString:notification.templateStyle.backgroundImage];
}
[NSThread sleepForTimeInterval:1];
[self presentInAppViewController:notificationVC forNotification:notification];
});
} else {
Expand All @@ -637,14 +636,13 @@ -(void)processModalNotification:(BlueShiftInAppNotification*)notification displa
BOOL isBannerImagePresent = [notificationVC isBannerImagePresentForNotification:notification];

if (isBackgroundImagePresent || isBannerImagePresent) {
dispatch_async(dispatch_get_global_queue(DISPATCH_QUEUE_PRIORITY_DEFAULT, 0), ^{
dispatch_async(dispatch_get_global_queue(DISPATCH_QUEUE_PRIORITY_HIGH, 0), ^{
if (isBackgroundImagePresent) {
[notificationVC loadAndCacheImageForURLString:notification.templateStyle.backgroundImage];
}
if (isBannerImagePresent) {
[notificationVC loadAndCacheImageForURLString:notification.notificationContent.banner];
}
[NSThread sleepForTimeInterval:1];
[self presentInAppViewController:notificationVC forNotification:notification];
});
} else {
Expand Down Expand Up @@ -673,7 +671,7 @@ - (void)presentInAppViewController:(BlueShiftNotificationViewController*)notific
return;
}
}
dispatch_async(dispatch_get_main_queue(), ^{
void(^ presentInAppBlock)(void) = ^{
if (notificationController && [self inAppNotificationDisplayOnPage]) {
[BlueshiftLog logInfo:@"Presenting in-app notification on the screen name" withDetails:[self inAppNotificationDisplayOnPage] methodName:nil];
notificationController.delegate = self;
Expand All @@ -684,7 +682,15 @@ - (void)presentInAppViewController:(BlueShiftNotificationViewController*)notific
self.currentNotificationController = nil;
[BlueshiftLog logInfo:@"Skipping preseting in-app notification as screen is not registered to receive in-app notification" withDetails:[self inAppNotificationDisplayOnPage] methodName:nil];
}
});
};

if ([NSThread isMainThread] == YES) {
presentInAppBlock();
} else {
dispatch_async(dispatch_get_main_queue(), ^{
presentInAppBlock();
});
}
}

#pragma mark - In App events
Expand Down
Loading

0 comments on commit 3ee760e

Please sign in to comment.