推通知服务扩展在 iOS 中是必要的,通过添加图片、视频或交互式按钮来使通知更吸引人。它们还确保在不会影响应用程序性能的情况下,安全地处理敏感任务,如处理用户数据。
点击展开
- 需要在项目中集成 WebEngage SDK
- 了解服务扩展和内容扩展的基本知识
- 了解推通知、swift/Obj-C 编程语言的基本知识
-
在服务扩展中集成图书馆有两种常见方法
建议选择 Swift Package Manager 或 Cocoapods 以将库集成到您的服务扩展中。混合这两种方法可能会导致项目设置中的冲突或不一致。
-
选择您的
项目
>包依赖项
>+
按钮。在搜索栏中输入包 URL:https://github.com/WebEngage/WEServiceExtension.git
https://github.com/WebEngage/WEServiceExtension.git
-
在
添加到目标
下选择NotificationService
(您的服务扩展目标)。 -
点击
添加包
。
-
-
系统内应已安装 Cocoapods
-
您的项目应有 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
-
-
保存对 Podfile 的更改。
-
通过运行以下命令安装 Pods
pod install
-
-
-
-
-
-
打开 NotificationService.swift
-
通过添加以下代码导入 WEServiceExtension:
import WEServiceExtension
-
从
NotificationService
类中删除所有现有代码 -
使用
WEXPushNotificationService
给NotificationService
添加子类import UserNotifications // Step 1 : Importing WEServiceExtension import WEServiceExtension // Step 2 : Subclassing service Extension class NotificationService: WEXPushNotificationService { }
-
-
- 打开 NotificationService.m
- 导入
WEServiceExtension/WEServiceExtension-Swift.h
- 创建
WEXPushNotificationService
的对象 - 通过上面创建的对象将必要信息传递给
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
-
-
以下是具体操作方法
-
App Groups 允许你的应用程序和 WebEngageNotificationServiceExtension 在接收到通知时进行通信,即使你的应用程序处于非活动状态。这是确认递送所必需的。
-
-
构建你的项目以确保库成功集成。
-
测试你的服务扩展以确保它与集成的库按预期工作。
-
-
-
如果你一直在使用旧的服务扩展并希望切换到新版本,只需遵循以下文档中的说明即可
-
以下是从 WebEngageBannerPush 迁移到 WEServiceExtension 的步骤
-
在 podfile 中从 Service Extension Target
NotificationService
删除pod 'WebEngageBannerPush'
。 -
然后执行
pod install
-
然后遵循第 2种方法:通过 Cocoapods 集成
-
完成以上步骤后,我们将转向代码部分
-
- 打开
NotificationService.swift
文件。 - 将
import WebEngageBannerPush
替换为import WEServiceExtension
。 - 完成
- 打开
-
- 打开
NotificationService.h
文件。 - 删除导入语句
#import <WebEngageBannerPush/WEXPushNotificationService.h>
。 - 将
WEXPushNotificationService
替换为UNNotificationServiceExtension
。 - 打开
NotificationService.m
文件。 - 按照第 Objective C 步骤替换代码
- 打开
-
-
-
WEServiceExtension 在 MIT 许可证下可用。更多信息请参阅 LICENSE 文件。