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

3

我想用推送通知来UI测试我的应用程序,也许有些人已经找到了最佳实践。有很多教程和问题,但我无法找到令人满意的答案。

  1. 开始测试
  2. 发送推送通知
  3. 点击推送通知
  4. 执行任务

但是,发送推送通知的最佳实践是什么呢?

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

谢谢!:)

2个回答

2
使用终端测试单行推送通知:
在 Mac 上安装 Houston,然后在终端中运行以下命令。
  1. gem install houston

    如果您遇到以下错误:

    Fetching houston-2.4.0.gem ERROR: While executing gem ... (Gem::FilePermissionError) You don't have write permissions for the /Library/Ruby/Gems/2.6.0 directory.

    首先在终端中运行以下命令安装Ruby:

    brew install ruby

    export GEM_HOME="$HOME/.gem"

    gem install rails

    成功安装后再运行一次:

    gem install houston

  2. 进入pem文件夹,并在该文件夹中打开终端。

  3. 运行以下命令:

    apn push "Device Token" -c PEM_FILE_NAME -m "MESSAGE"

    例如:

    apn push "5a4b74d5e5fc325b14d2f2641aa11bfb9744d1f88922822a5ed3512376d5f5b9" -c myapp_apns_dev.pem -m "Testing"

在成功运行上述命令后,它会要求您输入PEM密码短语,这是您的PEM文件的密码。
如果您的应用已上线,则使用生产PEM文件名称
像这样,
apn push "5a4b74d5e5fc325b14d2f2641aa11bfb9744d1f88922822a5ed3512376d5f5b9" -c myapp_apns_pro.pem -m "Testing"
完成。
对于UI测试,您可以使用本地通知,
您必须为本地通知设置categoryIdentifier,并确保将相同的categoryIdentifier设置为UNNotificationExtensionCategory在您的AppExtension Info.plist文件中
更详细的信息请参阅下面的链接。
将以下代码翻译成中文:

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)")
           }
 }

但是我不能使用这种方法从UI测试中触发推送通知,是吗? - Godlike
1
我认为以上信息足以进行推送通知自定义UI测试。 - Dhruvin Thumar
1
那其实是一个相当不错的解决方案。你的代码在 UI 测试中无法工作,但可以将其放入应用委托中,并使用启动参数进行操作。谢谢!我稍后会发布代码 :) - Godlike

1

你好,非常感谢您的回答。我已经尝试过NWPusher,但并不是很满意。 - Godlike
请问您能分享一下使用NWPusher时遇到了什么问题吗? - Dhaval Patoliya

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接