简体   繁体   English

如何在没有 iPhone 的情况下测试 Apple 推送通知服务?

[英]How can I test Apple Push Notification Service without an iPhone?

Is it possible test the Apple Push Notification Services without an iPhone application?是否可以在没有 iPhone 应用程序的情况下测试 Apple 推送通知服务? (Creating an emulator on windows?) (在 windows 上创建模拟器?)

If isn't, how could I test that?如果不是,我怎么能测试呢? Is there a free sample application compiled to do that?是否有编译的免费示例应用程序来执行此操作?

I created the Server provider, but I need test the functionallity.我创建了服务器提供程序,但我需要测试功能。

This answer is outdated .这个答案已经过时了 As of 2020 / Xcode 11.4 it's now possible to test push notifications in the simulator从 2020 / Xcode 11.4 开始,现在可以在模拟器中测试推送通知

See this full explanation in an answer below下面的答案中看到这个完整的解释

Sorry to say, but you'll need to find some hardware to test this functionality.抱歉,您需要找到一些硬件来测试此功能。

Push notifications are not available in the simulator.推送通知在模拟器中不可用。 They require a provisioning profile from iTunes Connect, and thus are required to be installed on a device.它们需要来自 iTunes Connect 的配置文件,因此需要安装在设备上。 That also means you'll probably have to be accepted into the apple iPhone developer program and pay your $99.这也意味着您可能必须被 Apple iPhone 开发者计划接受并支付 99 美元。

On the bright side, with the iPhone OS 3.0 update, you can test this functionality on any device, including the first gen iPhones.好的一面是,随着 iPhone OS 3.0 更新,您可以在任何设备上测试此功能,包括第一代 iPhone。

You can't test real push notifications.您无法测试真正的推送通知。 However , you can test your app's response to a simulated push notification by creating one programmatically and manually triggering your AppDelegate's - application:application didReceiveRemoteNotification:notification method.但是,您可以通过以编程方式创建一个并手动触发您的 AppDelegate 的- application:application didReceiveRemoteNotification:notification方法来测试您的应用程序模拟推送通知的响应

To trigger this from a different class (like a UIViewController):要从不同的类(如 UIViewController)触发它:

[[[UIApplication sharedApplication] delegate]
                    application:[UIApplication sharedApplication]
   didReceiveRemoteNotification:testNotification];

The testNotification should have the same format as a real notification, namely an NSDictionary that contains property list objects plus NSNull. testNotification 应该具有与真实通知相同的格式,即包含属性列表对象和 NSNull 的 NSDictionary。

Here's an example of how to provide the testNotification above:以下是如何提供上述testNotification的示例:

NSMutableDictionary *notification = [[NSMutableDictionary alloc] init];
[notification setValue:@"Test" forKey:@"alert"];
[notification setValue:@"default" forKey:@"sound"];

NSMutableDictionary *testNotification = [[NSMutableDictionary alloc] init];
[testNotification setValue:notification forKey:@"aps"];

This should create a reasonable notification NSDictionary to work with.这应该创建一个合理的通知 NSDictionary 来使用。

Nowadays, we can test push notifications with this library .现在,我们可以使用这个库测试推送通知。

It's pretty easy to send push via terminal:通过终端发送推送非常简单:

echo -n '{"message":"message"}' | nc -4u -w1 localhost 9930

echo -n '{"aps":{"alert" : "message","badge" : 99,"sound" : "default"}, "myField" : 54758}' | nc -4u -w1 localhost 9930

Testing push notifications using the Xcode 11.4 iOS Simulator使用 Xcode 11.4 iOS Simulator 测试推送通知

As of Xcode 11.4 , it is now possible to simulate push notifications by dragging and dropping an .apns file onto the iOS simulator.Xcode 11.4 开始,现在可以通过.apns文件拖放到 iOS 模拟器来模拟推送通知。 The Xcode 11.4 release notes have the following to say about the new feature: Xcode 11.4 发行说明对新功能有以下说明:

Simulator supports simulating remote push notifications, including background content fetch notifications. Simulator 支持模拟远程推送通知,包括后台内容获取通知。 In Simulator, drag and drop an APNs file onto the target simulator .在模拟器中,将 APNs 文件拖放到目标模拟器上 The file must be a JSON file with a valid Apple Push Notification Service payload, including the “aps” key .该文件必须是具有有效 Apple 推送通知服务有效负载的 JSON 文件,包括“aps”键 It must also contain a top-level “Simulator Target Bundle” with a string value matching the target application's bundle identifier.它还必须包含一个顶级“模拟器目标捆绑包” ,其字符串值与目标应用程序的捆绑包标识符相匹配。

simctl also supports sending simulated push notifications. simctl还支持发送模拟推送通知。 If the file contains “Simulator Target Bundle” the bundle identifier is not required, otherwise you must provide it as an argument (8164566):如果文件包含“Simulator Target Bundle”,则不需要包标识符,否则您必须将其作为参数提供 (8164566):

xcrun simctl push <device> com.example.my-app ExamplePush.apns

Example例子

Here is an example for such an .apns file, targeted towards the system settings app:这是此类.apns文件的示例,针对系统设置应用程序:

{
    "Simulator Target Bundle": "com.apple.Preferences",
    "aps": {
        "alert": "This is a simulated notification!",
        "badge": 3,
        "sound": "default"
    }
}

Dragging and dropping this onto the target simulator will present the notification and set the badge:将其拖放到目标模拟器上将显示通知并设置徽章:

iOS模拟器上的通知

Now, to use this for debugging purposes, you only have to change the Simulator Target Bundle to your own app's identifier and adjust the payload to your debugging needs!现在,要将其用于调试目的,您只需Simulator Target Bundle更改为您自己的应用程序标识符并根据您的调试需要调整有效负载

Apple has supporting push notification for simulators. Apple 支持模拟器的推送通知。 iOS 13.4 and above or Xcode 11.4 and above. iOS 13.4 及更高版本或 Xcode 11.4 及更高版本。

as usually create Xcode project and implement a user notification and authorization permission.通常创建Xcode项目并实现用户通知和授权权限。

run your application in simulator iOS 13.4 and above .模拟器 iOS 13.4 及更高版本中运行您的应用程序。

put your application in background.将您的应用程序置于后台。

  1. Create an APNS payload file named " payload.apns "创建一个名为“ payload.apns ”的 APNS 负载文件
{
  "aps": {
    "alert": {
      "title": "Test Push",
      "body": "Success! Push notification in simulator! 🎉",
      "sound": "default"
    },
    "badge": 10
  },
  "Simulator Target Bundle": "com.company.app"
}
  1. Drag and drop to your iOS simulator.拖放到您的 iOS 模拟器。

right now your push notification will appear on simulator.现在您的推送通知将出现在模拟器上。

And also you can simulate push notification by terminal您也可以通过终端模拟推送通知

get your simulator identifier by opening Window->Devices and Simulators and choose your targeted simulator and right click and copy your identifier.通过打开Window->Devices and Simulators并选择您的目标模拟器并右键单击并复制您的标识符来获取您的模拟器标识符。

Now build a terminal command like现在构建一个终端命令,如

xcrun simctl push <simulator-identifier> <path-to-payload-file>

ex:前任:

xcrun simctl push 27A23727-45A9-4C12-BE29-8C0E6D1E5360 payload.apns

run this command and simulate push notification in simulator运行此命令并在模拟器中模拟推送通知

The simulator does not do Push Notifications.模拟器不执行推送通知。

And to push from a server, you have to have device(s) to push to as well as your app on that device.要从服务器推送,您必须拥有要推送到的设备以及该设备上的应用程序。

The token contains the app identity as well as the device ID.令牌包含应用程序标识和设备 ID。

you have to use你必须使用

NSString *notificationString = @"{\"aps\":{\"alert\":\"Test alert\",\"sound\":\"default\"}}";

NSData *notificationData = [notificationString dataUsingEncoding:NSUTF8StringEncoding];

NSDictionary *testNotification = [NSJSONSerialization JSONObjectWithData:notificationData options:0 error:&error];

[[[UIApplication sharedApplication] delegate] application:[UIApplication sharedApplication] didReceiveRemoteNotification:testNotification  fetchCompletionHandler:nil];

Xcode 11.4 Beta starts supporting push notification in Simulator Xcode 11.4 Beta 开始支持模拟器中的推送通知

Simulator supports simulating remote push notifications, including background content fetch notifications. Simulator 支持模拟远程推送通知,包括后台内容获取通知。 In Simulator, drag and drop an APNs file onto the target simulator.在模拟器中,将 APNs 文件拖放到目标模拟器上。 The file must be a JSON file with a valid Apple Push Notification Service payload, including the “aps” key.该文件必须是具有有效 Apple 推送通知服务负载的 JSON 文件,包括“aps”键。 It must also contain a top-level “Simulator Target Bundle” with a string value matching the target application's bundle identifier.它还必须包含一个顶级“模拟器目标捆绑包”,其字符串值与目标应用程序的捆绑包标识符相匹配。

simctl also supports sending simulated push notifications. simctl 还支持发送模拟推送通知。 If the file contains “Simulator Target Bundle” the bundle identifier is not required, otherwise you must provide it as an argument (8164566):如果文件包含“Simulator Target Bundle”,则不需要包标识符,否则您必须将其作为参数提供 (8164566):

$ xcrun simctl push <device> com.example.my-app ExamplePush.apns

Release Notes: https://developer.apple.com/documentation/xcode_release_notes/xcode_11_4_beta_release_notes发行说明: https : //developer.apple.com/documentation/xcode_release_notes/xcode_11_4_beta_release_notes

Apart from @fredpi's answer, you can also use the Poes command-line tool.除了@fredpi 的回答,您还可以使用Poes命令行工具。 It allows us to quickly test remote push notifications on the iOS simulator without the hassle of creating a JSON payload file.它允许我们在 iOS 模拟器上快速测试远程推送通知,而无需创建 JSON 有效负载文件。

Poes --help
OVERVIEW: A Swift command-line tool to easily send push notifications to the iOS simulator

USAGE: Poes <options>

OPTIONS:
  --body, -b            The body of the Push Notification
  --bundle-identifier   The bundle identifier to push to
  --mutable, -m         Adds the mutable-content key to the payload
  --title, -t           The title of the Push Notification
  --verbose             Show extra logging for debugging purposes
  --help                Display available options

The following command could be enough to send out a simple push notification with a default title and body:以下命令足以发送带有默认标题和正文的简单推送通知:

$ Poes --bundle-identifier com.wetransfer.app --verbose
Generated payload:

{
  "aps" : {
    "alert" : {
      "title" : "Default title",
      "body" : "Default body"
    },
    "mutable-content" : false
  }
}

Sending push notification...
Push notification sent successfully

Yes, you can check push notification on the simulator, but you have to use a library in your app named SimulatorRemoteNotifications .是的,您可以在模拟器上检查推送通知,但您必须在您的应用程序中使用名为SimulatorRemoteNotifications的库。 By which, by using just 4-5 steps, you can test push notification on the simulator.通过它,只需使用 4-5 个步骤,您就可以在模拟器上测试推送通知。

They also provide PODs too:他们也提供 POD:

pod 'SimulatorRemoteNotifications', '~> 0.0.3'

It looks like now we have very powerful lib https://github.com/ctreffs/SwiftSimctl看起来现在我们有非常强大的库https://github.com/ctreffs/SwiftSimctl

At least it's much more power than lib SimulatorRemoteNotifications which were mentioned here.至少它比这里提到的 lib SimulatorRemoteNotifications强大得多。 And which was obsoleted also.这也已经过时了。

For a more complete answer since Xcode 11.4 and as of Xcode 12.4:对于自 Xcode 11.4 和 Xcode 12.4 起更完整的答案:

  1. Simulator supports simulating remote push notifications, including background content fetch notifications. Simulator 支持模拟远程推送通知,包括后台内容获取通知。 In Simulator, drag and drop an APNs file onto the target simulator.在模拟器中,将 APNs 文件拖放到目标模拟器上。 The file must be a JSON file with a valid Apple Push Notification Service payload, including the “aps” key.该文件必须是具有有效 Apple 推送通知服务负载的 JSON 文件,包括“aps”键。 It must also contain a top-level “Simulator Target Bundle” with a string value that matches the target application's bundle identifier.它还必须包含一个顶级“模拟器目标捆绑包”,其字符串值与目标应用程序的捆绑包标识符相匹配。 simctl also supports sending simulated push notifications. simctl 还支持发送模拟推送通知。

  2. Notification Service Extensions do not work in simulated push notifications.通知服务扩展在模拟推送通知中不起作用。 The mutable-content key is not honored.不遵守可变内容键。 (55822721) (55822721)

Reference: Xcode 11.4 Release Notes参考: Xcode 11.4 发行说明

With macOS 13 and Xcode 14, when on Mac computers with Apple silicon or T2 processors, it's now possible to receive real push notifications as didRegisterForRemoteNotificationsWithDeviceToken will return a real token.在 macOS 13 和 Xcode 14 中,在配备 Apple 芯片或 T2 处理器的 Mac 计算机上,现在可以接收真实推送通知,因为didRegisterForRemoteNotificationsWithDeviceToken将返回真实令牌。

See Xcode 14 release notes:请参阅 Xcode 14 发行说明:

Simulator now supports remote notifications in iOS 16 when running in macOS 13 on Mac computers with Apple silicon or T2 processors.模拟器现在支持 iOS 16 在具有 Apple 硅或 T2 处理器的 Mac 计算机上的 macOS 13 中运行时的远程通知。 Simulator supports the Apple Push Notification Service Sandbox environment.模拟器支持 Apple Push Notification Service Sandbox 环境。 Your server can send a remote notification to your app running in that simulator by connecting to the APNS Sandbox (api.sandbox.push.apple.com).您的服务器可以通过连接到 APNS 沙箱 (api.sandbox.push.apple.com) 向在该模拟器中运行的应用程序发送远程通知。 Each simulator generates registration tokens unique to the combination of that simulator and the Mac hardware it's running on.每个模拟器都会生成该模拟器和运行它的 Mac 硬件组合所独有的注册令牌。

https://developer.apple.com/documentation/xcode-release-notes/xcode-14-release-notes https://developer.apple.com/documentation/xcode-release-notes/xcode-14-release-notes

Most answers suggest using a third-party Cocoapods/SPM dependency.大多数答案建议使用第三方 Cocoapods/SPM 依赖项。 But these dependencies can easily become unsupported for quite a while after a new iOS or Xcode release.但是在新的 iOS 或 Xcode 版本发布后的很长一段时间内,这些依赖项很容易变得不受支持。

Here is just a lightweight solution that will work in any case and in any weather:这只是一个适用于任何情况和任何天气的轻量级解决方案:

  1. Start any HTTP server on localhost before running the tests (eg: ruby / python )在运行测试之前在本地主机上启动任何 HTTP 服务器(例如: ruby / python
  2. Send a POST request to this localhost from the test when you need to trigger a push notification (eg viaURLSession )当您需要触发推送通知(例如通过URLSession )时,从测试中向此本地主机发送 POST 请求
  3. Get a request on localhost and execute an xcrun simctl push command在本地主机上获取请求并执行xcrun simctl push命令

For more details feel free to check out this blog post .如需更多详细信息,请随时查看此博客文章 Hope this helps.希望这可以帮助。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM