A-A+

iOS开发之模拟器测试远程推送

博客主机

Xcode 11.4 之前必须使用第三方和真机来测试远程推送(APNS)。 但 Xcode 11.4 之后可以通过simctl命令在模拟器上进行测试。

使用命令行

Xcode 命令行工具允许从终端上使用模拟器:启动模拟器,触发通用链接等。 其中有一个命令就是将远程推送通知发送到 iOS 模拟器。

查看帮助

xcrun simctl push --help
Send a simulated push notification
Usage: simctl push <device> [<bundle identifier>] (<json file> | -)

    bundle identifier
         The bundle identifier of the target application
         If the payload file contains a 'Simulator Target Bundle' top-level key this parameter may be omitted.
         If both are provided this argument will override the value from the payload.
    json file
         Path to a JSON payload or '-' to read from stdin. The payload must:
           - Contain an object at the top level.
           - Contain an 'aps' key with valid Apple Push Notification values.
           - Be 4096 bytes or less.

Only application remote push notifications are supported. VoIP, Complication, File Provider, and other types are not supported.

参数解释

  • device :只需将其设置为booted即可使用已经启动的模拟器。 还可以使用xcrun simctl list devices | grep Booted查看已经启动的模拟器。
xcrun simctl list devices | grep Booted
    iPhone 11 Pro Max (97AE0B1F-4C63-4B02-906B-8B2CF9E3F4B0) (Booted)
  • bundle identifier:设置要测试远程推送的 App 的唯一标识符。
  • json file:包含远程推送通知详细内容的 JSON 文件。

案例

xcrun simctl push booted developer.yf.TestUIKit /Users/yangfan/Desktop/playload.json

playload.json

{
  "aps":{
    "alert":{
      "title":"测试",
      "subtitle":"远程推送",
      "body":"这是一条从远处而来的通知"
    },
    "sound":"default",
    "badge":1
  }
}
json结果

使用APNS文件

在 iOS 模拟器上测试远程推送通知的另一种方法是将 APNS 文件拖到 iOS 模拟器中。该文件后缀名为.apns,内容和上面的 JSON 文件差不多,但是添加了一个 Simulator Target Bundle ,描述唯一标识符。

{
  "Simulator Target Bundle": "developer.yf.TestUIKit",
  "aps":{
    "alert":{
      "title":"测试",
      "subtitle":"远程推送",
      "body":"这是一条从远处而来的通知"
    },
    "sound":"default",
    "badge":1
  }
}
apns结果

给我留言

Copyright © ios教程,苹果粉丝,苹果资讯,ios入门教程,ios学习,ios程序员,ios视频教程,ios粉丝网 保留所有权利.   Theme  Ality

用户登录