VizuryRichNotification 2.1.2

VizuryRichNotification 2.1.2

Anurag Bhowmick维护。



  • 作者:
  • Ayon Chowdhury

Vizury Logo

VizuryEventLogger

Version License

Vizury是一个商业营销平台,其个性化的再营销堆叠被数字公司用来增长营销ROI和增强交易。

摘要

这是iOS SDK集成指南。SDK支持iOS9及以上版本。

基本集成

通过CocoaPods集成

CocoaPods是Objective C & Swift项目的依赖管理器,使集成更简单。

  1. 如果您还没有安装CocoaPods,您可以在终端中执行以下行来进行安装。

    sudo gem install cocoapods

  2. 如果您还没有Podfile,您可以在Xcode项目目录中创建一个名为Podfile的纯文本文件,并确保将平台和版本设置为与您的应用程序匹配。

    pod 'VizuryEventLogger'
    pod 'VizuryRichNotification'

    为了避免出现问题,请在项目中添加以下Firebase馆
    pod 'Firebase/Messaging'

  3. 通过在Xcode项目目录中执行以下命令来安装VizuryEventLoggerVizuryRichNotification SDK。

    pod install

  4. 现在,打开您的项目工作区,检查是否已正确添加VizuryEventLoggerVizuryRichNotification SDK。

Vizury SDK 初始化

在您的 AppDelegate 文件中导入 VizuryEventLogger

Objective-C


#import <VizuryEventLogger/VizuryEventLogger.h>

在AppDelegate的didFinishLaunchingWithOptions方法中添加以下代码以初始化SDK

  [VizuryEventLogger initializeEventLoggerInApplication:(UIApplication*)application
                            WithPackageId:(NSString *)packageId
                            ServerURL:(NSString *)serverURL
                            WithCachingEnabled:(BOOL) caching
                            AndWithFCMEnabled:(BOOL) isFCMEnabled];

Swift


#import VizuryEventLogger

更新您的AppDelegate

class AppDelegate: UIResponder, UIApplicationDelegate, UNUserNotificationCenterDelegate

在AppDelegate的didFinishLaunchingWithOptions方法中添加以下代码以初始化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

搞定!SDK 已成功集成并初始化,项目准备就绪,可以使用。

事件记录

当用户浏览应用时,会发生各种活动,例如访问产品、将产品添加到购物车、进行购买等。这些活动被称为事件。对于每个事件,应用需要将某些变量传递给SDK,SDK将自动将它们传递到Vizury服务器。

Objective-C


创建一个包含与事件相关属性的attributeDictionary,并使用事件名称和attributeDictionary调用[VizuryEventLogger logEvent]

    #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];

Swift


创建一个具有相关事件属性的attributeDictionary,并调用`VizuryEventLogger.logEvent`方法,传入事件名称和attributeDictionary。

        let attributeDictionary = [ "productId":"AKSJDASNBD",
                                        "price" : "999",
                                        "category" : "shirt"]
        VizuryEventLogger.logEvent("productPage", withAttributes: attributeDictionary)

推送通知

为了发送推送通知,我们使用了GCM-APNS接口。为此,您需要在Apple开发者会员中心配置推送通知应用程序,并从Google获取配置文件。

配置Apple开发者设置

要启用通过APNs发送推送通知,您需要

a) 创建认证密钥。

b) 创建App ID。

c) 为该App ID创建配置文件。

您可以在Apple开发者会员中心创建它们,按照这些步骤进行。

配置FCM项目

设置CocoaPods依赖

  • 如果您还没有Xcode项目,请现在创建一个。
  • 如果没有Podfile,请创建一个。
$ cd your-project directory
$ pod init
  • 添加Firebase/Messaging Pod。
pod 'Firebase/Messaging'
  • 安装Pod并打开.xcworkspace文件,在Xcode中查看项目。
$ pod install
$ open your-project.xcworkspace

启用 FCM

如果您还没有 Firebase 项目,请在 Firebase 控制台 中创建一个。输入 项目名称

createProject-1

单击 iOS 选项,在下一页添加 iOS 包标识符。该 iOS 包标识符 应与您的应用包标识符相同。您可以在下一步中下载 GoogleService-Info.plist 文件。

createProject-2

注意:您下载的 GoogleService-Info.plist 文件将包含某些设置,例如 IS_ADS_ENABLED、IS_SIGNIN_ENABLED 设置为 YES。您必须添加相应的 pod 依赖项或如果您不使用它们,可以将其关闭

接下来,单击创建的项目中的 设置图标 选项。

createProject-3

单击 云消息 选项卡并上传 APNs 认证密钥 (.p8 格式)。还要注意,记录下 服务器密钥,因为您在集成过程中需要它。您还可以上传 APNs 证书,但建议使用认证密钥进行配置,因为它们是发送 iOS 通知的更现代方法

createProject-4

在上传 APNs 认证密钥 (.p8 格式) 时,您需要输入密钥 ID 和团队 ID。

  1. “密钥 ID”是您在 Apple 开发者控制台“证书、标识符和配置文件”->“密钥”->选择特定的证书下创建的认证密钥的 ID。

createProject-4

  1. “团队 ID”是 Apple 开发者控制台“成员资格”->“成员资格详情”下的 Team Member ID。

createProject-4

配置应用

  • 将您刚刚下载的 GoogleService-Info.plist 文件拖放到 Xcode 项目的根目录,并将其添加到所有目标中
  • AppDelegatedidFinishLaunchingWithOptions 方法内部注册推送通知

Objective-C


    // 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]

Swift


        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()
  • 对于 iOS10 及以上版本,您还需要添加以下代码。您可以参考示例应用。
#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
  • 注册后

将 APNS 令牌传递给 Vizury

Objective-C


- (void)application:(UIApplication *)application didRegisterForRemoteNotificationsWithDeviceToken:(NSData *)deviceToken {

    [VizuryEventLogger registerForPushWithToken:deviceToken];
}

Swift


    func application(_ application: UIApplication, didRegisterForRemoteNotificationsWithDeviceToken deviceToken: Data) {
        VizuryEventLogger.registerForPush(withToken: deviceToken)
    }

任何失败的注册情况

Objective-C


- (void)application:(UIApplication *)application didFailToRegisterForRemoteNotificationsWithError:(NSError *)error {

    [VizuryEventLogger didFailToRegisterForPush];
}

Swift


    func application(_ application: UIApplication, didFailToRegisterForRemoteNotificationsWithError error: Error) {
        VizuryEventLogger.didFailToRegisterForPush()
    }
  • 处理通知负载

Objective-C


- (void)application:(UIApplication *)application didReceiveRemoteNotification:(NSDictionary *)userInfo fetchCompletionHandler:(void (^)(							UIBackgroundFetchResult))completionHandler {
    [VizuryEventLogger didReceiveRemoteNotificationInApplication:application withUserInfo:userInfo];
 }

Swift


    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();
    }

Deep Links

为了打开随推送通知一起发送到设备作为键/值对的 Deep Links,您必须实现一个自定义处理程序

Objective-C


- (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
    }
}

Swift


    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)
        } 
    }

支持

请访问此存储库的GitHub问题跟踪器以报告针对我们iOS SDK的特定错误。对于其他问题和支持,请从您的仪表板联系Vizury支持。