如何在IOS模拟器Xcode中测试推送通知

3

如何在使用 Xcode 11.4 及以上版本的 iOS 模拟器中测试推送通知,而不需要使用 iOS 设备。

2个回答

17

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

要进行测试:

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

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

enter image description here

选项2-使用终端

在终端中运行以下命令

xcrun simctl push <simulator identifier> <bundle identifier of the app> <pushcontentfile>.apns"

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

Xcode Menu => Window => Devices and Simulators

enter image description here

.apns文件格式

  • 将推送通知负载(json格式)保存到扩展名为“.apns”的文件中
{
    "aps": {
        "alert": "Push Notifications Test",
        "sound": "default",
        "badge": 1
    }
}

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

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

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

很棒的答案和选项 - Ben Patch
2
要获取模拟器ID,请执行以下操作:xcrun simctl list | egrep '(Booted)' - Albert Vila Calvo
还可以试试:http://pushplayground.com/ 这是一个免费的网页工具,用于测试推送通知。 - Johnny

0

我刚刚注意到我可以通过APNs将通知推送到在我的Mac上运行的iOS 16模拟器。它可以在不需要创建“文件”和模拟推送通知的情况下工作。也许你需要Xcode 14.0,那是我使用的版本。

只需像平常一样将通知推送到设备上(例如使用firebase_messaging,或者使用我的脚本:https://github.com/ben-xD/push/blob/main/tools/ios/send_ios_example

enter image description here


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