如何在 IOS 模拟器 Xcode 中测试 PushNotification

Nev*_*aul 2 xcode apple-push-notifications ios ios-simulator

如何在不使用 ios 设备的情况下使用 Xcode 11.4 及更高版本在 IOS 模拟器中测试推送通知。

Nev*_*aul 6

Xcode 11.4 及更高版本支持使用模拟器测试推送通知。

去测试,

选项 1 - 使用由 Paul Hudson(https://www.hackingwithswift.com/ 的作者)创建的“ControlRoom”Mac 应用程序

Controlroom 是我最近遇到的一个了不起的应用程序,它允许控制模拟器。它提供了一个很好的 UI 来自定义通知。特别感谢 Paul Hudson 在 git 中分享源代码。Git URL - https://github.com/twostraws/ControlRoom

在此处输入图片说明

选项 2 - 使用终端

在终端中运行以下命令

   xcrun simctl push <simulator identifier> <bundle identifier of the app> <pushcontentfile>.apns"
Run Code Online (Sandbox Code Playgroud)

如何使用 Xcode 获取模拟器标识符

Xcode Menu => Window => Devices and Simulators
Run Code Online (Sandbox Code Playgroud)

在此处输入图片说明

.apns 文件的格式

  • 将推送通知负载(json 格式)保存到扩展名为“.apns”的文件中

    {
        "aps": {
            "alert": "Push Notifications Test",
            "sound": "default",
            "badge": 1
        }
    }

Run Code Online (Sandbox Code Playgroud)

选项 3 - 将 .apns 文件拖放到模拟器中

.apns 文件应包含应用程序的包标识符作为有效负载的一部分


    {
      "Simulator Target Bundle": "<bundle identifier of the app>",
      "aps": {
        "alert": "Push Notifications Test",
        "sound": "default",
        "badge": 1
      }
    }

Run Code Online (Sandbox Code Playgroud)

  • 要获取模拟器 ID,请执行以下操作:`xcrun simctl list | egrep '(启动)'` (2认同)