【问题标题】:How can I test Apple Push Notification Service without an iPhone?如何在没有 iPhone 的情况下测试 Apple 推送通知服务?
【发布时间】:2010-11-08 00:14:03
【问题描述】:

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

如果不是,我该如何测试?是否有为此编译的免费示例应用程序?

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

【问题讨论】:

  • 是时候更新最佳答案了。我的答案中的图书馆非常有用,感谢“acoomans”!
  • 虽然您可以使用模拟器测试有效负载。模拟器有一个错误,似乎仍然不支持mutable-content 键。见here

标签: ios testing apple-push-notifications ios-simulator


【解决方案1】:

此答案已过时。从 2020 / Xcode 11.4 开始,现在可以在模拟器中测试推送通知

an answer below看到这个完整的解释

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

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

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

【讨论】:

  • 我遇到了同样的问题,你的回答不清楚谈论问题的第二部分: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.
  • 这是在模拟器中支持推送通知的教程链接。 medium.com/swlh/…
  • 但我有一些好消息要告诉你。 Xcode 11.4 beta 已经发布,这个版本对我来说最好的部分是我们终于可以在 iOS 模拟器中测试推送通知了!以下是示例实现 - medium.com/better-programming/…
【解决方案2】:

模拟器不做推送通知。

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

令牌包含应用身份以及设备 ID。

【讨论】:

    【解决方案3】:

    您无法测试真正的推送通知。 但是,您可以通过以编程方式创建一个并手动触发 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 来使用。

    【讨论】:

    • 是否在某处找到了这个的示例实现?
    • 我没有(我的测试实现采用了不同的路线,尽管它确实应该按照我上面的建议进行)。我猜是经典的“照我说的做,而不是照我做”的场景。
    • 这是一个不错的答案。
    • 详述You can't test real push notifications. -- 在模拟器上,如果你调用UIApplication.shared.registerForRemoteNotifications(),你仍然会失败 -- 你会得到2021-01-16T13:11:35-0700 error : didFailToRegisterForRemoteNotificationsWithError: Error Domain=NSCocoaErrorDomain Code=3010 "remote notifications are not supported in the simulator" UserInfo={NSLocalizedDescription=remote notifications are not supported in the simulator}
    【解决方案4】:

    现在,我们可以使用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
    

    【讨论】:

      【解决方案5】:

      你必须使用

      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】:

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

        他们也提供 POD:

        pod 'SimulatorRemoteNotifications', '~> 0.0.3'
        

        【讨论】:

        • 请多解释!
        • @NegarMoshtaghi 请打开我提供的答案的链接,它有完整的过程如何使用以及它是如何工作的。
        • 这个库5-6岁了,还能用吗?
        【解决方案7】:

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

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

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

        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"
            }
        }
        

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

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

        【讨论】:

        • 这还不是发布版本
        • 好的,但这不会触发 didReceiveRemoteNotification?我无法通过点击消息获得任何信息。
        • 您是否尝试过使用didReceiveLocalNotification(仅用于模拟目的)?除此之外:您可能应该只使用UserNotifications 框架提供的方法;我想这些会起作用的......
        • 您知道使用 OneSignal 推送通知模拟这种情况的方法吗?
        • @mnemonic23 如果您想通过此方法测试对传入 OneSignal 通知的处理,您将必须找出 OneSignal 推送通知有效负载的外观并复制该确切有效负载。但我想不出一个有意义的场景:如果你想测试你的应用程序对有效负载的处理,那是独立于 OneSignal 传递机制的;如果你想测试 OneSignal 传递机制是否有效,这个 lokal push mocking 对你没有帮助。
        【解决方案8】:

        除了@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】:

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

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

          simctl 还支持发送模拟推送通知。如果文件包含“模拟器目标包”,则不需要包标识符,否则您必须将其作为参数提供 (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

          【讨论】:

          • 复制到一周前提供的更清晰简洁的answer
          【解决方案10】:

          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 模拟器。

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

          你也可以通过终端模拟推送通知

          通过打开Window->Devices and Simulators获取您的模拟器标识符,然后选择您的目标模拟器并右键单击并复制您的标识符。

          现在构建一个类似的终端命令

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

          例如:

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

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

          【讨论】:

            【解决方案11】:

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

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

            【讨论】:

              【解决方案12】:

              对于自 Xcode 11.4 和自 Xcode 12.4 起的更完整答案:

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

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

              参考:Xcode 11.4 Release Notes

              【讨论】:

                猜你喜欢
                • 2017-11-18
                • 2016-09-21
                • 1970-01-01
                • 1970-01-01
                • 1970-01-01
                • 1970-01-01
                • 2015-05-26
                • 1970-01-01
                • 1970-01-01
                相关资源
                最近更新 更多