问题描述
我想使用推送通知对我的应用进行 UI 测试,也许你们中的一些人已经找到了最佳实践。有很多教程和问题,但我找不到满意的答案。
但是发送推送通知的最佳做法是什么?
- 触发推送通知的休息调用
- 访问终端(但如何访问?)并运行:
xcrun simctl push <device> com.example.my-app ExamplePush.apns
- 发送本地推送通知?
- 模拟服务器
- 使用 Mussel 之类的框架
谢谢! :)
解决方法
使用终端通过单行命令测试推送通知
在 Mac 中安装 Houston,在终端中运行以下命令。
-
gem 安装休斯顿
如果您遇到这样的错误,
正在获取 houston-2.4.0.gem 错误:执行 gem 时 ... (Gem::FilePermissionError) 您没有 /Library/Ruby/Gems/2.6.0 目录的写入权限。
首先在终端中运行以下命令来安装 Ruby
brew install ruby
导出 GEM_HOME="$HOME/.gem"
gem 安装导轨
安装成功后再次运行
gem 安装休斯顿
-
转到 pem 文件文件夹并从该文件夹打开终端。
-
像下面这样运行命令
apn push "设备令牌" -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
详情请参考以下链接
以下是使用类别标识符触发本地通知的示例代码
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)")
}
}
,
出于测试目的,如果您想创建测试推送通知
https://github.com/noodlewerk/NWPusher
这个库非常有用。