Setup - SDK and Push Notification

import BlueShift SDK header inside AppDelegate.h file

  • Objective-C
#import <BlueShift-iOS-SDK/BlueShift.h>    
@interface AppDelegate : UIResponder <UIApplicationDelegate, BlueShiftPushDelegate>
  • Swift
import BlueShift_iOS_SDK
class AppDelegate: UIResponder, UIApplicationDelegate, BlueShiftPushDelegate

Add the following code inside

  • Objective-C
- (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions {
 // Override point for customization after application launch.

 // Obtain an instance of BlueShiftConfig ...
 BlueShiftConfig *config = [BlueShiftConfig config];

 // Set the api Key for the config ...
 [config setApiKey:@"Your API Key here"];

 // Set the applications launch Options for SDK to track ...
 [config setApplicationLaunchOptions:launchOptions];

 // Set the Three Predefined DeepLinking URL's ...
 [config setProductPageURL:[NSURL URLWithString:@"Product page URL here"]];
 [config setCartPageURL:[NSURL URLWithString:@"Cart page URL here"]];
 [config setOfferPageURL:[NSURL URLWithString:@"Offer page URL here"]];

 // For Carousel push notification deep linking for each image
 [config setAppGroupID:@"Your App Group ID here"];   
    
 // Set Batch upload interval period in seconds
 [[BlueShiftBatchUploadConfig sharedInstance] setBatchUploadTimer:60.0];

 // Initialize the configuration ...
 [BlueShift initWithConfiguration:config];

 return YES;
}
  • Swift
func application(_ application: UIApplication, didFinishLaunchingWithOptions launchOptions: [UIApplicationLaunchOptionsKey: Any]?) -> Bool {
    // Override point for customization after application launch.
    
    // Obtain an instance of BlueShiftConfig ...
    let config: BlueShiftConfig = BlueShiftConfig()
    
    // Set the api Key for the config ...
    config.apiKey = "Your API Key here"

    // Set the applications launch Options for SDK to track ...
    config.applicationLaunchOptions = launchOptions

    // Set the Three Predefined DeepLinking URL's ...
    config.productPageURL = NSURL.init(string: "Product page URL here") as URL!
    config.cartPageURL = NSURL.init(string: "Cart page URL here") as URL!
    config.offerPageURL = NSURL.init(string: "Offer page URL here") as URL!

    // For Carousel push notification deep linking for each image
    config.appGroupID = "Your App Group ID here"       

    // Set Batch upload interval period in seconds
    BlueShiftBatchUploadConfig.sharedInstance().batchUploadTimer = 90;
    
    // Initialize the configuration ...
    BlueShift.initWithConfiguration(config)
    
    return true
}

You can read more about Batching and Real-time events here.

 

Deeplinking URL

<scheme>://<scheme_identifier>/<view_controller_identifier_1>/../<view_controller_identifi er_n>

<scheme> ­ the scheme name, this is generally your application name.

<scheme_identifier> ­ the identifier depicting the scheme ID, usually, the bundle identifier.

<view_controller_identifier> ­ the unique storyboard ID representing the view controller.

Below is an example for a url scheme which enables deeplinking through two view controllers (viewcontroller1 and viewcontroller2):

blueshiftdemoapp://com.blueshift.demoapp/viewcontroller1/viewcontroller2

 

Disable functionalities

If developer want to disable BlueShift push notification, BlueShift analytics, BlueShift analytics accesssing locations can be configured during SDK Initialization. By default SDK will enable these functionalities.

 

Objective C

// Disable BlueShift Push Notification
[config setEnablePushNotification:NO];


// Disable BlueShift Analytics accessing location
[config setEnableLocationAccess:NO];


// Disable BlueShift Analytics
[config setEnableAnalytics:NO];

 

Swift

// Disable BlueShift Push Notification
config.enablePushNotification = false


// Disable BlueShift Analytics accessing location
config.enableLocationAccess = false


// Disable BlueShift Analytics
config.enableAnalytics = false

SDK Notification Callbacks

SDK provides callbacks while user click on notification or notification action buttons (find here)

By default developer can write these callbacks in AppDelegate, but if application want to implement callbacks in separate class, application need to pass custom class reference in BlueShifConfig during SDK initialization part.

Objective C

// BlueShiftDelegates is the class for handling BlueShiftPushDelegate delegate Callbacks
BlueShiftDelegates *blueShiftDelegatge = [[BlueShiftDelegates alloc] init];
[config setBlueShiftPushDelegate:blueShiftDelegatge];

Swift

// BlueShiftDelegates is the class for handling BlueShiftPushDelegate delegate Callbacks
let blueShiftDelegatge: BlueShiftDelegates = BlueShiftDelegates()

config.blueShiftDelegatge = blueShiftDelegatge
 

Auto Integration

Call + (void) autoIntegration after calling + (void) initWithConfiguration:(BlueShiftConfig *)config for making things simpler, it will set [UIApplication sharedApplication].delegate to Instance of BlueShiftAppDelegate and it will handle all appDelegate methods, developer need not implement any appDelegate mehtods.
But if application try to accesss [UIApplication sharedApplication].delegate it will retrun BlueShiftAppDelegate instead of appDelegate if it cause any trouble go with manual integration.

 

Objective C

  • Objective-C
- (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions {
 // Override point for customization after application launch.

 // Obtain an instance of BlueShiftConfig ...
 BlueShiftConfig *config = [BlueShiftConfig config];

 // Set the api Key for the config ...
 [config setApiKey:@"Your API Key here"];

 // Set the applications launch Options for SDK to track ...
 [config setApplicationLaunchOptions:launchOptions];

 // Set the Three Predefined DeepLinking URL's ...
 [config setProductPageURL:[NSURL URLWithString:@"Product page URL here"]];
 [config setCartPageURL:[NSURL URLWithString:@"Cart page URL here"]];
 [config setOfferPageURL:[NSURL URLWithString:@"Offer page URL here"]];

 // For Carousel push notification deep linking for each image
 [config setAppGroupID:@"Your App Group ID here"];

 // Set Batch upload interval period in seconds
 [[BlueShiftBatchUploadConfig sharedInstance] setBatchUploadTimer:60.0];

 // Initialize the configuration ...
 [BlueShift initWithConfiguration:config];
 
 // Auto Integration
 [BlueShift autoIntegration];

 return YES;
}
 

Swift

  • Swift
func application(_ application: UIApplication, didFinishLaunchingWithOptions launchOptions: [UIApplicationLaunchOptionsKey: Any]?) -> Bool {
    // Override point for customization after application launch.
    
    // Obtain an instance of BlueShiftConfig ...
    let config: BlueShiftConfig = BlueShiftConfig()
    
    // Set the api Key for the config ...
    config.apiKey = "Your API Key here"

    // Set the applications launch Options for SDK to track ...
    config.applicationLaunchOptions = launchOptions

    // Set the Three Predefined DeepLinking URL's ...
    config.productPageURL = NSURL.init(string: "Product page URL here") as URL!
    config.cartPageURL = NSURL.init(string: "Cart page URL here") as URL!
    config.offerPageURL = NSURL.init(string: "Offer page URL here") as URL!

    // For Carousel push notification deep linking for each image
    config.appGroupID = "Your App Group ID here"          

    // Set Batch upload interval period in seconds
    BlueShiftBatchUploadConfig.sharedInstance().batchUploadTimer = 90;
    
    // Initialize the configuration ...
    BlueShift.initWithConfiguration(config)
    
    // Auto Integration
    BlueShift.autoIntegration()

    return true
}
 

App Delegate for Push Notifcations

In addition to configure Push notification and background event batch upload you would need to add the following to your AppDelegate.h file

  • Objective-C
- (void)application:(UIApplication *)application didRegisterForRemoteNotificationsWithDeviceToken:(nonnull NSData *)deviceToken {
    [[BlueShift sharedInstance].appDelegate registerForRemoteNotification:deviceToken];
}

- (void)application:(UIApplication*)application didFailToRegisterForRemoteNotificationsWithError:(NSError*)error {
    [[BlueShift sharedInstance].appDelegate failedToRegisterForRemoteNotificationWithError:error];
}

- (void)application:(UIApplication *)application didReceiveRemoteNotification:(NSDictionary *)userInfo fetchCompletionHandler:(void (^)(UIBackgroundFetchResult result))handler {
    [[BlueShift sharedInstance].appDelegate handleRemoteNotification:userInfo forApplication:application fetchCompletionHandler:handler];
}

- (void)application:(UIApplication *)application didReceiveRemoteNotification:(nonnull NSDictionary *)userInfo {
    [[BlueShift sharedInstance].appDelegate application:application handleRemoteNotification:userInfo];
}

- (void)application:(UIApplication *)application didReceiveLocalNotification:(nonnull UILocalNotification *)notification {
    [[BlueShift sharedInstance].appDelegate application:application handleLocalNotification:notification];
}

- (void)application:(UIApplication *) application handleActionWithIdentifier: (NSString *) identifier forRemoteNotification: (NSDictionary *) notification completionHandler: (void (^)()) completionHandler {  
    [[BlueShift sharedInstance].appDelegate handleActionWithIdentifier:identifier forRemoteNotification:notification completionHandler:completionHandler];
}

- (void)applicationDidEnterBackground:(UIApplication *)application {
    // Use this method to release shared resources, save user data, invalidate timers, and store enough application state information to restore your application to its current state in case it is terminated later.
    // If your application supports background execution, this method is called instead of applicationWillTerminate: when the user quits.
    [[BlueShift sharedInstance].appDelegate appDidEnterBackground:application];
}

- (void)applicationDidBecomeActive:(UIApplication *)application {
    // Restart any tasks that were paused (or not yet started) while the application was inactive. If the application was previously in the background, optionally refresh the user interface.
    [[BlueShift sharedInstance].appDelegate appDidBecomeActive:application];
}

The BlueShiftUserInfo class helps sending the user related details to Blueshift. If the values are set after sign in, they will be used for building the events params for all events going forward. Here is an example of setting customer id and email after user sign in.

  • Objective-C
// sign in complete
[BlueShiftUserInfo sharedUserInfo] setEmail:@"Email ID here"];
[[BlueShiftUserInfo sharedUserInfo] setRetailerCustomerID:@"Customer ID here"];

// add other user info initializations here.

// It is important to save the instance of BlueShiftUserInfo after it has been updated  
[[BlueShiftUserInfo sharedUserInfo] save];
 

Register user with BlueShift

Once BlueShift user info is saved, developer need to fire identify event ASAP.
refer identify event here

 

Permissions required

To receive push notification your application need to enable Push Notification in Xcode's Project editor / Capabilities tab by switching Push Notification to ON.

To receive silent push notifications your application needs to enable Remote Notifications in Xcode's Project Editor / Capabilities tab by switching Background Modes to ON and checking Remote Notifications.

The developer can configure the SDK to send events real-time or batched using the Blueshift Bulk Events API.

There are certain events (for eg. app_open, push_view) that pre-configured by the SDK. These events are always sent in batches. You can learn more about these events (here)

The SDK allows you to specify if an event needs to be sent real-time or in a batch. Events are sent to Blueshift by calling the SDK methods [here] (). The last argument in each method controls whether an event needs to be send real-time or in batch.

 

Batch Events

Batched events are sent using the Blueshift Bulk Event API along with other events. The events are added to a persistent queue which ensures that they are not lost if the user force quits the app. Batches of 100 events are created from the queue every 5 min (or the time interval configured during SDK set-up) and sent to Blueshift using the Bulk Events API called from non UI blocking background thread.

 

Real-time Events

Your application may prefer sending certain events (for eg. Purchase) real-time to Blueshift. The SDK may store these events in a queue if no network is available. In this situation, the events are sent as a part of the next batch of events, as soon as network is available.

 

Notification Extensions

For handling media based push notifications apple provide two notifications extensions, Service extension and Content extension since iOS 10 onward.
See notification extensions integration here

If analytics want to capture push notification delivery event precisely, it's necessary to add Service extension and Blueshift integration in it.

Things needed to take care
* Operating system must be iOS 10 +
* Notification service extension integrated along with the app and SDK method called inside in it.

Objective C

- (void)didReceiveNotificationRequest:(UNNotificationRequest *)request withContentHandler:(void (^)(UNNotificationContent * _Nonnull))contentHandler {

   self.contentHandler = contentHandler;
   self.bestAttemptContent = [request.content mutableCopy];
   //Modify the notification content here...
   [[BlueShiftPushNotification sharedInstance] setApiKey: @"yourApiKeyHere"];
   self.bestAttemptContent.attachments = [[BlueShiftPushNotification sharedInstance] integratePushNotificationWithMediaAttachementsForRequest:request];
   self.contentHandler(self.bestAttemptContent);
}
- (void)serviceExtensionTimeWillExpire {
   // Called just before the extension will be terminated by the system.
   // Use this as an opportunity to deliver your "best attempt" at modified content, otherwise the original push payload will be used.
   self.bestAttemptContent.attachments = [BlueShiftPushNotification sharedInstance].attachments;
   self.contentHandler(self.bestAttemptContent);
} 

Swift

override func didReceive(_ request: UNNotificationRequest, withContentHandler contentHandler: @escaping (UNNotificationContent) -> Void) {
    self.contentHandler = contentHandler
    bestAttemptContent = (request.content.mutableCopy() as? UNMutableNotificationContent)
    BlueShiftPushNotification.sharedInstance().apiKey = "yourApiKeyHere"
    if let bestAttemptContent = bestAttemptContent {
        // Modify the notification content here...
        bestAttemptContent.attachments = BlueShiftPushNotification.sharedInstance().integratePushNotificationWithMediaAttachements(for: request) as! [UNNotificationAttachment]

        contentHandler(bestAttemptContent)
    }
 }


 override func serviceExtensionTimeWillExpire() {
    // Called just before the extension will be terminated by the system.
    // Use this as an opportunity to deliver your "best attempt" at modified content, otherwise the original push payload will be used.
    bestAttemptContent?.attachments = BlueShiftPushNotification.sharedInstance().attachments
    if let contentHandler = contentHandler, let bestAttemptContent =  bestAttemptContent {
        contentHandler(bestAttemptContent)
    }
 }


* Every push notification payload must contain mutable-content flag with value '1' (for invoking extension)

Was this article helpful?
0 out of 0 found this helpful
Have more questions? Submit a request