This is iOS SDK integration guide. The SDK supports iOS7 and above.
Examples on how the Vizury iOS SDK can be integrated.
sampleApp/HelloVizury
is a sample app having a basic integration with vizury SDK.
Download the latest Vizury iOS SDK VizuryEventLogger
. The framework file is available with bitcode enabled
and bitcode disabled
. Extract the required archive into a directory of your choice. The extracted file is VizuryEventLogger.framework
Go to the Build phases -> Link Binary with Libraries. Click on the +
icon
Add Foundation.framework
, then click on Add Other
and add the extracted VizuryEventLogger.framework
file
Import the VizuryEventLogger
#import <VizuryEventLogger/VizuryEventLogger.h>
Add the following in didFinishLaunchingWithOptions
method of AppDelegate to initialize the SDK
[VizuryEventLogger initializeEventLoggerInApplication:(UIApplication*)application
WithPackageId:(NSString *)packageId
ServerURL:(NSString *)serverURL
WithCachingEnabled:(BOOL) caching
AndWithFCMEnabled:(BOOL) isFCMEnabled];
In yout bridging header file add
#import <VizuryEventLogger/VizuryEventLogger.h>
Update your AppDelegate
class AppDelegate: UIResponder, UIApplicationDelegate, UNUserNotificationCenterDelegate
Add the following in didFinishLaunchingWithOptions
method of AppDelegate to initialize the SDK
VizuryEventLogger.initializeEventLogger(in: application,
withPackageId: packageId,
serverURL: serverUrl,
withCachingEnabled: caching,
AndWithFCMEnabled: isFCMEnabled)
Where
packageId : packageId obtained from vizury
serverURL : serverURL obtained from vizury
caching : pass true if your app supports offline usage and you want to send user behaviour data
to vizury while he was offline. Pass false otherwise
isFCMEnabled : true/false depending on if you want to use vizury for push
When a user browse through the app, various activities happen e.g. visiting a product, adding the product to cart, making purchase, etc. These are called events. Corresponding to each event, app needs to pass certain variables to the SDK which the SDK will automatically pass to Vizury servers.
Create an attributeDictionary with the attributes associated with the event and call [VizuryEventLogger logEvent]
with event name and the attributeDictionary.
#import <VizuryEventLogger/VizuryEventLogger.h>
NSDictionary *attributeDictionary = [[NSDictionary alloc] initWithObjectsAndKeys:
@"AKSJDASNBD",@"productid",
@"789", @"productPrice",
@"Shirt",@"category",
nil];
[VizuryEventLogger logEvent:@"productPage" WithAttributes:attributeDictionary];
// sending a JSONObject
NSDictionary *productDetail = [NSDictionary dictionaryWithObjectsAndKeys:
@"62112",@"product_id",
@"1",@"quantity",
@"50", @"price",
nil];
NSData *productjsonData = [NSJSONSerialization dataWithJSONObject:productDetail options:0 error:nil];
NSString *productjsonStr = [[NSString alloc] initWithData:productjsonData encoding:NSUTF8StringEncoding];
NSDictionary *productjsonDictionary = [NSDictionary dictionaryWithObjectsAndKeys:
productjsonStr,@"viz_data",
nil];
[VizuryEventLogger logEvent:@"productDetails" WithAttributes:productjsonDictionary];
Create an attributeDictionary with the attributes associated with the event and call `VizuryEventLogger.logEvent with event name and the attributeDictionary.
let attributeDictionary = [ "productId":"AKSJDASNBD",
"price" : "999",
"category" : "shirt"]
VizuryEventLogger.logEvent("productPage", withAttributes: attributeDictionary)
For sending push notifications we are using GCM-APNS interface. For this you need to configure the App for push notifications in Apple Developer Member Center and get a configuration file from google.
To enable sending Push Notifications through APNs, you need
a) Create the authentication key.
b) Create an App ID
c) Provisioning profile for that App ID.
You can create them in the Apple Developer Member Center by following these steps
- If you don't have an Xcode project yet, create one now
- Create a Podfile if you don't have one
$ cd your-project directory
$ pod init
- Add the
Firebase/Messaging
pod
pod 'Firebase/Messaging'
- Install the pods and open the .xcworkspace file to see the project in Xcode
$ pod install
$ open your-project.xcworkspace
Create a Firebase project in the Firebase console if you don't already have one. Enter the AppName
and Region
.
Click on iOS
option and in the next screen add the iOS Bundle Id
. The iOS Bundle Id
should be same as your apps bundle identifier. You can download the GoogleService-Info.plist
file in the next step.
Note : The GoogleService-Info.plist file that you have downloaded will have certain settings like IS_ADS_ENABLED, IS_SIGNIN_ENABLED set as YES. You have to add correspinding pod dependencies for the same or you can turn them off if you are not using them
Next go the Manage
option of the created project.
Click on Cloud Messaging
tab and upload APNs Authentication Key (.p8 format). Also note down the the Legacy Server key
as this will be required later during the integration. You can also upload APNs certificaties but configuration with auth keys is recommended as they are the more current method for sending notifications to iOS
While uploading APNs Authentication Key (.p8 format) you need to enter the KeyId and App Id prefix
- 'Key ID' is the id of the authentication key you created in Apple developer console under Keys -> All
- 'App ID Prefix' is the app prefix in Apple developer console under Identifiers -> App Ids
- Drag the GoogleService-Info.plist file you just downloaded into the root of your Xcode project and add it to all targets
- Register for Pushnotifications inside didFinishLaunchingWithOptions method of you AppDelegate
// Register for remote notifications. This shows a permission dialog on first run, to
// show the dialog at a more appropriate time move this registration accordingly.
if (floor(NSFoundationVersionNumber) <= NSFoundationVersionNumber_iOS_7_1) {
// iOS 7.1 or earlier. Disable the deprecation warnings.
#pragma clang diagnostic push
#pragma clang diagnostic ignored "-Wdeprecated-declarations"
UIRemoteNotificationType allNotificationTypes =
(UIRemoteNotificationTypeSound | UIRemoteNotificationTypeAlert | UIRemoteNotificationTypeBadge);
[application registerForRemoteNotificationTypes:allNotificationTypes];
#pragma clang diagnostic pop
} else {
// iOS 8 or later
// [START register_for_notifications]
if (floor(NSFoundationVersionNumber) <= NSFoundationVersionNumber_iOS_9_x_Max) {
#pragma clang diagnostic push
#pragma clang diagnostic ignored "-Wdeprecated-declarations"
UIUserNotificationType allNotificationTypes =
(UIUserNotificationTypeSound | UIUserNotificationTypeAlert | UIUserNotificationTypeBadge);
UIUserNotificationSettings *settings =
[UIUserNotificationSettings settingsForTypes:allNotificationTypes categories:nil];
[[UIApplication sharedApplication] registerUserNotificationSettings:settings];
} else {
// iOS 10 or later
#if defined(__IPHONE_10_0) && __IPHONE_OS_VERSION_MAX_ALLOWED >= __IPHONE_10_0
// For iOS 10 display notification (sent via APNS)
[UNUserNotificationCenter currentNotificationCenter].delegate = self;
UNAuthorizationOptions authOptions =
UNAuthorizationOptionAlert | UNAuthorizationOptionSound | UNAuthorizationOptionBadge;
[[UNUserNotificationCenter currentNotificationCenter] requestAuthorizationWithOptions:authOptions completionHandler:^(BOOL granted, NSError * _Nullable error) {
}];
#endif
}
[[UIApplication sharedApplication] registerForRemoteNotifications];
// [END register_for_notifications]
- For iOS10 and above you also need to add the below code. You can refer to the sample app.
#if defined(__IPHONE_10_0) && __IPHONE_OS_VERSION_MAX_ALLOWED >= __IPHONE_10_0
@import UserNotifications;
#endif
// Implement UNUserNotificationCenterDelegate to receive display notification via APNS for devices
// running iOS 10 and above.
#if defined(__IPHONE_10_0) && __IPHONE_OS_VERSION_MAX_ALLOWED >= __IPHONE_10_0
@interface AppDelegate () <UNUserNotificationCenterDelegate>
@end
#endif
if #available(iOS 10.0, *) {
let authOptions: UNAuthorizationOptions = [.alert, .badge, .sound]
UNUserNotificationCenter.current().requestAuthorization(
options: authOptions,
completionHandler: {_, _ in })
UNUserNotificationCenter.current().delegate = self
} else {
let settings: UIUserNotificationSettings =
UIUserNotificationSettings(types: [.alert, .badge, .sound], categories: nil)
application.registerUserNotificationSettings(settings)
}
application.registerForRemoteNotifications()
- Post Registration
Pass the APNS token to Vizury
- (void)application:(UIApplication *)application didRegisterForRemoteNotificationsWithDeviceToken:(NSData *)deviceToken {
[VizuryEventLogger registerForPushWithToken:deviceToken];
}
func application(_ application: UIApplication, didRegisterForRemoteNotificationsWithDeviceToken deviceToken: Data) {
VizuryEventLogger.registerForPush(withToken: deviceToken)
}
In case of any failed registration
- (void)application:(UIApplication *)application didFailToRegisterForRemoteNotificationsWithError:(NSError *)error {
[VizuryEventLogger didFailToRegisterForPush];
}
func application(_ application: UIApplication, didFailToRegisterForRemoteNotificationsWithError error: Error) {
VizuryEventLogger.didFailToRegisterForPush()
}
- Handling notification payload
- (void)application:(UIApplication *)application didReceiveRemoteNotification:(NSDictionary *)userInfo fetchCompletionHandler:(void (^)( UIBackgroundFetchResult))completionHandler {
[VizuryEventLogger didReceiveRemoteNotificationInApplication:application withUserInfo:userInfo];
}
func application(_ application: UIApplication, didReceiveRemoteNotification userInfo: [AnyHashable: Any], fetchCompletionHandler completionHandler: @escaping (UIBackgroundFetchResult) -> Void) {
VizuryEventLogger.didReceiveRemoteNotification(in: application, withUserInfo: userInfo)
if (application.applicationState == UIApplicationState.inactive) {
self.customPushHandler(userInfo: userInfo)
}
completionHandler(UIBackgroundFetchResult.newData)
}
@available(iOS 10.0, *)
func userNotificationCenter(_ center: UNUserNotificationCenter, didReceive response: UNNotificationResponse, withCompletionHandler completionHandler: @escaping () -> Void) {
let pushDictionary = response.notification.request.content.userInfo
VizuryEventLogger.didReceiveResponse(userInfo: pushDictionary)
self.customPushHandler(userInfo: pushDictionary)
completionHandler();
}
In order to open Deep Links that are sent to the device as a Key/Value pair along with a push notification you must implement a custom handler
- (void)application:(UIApplication *)application didReceiveRemoteNotification:(NSDictionary *)userInfo fetchCompletionHandler:(void (^)( UIBackgroundFetchResult))completionHandler {
[VizuryEventLogger didReceiveRemoteNotificationInApplication:application withUserInfo:userInfo];
if(application.applicationState == UIApplicationStateInactive) {
NSLog(@"Appilication Inactive - the user has tapped in the notification when app was closed or in background");
[self customPushHandler:userInfo];
}
}
- (void) customPushHandler:(NSDictionary *)notification {
if (notification !=nil && [notification objectForKey:@"deeplink"] != nil) {
NSString* deeplink = [notification objectForKey:@"deeplink"];
NSLog(@"%@",deeplink);
// Here based on the deeplink you can open specific screens that's part of your app
}
}
func application(_ application: UIApplication, didReceiveRemoteNotification userInfo: [AnyHashable: Any], fetchCompletionHandler completionHandler: @escaping (UIBackgroundFetchResult) -> Void) {
VizuryEventLogger.didReceiveRemoteNotification(in: application, withUserInfo: userInfo)
if (application.applicationState == UIApplicationState.inactive) {
self.customPushHandler(userInfo: userInfo)
}
completionHandler(UIBackgroundFetchResult.newData)
}
@available(iOS 10.0, *)
func userNotificationCenter(_ center: UNUserNotificationCenter, didReceive response: UNNotificationResponse, withCompletionHandler completionHandler: @escaping () -> Void) {
let pushDictionary = response.notification.request.content.userInfo
VizuryEventLogger.didReceiveResponse(userInfo: pushDictionary)
self.customPushHandler(userInfo: pushDictionary)
completionHandler();
}
func customPushHandler(userInfo : [AnyHashable : Any]) {
if let deeplink = userInfo[AnyHashable("deeplink")] {
// handle the deeplink
print("deeplink is ", deeplink)
}
}
With iOS 10 you can add image, gifs, audio and video to your notifications.
- Add a Notification Service Extension target to your project (File -> New -> Target -> Notification Service Extension)
-
Define the name for the Notification Service Extension in the next step.
-
Make sure the deployment target for notification service is set to 10
-
Make sure in capabilities for the target App,
Push notifications
is enabled -
Make sure in capabilities for the target App,
Background Modes
is enabled withBackground Fetch
andRemote Notification
Add the VizuryRichNotification
folder to your Notification Service Extension target
Make the below code changes in the NotificationService.m file.
#import "VizuryRichNotification.h"
- (void)didReceiveNotificationRequest:(UNNotificationRequest *)request
withContentHandler:(void (^)(UNNotificationContent * _Nonnull))contentHandler {
self.contentHandler = contentHandler;
self.bestAttemptContent = [request.content mutableCopy];
[[VizuryRichNotification getInstance] didReceiveNotificationRequest:request withContentHandler:contentHandler];
}