WEServiceExtension 1.1.2

WEServiceExtension 1.1.2

WebEngageWEPersonalization 维护。



  • WebEngage

WEServiceExtension

Version License Platform LastUpdated


推通知服务扩展在 iOS 中是必要的,通过添加图片、视频或交互式按钮来使通知更吸引人。它们还确保在不会影响应用程序性能的情况下,安全地处理敏感任务,如处理用户数据。

目录

点击展开

先决条件

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

  • 第 1 步:为项目创建服务扩展

    • 在 Xcode 中,转到 文件 > 新建 > 目标,选择 通知服务扩展,然后 下一步

    • 将产品名称输入为 NotificationService,然后点击完成。

    • 点击提示中的激活,以激活服务扩展。现在 Xcode 将在你的项目中创建一个名为 NotificationService 的新顶级文件夹。

    • 屏幕截图

      Screenshot 1

      截图 1

      Screenshot 2

      截图 2

      Screenshot 3

      截图 3

      Screenshot 4

      截图 4

  • 第 2 步:在服务扩展中集成 WEServiceExtension

    在服务扩展中集成图书馆有两种常见方法

    注意:选择 SPM 或 Cocoapods

    建议选择 Swift Package Manager 或 Cocoapods 以将库集成到您的服务扩展中。混合这两种方法可能会导致项目设置中的冲突或不一致。


    方法 1:通过 SPM 集成

    • 选择您的 项目 > 包依赖项 > + 按钮。在搜索栏中输入包 URL:https://github.com/WebEngage/WEServiceExtension.git

      https://github.com/WebEngage/WEServiceExtension.git
      

      Screenshot 1 (SPM)

      截图 1 (SPM)

      Screenshot 2 (SPM)

      截图 2 (SPM)

      Screenshot 3 (SPM)

      截图 3 (SPM)
    • 添加到目标 下选择 NotificationService(您的服务扩展目标)。

      Screenshot 4 (SPM)

      截图 4 (SPM)
    • 点击 添加包

      Screenshot 5 (SPM)

      截图 5 (SPM)


    方法 2:通过 CocoaPods 集成

    • 先决条件

      • 系统内应已安装 Cocoapods

      • 您的项目应有 podfile

    • 编辑 Podfile

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

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

        # this target name should be your ServiceExtension Name
        target 'NotificationService' do
          # Uncomment the line below if the parent target also uses frameworks
          # use_frameworks!
          pod 'WEServiceExtension'
          # Add other pods for the NotificationService target here
        end

        注意:您的目标名称应与您在创建 ServiceExtension 时输入的服务扩展名称相同。在此处请参考截图 3

      • 安装 Pods

        • 保存对 Podfile 的更改。

        • 通过运行以下命令安装 Pods

          pod install

  • 第 3 步:在服务扩展中导入和使用 WebEngage

    • SWIFT

      1. 打开 NotificationService.swift

      2. 通过添加以下代码导入 WEServiceExtensionimport WEServiceExtension

      3. NotificationService 类中删除所有现有代码

      4. 使用 WEXPushNotificationServiceNotificationService 添加子类

        import UserNotifications
        // Step 1 : Importing WEServiceExtension
        import WEServiceExtension
        
        // Step 2 : Subclassing service Extension
        class NotificationService: WEXPushNotificationService {
        }
    • Objective C

      1. 打开 NotificationService.m
      2. 导入 WEServiceExtension/WEServiceExtension-Swift.h
      3. 创建 WEXPushNotificationService 的对象
      4. 通过上面创建的对象将必要信息传递给 WebEngage

      下面是 NotificationService.m 的代码片段

      #import "NotificationService.h"
        // Step 1 : Importing WEServiceExtension
        #import <WEServiceExtension/WEServiceExtension-Swift.h>
      
        @interface NotificationService ()
        // Step 2 : Creating Object of service Extension
        @property (nonatomic, strong) WEXPushNotificationService *serviceExtension;
      
        @end
      
        @implementation NotificationService
      
        // Step 3 : Pass necessary information to WebEngage
        - (void)didReceiveNotificationRequest:(UNNotificationRequest *)request withContentHandler:(void (^)(UNNotificationContent * _Nonnull))contentHandler {
            if (_serviceExtension == NULL){
                _serviceExtension = [[WEXPushNotificationService alloc]init];
            }
            [_serviceExtension didReceiveNotificationRequest:request
                                          withContentHandler:contentHandler];
        }
      
        - (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.
            [_serviceExtension serviceExtensionTimeWillExpire];
        }
      
        @end
  • 第4步: 配置 ServiceExtension-Info.plist

    以下是具体操作方法

    • 打开 NotificationServiceInfo.plist 文件
    • NotificationService-Info.plist 文件下的 信息属性列表 中添加 App Transport Security Settings 键。
    • App Transport Security Settings 下的 Allow Arbitrary Loads 设置为 YES - 如果你确定在 WebEngage 控制台提供的图像 URL 总是使用 https,则不需要此设置。

      Screenshot 1

      截图 1
  • 第5步: 为所有目标创建应用组

    App Groups 允许你的应用程序和 WebEngageNotificationServiceExtension 在接收到通知时进行通信,即使你的应用程序处于非活动状态。这是确认递送所必需的。

    • 选择你的 主应用程序目标 > 签名 & 能力 > + 能力 > App Groups
    • App Groups 中,点击 + 按钮。
    • 将 App Groups 容器设置为 group.YOUR_BUNDLE_IDENTIFIER.WEGNotificationGroup,其中 YOUR_BUNDLE_IDENTIFIER 与你的主应用程序的 "包标识符" 相同。
    • 点击 OK 并对 NotificationService 目标进行重复操作。
    • 屏幕截图



      Screenshot 1

      截图 1


      Screenshot 1

      截图 2


      Screenshot 1

      截图 3

  • 第6步: 构建和测试

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

    • 测试你的服务扩展以确保它与集成的库按预期工作。


  • 从 WebEngageBannerPush 迁移到 WEServiceExtension

    • 如果你一直在使用旧的服务扩展并希望切换到新版本,只需遵循以下文档中的说明即可

    • 以下是从 WebEngageBannerPush 迁移到 WEServiceExtension 的步骤

      • 在 podfile 中从 Service Extension Target NotificationService 删除 pod 'WebEngageBannerPush'

      • 然后执行 pod install

      • 然后遵循第 2种方法:通过 Cocoapods 集成

      • 完成以上步骤后,我们将转向代码部分

        • 对于 Swift 用户

          • 打开 NotificationService.swift 文件。
          • import WebEngageBannerPush 替换为 import WEServiceExtension
          • 完成
        • 对于 Objective-C 用户

          • 打开 NotificationService.h 文件。
          • 删除导入语句 #import <WebEngageBannerPush/WEXPushNotificationService.h>
          • WEXPushNotificationService 替换为 UNNotificationServiceExtension
          • 打开 NotificationService.m 文件。
          • 按照第 Objective C 步骤替换代码

许可证

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