测试推送通知的 iOS 最佳实践(UI 测试)

标签 ios swift xcode xcode-ui-testing uitest

我想使用推送通知对我的应用程序进行 UI 测试,也许你们中的一些人已经找到了最佳实践。有很多教程和问题,但我找不到满意的答案。

  1. 开始测试
  2. 发送推送通知
  3. 点击推送通知
  4. 做事

但是发送推送通知的最佳做法是什么?

  • 触发推送通知的Rest Call
  • 获得对终端的访问权限(但如何?)并运行:xcrun simctl push <device> com.example.my-app ExamplePush.apns
  • 发送本地推送通知?
  • 模拟服务器
  • 使用类似Mussel 的框架

谢谢! :)

最佳答案

使用终端通过单行命令测试推送通知

在您的 Mac 中安装 Houston,在终端中运行以下命令。

  1. gem 安装休斯顿

    如果你遇到这样的错误,

    正在获取 houston-2.4.0.gem 错误:执行 gem 时...(Gem::FilePermissionError) 您没有/Library/Ruby/Gems/2.6.0 目录的写入权限。

    首先在终端运行下面​​的命令来安装Ruby

    brew 安装 ruby​​

    导出 GEM_HOME="$HOME/.gem"

    gem 安装导轨

    安装成功后再次运行

    gem 安装休斯顿

  2. 转到 pem 文件文件夹并从该文件夹打开终端。

  3. 运行如下命令

    apn push "设备 token "-c PEM_FILE_NAME -m "MESSAGE"

    喜欢:

    apn push "5a4b74d5e5fc325b14d2f2641aa11bfb9744d1f88922822a5ed3512376d5f5b9"-c myapp_apns_dev.pem -m "测试"

成功运行上述命令后,它会询问 PEM 密码,这是您的 pem 文件的密码。

如果您的应用已上线,则使用生产 pem 文件名

像这样,

apn push "5a4b74d5e5fc325b14d2f2641aa11bfb9744d1f88922822a5ed3512376d5f5b9"-c myapp_apns_pro.pem -m "测试"

完成。

对于 UI 测试,您可以使用本地通知,

您必须为本地通知设置 categoryIdentifier 并确保在您的 AppExtension Info.plist 文件中将相同的 categoryIdentifier 设置为 UNNotificationExtensionCategory

更多详情请引用以下链接

https://levelup.gitconnected.com/custom-push-notification-in-ios-swift-5-210552643e86#:~:text=Go%20to%20xcode%20select%20File,have%20its%20unique%20category%20Identifier .

下面是使用类别标识符触发本地通知的示例代码

let content = UNMutableNotificationContent()
content.title = title
content.body = body
content.categoryIdentifier = "LOCAL_NOTIFICATION"
    
if let info = userInfo {
       let dic = ["body" : "Custom Data"]
       content.userInfo = dic as [AnyHashable : Any]
 }
    
content.sound = UNNotificationSound.init(named: UNNotificationSoundName(rawValue: sound))
            
let trigger = UNTimeIntervalNotificationTrigger(timeInterval: 1, repeats: false)
            
let request = UNNotificationRequest(identifier: "LOCAL_NOTIFICATION", content: content, trigger: trigger)
    
let notificationCenter = UNUserNotificationCenter.current()
     notificationCenter.add(request) { (error) in
          if let error = error {
                print("Error \(error.localizedDescription)")
           }
 }

关于测试推送通知的 iOS 最佳实践(UI 测试),我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/66349457/

相关文章:

ios - 从 TableView 中删除行时 CoreData 应用程序崩溃

ios - 检查 ios 模拟器/设备中的 http 请求

iphone - cellForRowAtIndexPath 加载可见单元格上方和下方的 3 个单元格

ios - 我们可以通过编程方式更改 sectionindextitlesfortableview(A 到 Z Alphabet) 的 y 位置吗?

swift - “ fatal error :在展开可选值时意外发现nil”是什么意思?

ios - 如何通过 xcode 中的 nib 将 UIViewController 放置在另一个 UIViewController 中

c++ - 分水岭分割opencv xcode

ios - 从服务器流式传输 .caf 音频文件

Swift 将变量转换为变量类

xcode - 自定义编程 UITableViewCell 的自动布局在滚动时失败