WEContentExtension 1.1.1

WEContentExtension 1.1.1

WEPersonalizationBhavesh SarwarWebEngage 维护。



WEContentExtension

Version License Platform LastUpdated

WEContentExtension

WEContentExtension 是集成在 WebEngage 中的扩展 SDK,用于支持在 iOS 应用程序内丰富的推送布局

目录

点击展开

先决条件

  • 需要在项目中集成 WebEngage SDK
  • 了解服务扩展和内容扩展的基本知识
  • 了解推送通知,Swift / Objective-C 编程语言的基本知识

  • 第 1 步:为项目创建内容扩展

    本指南提供将内容扩展集成到您的 iOS 项目中的逐步指南。内容扩展允许您通过提供可在各种上下文中显示的附加内容来扩展应用程序的功能,例如 Today 视图或 Messages 应用程序。

    • 打开您的 Xcode 项目。

    • 从菜单中选择 File > New > Target...

    • 从列表中选择 App Extension 模板。

    • 选择您要创建的扩展类型(例如,ContentExtension)。

    • 为您的扩展提供一个名称并点击 Finish

    • 屏幕截图

      Screenshot 1

      屏幕截图 1

      Screenshot 2

      屏幕截图 2

      Screenshot 3

      屏幕截图 3

      Screenshot 4

      屏幕截图 4

  • 第 2 步:在 ContentExtension 中集成 WebEngage

    在内容扩展中集成库有两种常见方法

    注意:选择 SPM 或 Cocoapods

    建议选择 Swift Package Manager 或 Cocoapods 将库集成到您的内容扩展中。同时使用这两种方法可能会导致项目设置中的冲突或不一致。


    方法 1:通过 SPM 集成

    • 添加包依赖项
      1. 转到 File > Swift Packages > Add Package Dependency...

      Screenshot 1 (SPM)

      SPM 屏幕截图 1
      1. 输入库仓库的 URL:https://github.com/WebEngage/WEContentExtension

      2. 单击 Next

      3. 选择要使用的分支或输入特定的分支要求(master)。

      4. 单击 Next

      Screenshot 1 (SPM)

      SPM 屏幕截图 2
      1. 选择要将依赖项添加到的目标,即您的 Content Extension 目标。

      2. 单击 Finish

      Screenshot 1 (SPM)

      SPM 屏幕截图 3


    方法 2:通过 CocoaPods 集成

    • 先决条件

      • 应将 Cocoapods 安装在您的系统上

      • 为您的项目应可用的 podfile

    • 编辑 Podfile

      • 使用文本编辑器打开 Podfile。

      • 将库依赖项添加到 Podfile 中。例如

        # this target name should be your ContentExtension Name
        target 'ContentExtension' do
          pod 'WEContentExtension'
        end

        注意:您的目标名称应为在创建 ContentExtension 时输入的内容扩展名称,在此处参考截图 3

    • 安装 Pods

      • 保存对 Podfile 的更改。

      • 运行以下命令安装 pods:

        pod install

  • 第3步: 在内容扩展中导入和使用WebEngage

    • SWIFT

      1. 打开 NotificationViewController.swift

      2. 通过添加代码 import WEContentExtension 来导入WEContentExtension

      3. 将类 NotificationViewController 中所有现有的代码移除

      4. 使用 WEXRichPushNotificationViewControllerNotificationViewController 进行子类化


      NotificationViewController.swift 将呈现如下代码片段

      import UIKit
      import UserNotifications
      import UserNotificationsUI
      
      // Import WebEngage Extension SDK
      import WEContentExtension
      
      // Subclassing current class with WebEngage provided class
      class NotificationViewController: WEXRichPushNotificationViewController {
      
          // remove all existing code inside this class
      
      }
    • Objective C

      1. 打开 NotificationViewController.m

      2. 导入 WEContentExtension

      3. 创建一个 WEContentExtension 对象

      4. 通过上述创建的对象将必要的信息传递给WebEngage

      NotificationViewController.m 将呈现如下代码片段

      #import "NotificationViewController.h"
      #import <UserNotifications/UserNotifications.h>
      #import <UserNotificationsUI/UserNotificationsUI.h>
      
      // Step 1 : Importing WEContentExtension
      #import <WEContentExtension/WEContentExtension-Swift.h>
      
      @interface NotificationViewController () <UNNotificationContentExtension>
      
      @property IBOutlet UILabel *label;
      // Step 2 : Creating Object of content Extension
      @property WEXRichPushNotificationViewController *weRichPushVC;
      
      @end
      
      @implementation NotificationViewController
      
      // Step 3 : Pass necessary information to WebEngage
      - (void)viewDidLoad {
          if (_weRichPushVC == NULL){
              _weRichPushVC = [[WEXRichPushNotificationViewController alloc]init];
          }
          [_weRichPushVC setUpViewsWithParentVC:self];
          [super viewDidLoad];
      
      }
      
      - (void)didReceiveNotification:(UNNotification *)notification {
          [_weRichPushVC didReceiveNotification:notification];
      }
      
      - (void)didReceiveNotificationResponse:(UNNotificationResponse *)response completionHandler:(void (^)(UNNotificationContentExtensionResponseOption))completion{
          [_weRichPushVC didReceiveNotificationResponse:response completionHandler:completion];
      }
      @end

  • 第4步: 配置ContentExtension-Info.plist

    以下是操作方法

    • 打开针对 NotificationViewControllerInfo.plist 文件

    • 展开 NSExtension > NSExtensionAttributes

    • NSExtensionAttributes 中查找 UNNotificationExtensionCategory。如果不存在,请添加它,并将类型设置为 Array。在Array中添加以下项:

      • WEG_CAROUSEL_V1
      • WEG_RATING_V1
      • WEG_RICH_V1
      • WEG_RICH_V2
      • WEG_RICH_V3
      • WEG_RICH_V4
      • WEG_RICH_V5
      • WEG_RICH_V6
      • WEG_RICH_V7
      • WEG_RICH_V8
    • UNNotificationExtensionDefaultContentHidden 下的属性设置为 YES

    • 在NotificationViewController的Info.plist文件中的信息属性列表下添加App Transport Security设置键。在这两个文件中,都将App Transport Security设置下的Allow Arbitrary Loads设置为YES

      (注意:如果您确定WebEngage仪表板上的推送通知提供的图像URL始终使用https,则上述步骤不是必需的。)

    • Info.plist的源代码片段

          <key>NSExtension</key>
          <dict>
            <key>NSExtensionAttributes</key>
            <dict>
              <key>UNNotificationExtensionCategory</key>
              <array>
                <string>WEG_CAROUSEL_V1</string>
                <string>WEG_RATING_V1</string>
                <string>WEG_RICH_V1</string>
                <string>WEG_RICH_V2</string>
                <string>WEG_RICH_V3</string>
                <string>WEG_RICH_V4</string>
                <string>WEG_RICH_V5</string>
                <string>WEG_RICH_V6</string>
                <string>WEG_RICH_V7</string>
                <string>WEG_RICH_V8</string>
              </array>
              <key>UNNotificationExtensionDefaultContentHidden</key>
              <true/>
              <key>UNNotificationExtensionInitialContentSizeRatio</key>
              <real>1</real>
            </dict>
            <key>NSExtensionMainStoryboard</key>
            <string>MainInterface</string>
            <key>NSExtensionPointIdentifier</key>
            <string>com.apple.usernotifications.content-extension</string>
          </dict>



    • Info.plist 应该看起来像以下截图

      InfoPlist

      info.plist

  • 第5步: 为所有目标创建App Groups

    App Groups允许当接收到通知时,即使您的应用程序未处于活动状态,您的应用程序和WebEngageNotificationContentExtension之间的通信。这对于确认投递是必需的。

    • 选择您的 主应用程序目标 > 签名和功能 > + 功能 > App Groups
    • App Groups 中,点击 + 按钮。
    • 将App Groups容器设置为 group.YOUR_BUNDLE_IDENTIFIER.WEGNotificationGroup,其中 YOUR_BUNDLE_IDENTIFIER 与主应用程序的 "Bundle Identifier" 相同。
    • 确定 按钮,然后对 NotificationViewController 目标重复此操作。
    • 屏幕截图



      Screenshot 1

      屏幕截图 1


      Screenshot 1

      屏幕截图 2


      Screenshot 1

      屏幕截图 3

  • 第6步: 构建 和 测试

    • 构建您的项目以确保库成功集成。

    • 测试您的内容扩展以确保它使用集成库按预期工作。


  • 从 WebEngageAppEx 迁移到 WEContentExtension

    • 如果您一直在使用旧内容扩展并希望切换到新的内容扩展,请始终遵循文档中的这些说明

    • 以下是从WebEngageAppEx迁移到WEContentExtension的步骤:

      • 从podfile中删除 pod 'WebEngageAppEx/ContentExtension' 中的 Content Extension Target NotificationViewController

      • 然后执行 pod install

      • 然后按照第2种方法:通过Cocoapods集成进行操作

      • 完成以上步骤后,让我们进入代码部分

        • 对于Swift用户

          • 打开 NotificationViewController.swift 文件。
          • import WebEngageAppEx 替换为 import WEContentExtension
          • 完成了
        • 对于Objective-C用户

          • 打开 NotificationViewController.h 文件。
          • 移除导入语句 #import
          • WEXRichPushNotificationViewController 替换为 UNNotificationContentExtension
          • 打开 NotificationViewController.m 文件。
          • 如第Objective-C步所述替换代码

许可

WEContentExtension可在MIT许可证下使用。更多信息请参阅LICENSE文件。