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

时间:2009-07-03 19:26:42

标签: iphone apple-push-notifications

是否可以在没有iPhone应用程序的情况下测试Apple推送通知服务? (在Windows上创建模拟器?)

如果不是,我该怎么测试呢?是否有编译的免费样本应用程序?

我创建了服务器提供程序,但我需要测试functionallity。

12 个答案:

答案 0 :(得分:71)

(此答案可能在2013年后过时)

很抱歉,但您需要找一些硬件来测试此功能。

模拟器中没有推送通知。它们需要iTunes Connect的配置文件,因此需要安装在设备上。这也意味着你可能不得不接受苹果iPhone开发者计划并支付99美元。

从好的方面来说,通过iPhone OS 3.0更新,您可以在任何设备上测试此功能,包括第一代iPhone。

答案 1 :(得分:52)

您无法测试真实推送通知。 ,您可以通过以编程方式创建一个并手动触发AppDelegate的- application:application didReceiveRemoteNotification:notification方法来测试应用的响应到模拟推送通知。

要从另一个类(如UIViewController)触发它:

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

testNotification应该具有与真实通知相同的格式,即包含属性列表对象和NSNull的NSDictionary。

以下是如何提供上述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"];

这应该创建一个合理的通知NSDictionary来使用。

答案 2 :(得分:30)

如今,我们可以使用this library测试推送通知。

通过终端发送推送非常容易:

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

答案 3 :(得分:16)

使用Xcode 11.4 iOS模拟器测试推送通知

Xcode 11.4 开始,现在可以通过将.apns文件拖放到iOS模拟器上来模拟推送通知。 Xcode 11.4 release notes对新功能有以下说法:

Simulator支持模拟远程推送通知,包括 后台内容获取通知。在模拟器中,拖放一个 APNs文件到目标模拟器上。该文件必须是带有以下内容的JSON文件: 有效的Apple Push Notification Service有效负载,包括“ aps” 键。它还必须包含顶级“模拟器目标捆绑包” ,其中包含 与目标应用程序的包标识符匹配的字符串值。

simctl还支持发送模拟的推送通知。如果文件 包含“模拟器目标捆绑包”,捆绑包标识符不是 必填,否则您必须将其作为参数提供(8164566):

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

示例

以下是针对系统设置应用的此类.apns文件的示例:

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

将其拖放到目标模拟器上将显示通知并设置徽章:

Notification on the iOS simulator

现在,要将其用于调试目的,您只需Simulator Target Bundle更改为自己的应用程序的标识符,并调整有效负载即可满足调试需求!

答案 4 :(得分:5)

模拟器不执行推送通知。

要从服务器推送,您必须拥有设备以及该设备上的应用程序。

令牌包含应用标识和设备ID。

答案 5 :(得分:4)

你必须使用

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];

答案 6 :(得分:4)

Apple支持模拟器的推送通知。 iOS 13.4及更高版本或Xcode 11.4及更高版本。

通常创建 Xcode 项目并实施用户通知和授权权限。

模拟器iOS 13.4和更高版本中运行您的应用程序。

将您的应用程序置于后台。

  1. 创建一个名为“ 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. 拖放到您的iOS模拟器中。

现在您的推送通知将出现在模拟器上。

您还可以通过终端模拟推送通知

通过打开窗口->设备和模拟器并选择目标模拟器并右键单击并复制您的标识符来获取模拟器标识符。

现在建立一个终端命令,例如

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

例如:

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

运行此命令并在模拟器中模拟推送通知

答案 7 :(得分:3)

Xcode 11.4 Beta开始在模拟器中支持推送通知

Simulator支持模拟远程推送通知,包括后台内容获取通知。在模拟器中,将APNs文件拖放到目标模拟器上。该文件必须是带有有效Apple Push Notification Service有效负载(包括“ aps”密钥)的JSON文件。它还必须包含顶层的“ Simulator Target Bundle”,其字符串值必须与目标应用程序的bundle标识符相匹配。

simctl还支持发送模拟的推送通知。如果文件包含“ Simulator Target Bundle”,则不需要束标识符,否则,必须将其作为参数提供(8164566):

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

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

答案 8 :(得分:2)

除了@fredpi的答案,您还可以使用Poes命令行工具。它使我们能够在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

以下命令足以发送带有默认标题和正文的简单推送通知:

$ 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

答案 9 :(得分:1)

是的,您可以在模拟器上检查推送通知,但必须在名为SimulatorRemoteNotifications的应用中使用一个库。通过仅需4-5个步骤,您就可以在模拟器上测试推送通知。

他们也提供POD

pod 'SimulatorRemoteNotifications', '~> 0.0.3'

答案 10 :(得分:0)

看来我们现在拥有非常强大的lib https://github.com/ctreffs/SwiftSimctl

至少它比这里提到的lib SimulatorRemoteNotifications强大得多。而且也被淘汰了。

答案 11 :(得分:0)

从 Xcode 11.4 到 Xcode 12.4 的更完整答案:

  1. Simulator 支持模拟远程推送通知,包括后台内容获取通知。在模拟器中,将 APNs 文件拖放到目标模拟器上。该文件必须是具有有效 Apple 推送通知服务负载的 JSON 文件,包括“aps”键。它还必须包含一个顶级“模拟器目标捆绑包”,其字符串值与目标应用程序的捆绑包标识符相匹配。 simctl 还支持发送模拟推送通知。

  2. 通知服务扩展在模拟推送通知中不起作用。不遵守可变内容键。 (55822721)

参考:Xcode 11.4 Release Notes

相关问题