SimulatorRemoteNotifications 0.0.3

SimulatorRemoteNotifications 0.0.3

测试已测试
语言语言 Obj-CObjective C
许可证 BSD
发布最后发布2014年12月

Arnaud Coomans 维护。




  • Arnaud Coomans

SimulatorRemoteNotifications 是一个用于向 iOS 模拟器发送模拟远程通知的库。

该库通过嵌入一个监听包含 JSON 格式负载的 UDP 数据包的迷你服务器,并提供了向迷你服务器发送通知的服务来扩展 UIApplication

该项目包括名为 iOS Simulator Notifications 的 MacOSX 应用程序,以帮助您发送模拟通知。

请注意,SimulatorRemoteNotifications 不通过 Apple 的推送服务发送通知。

Build Status

安装

安装静态库

  1. 将项目文件复制到您的项目中
  2. 目标 > 构建阶段 > 与二进制链接库 下链接您的可执行文件与库,然后添加 libSimulatorRemoteNotifications.a
  3. 为目标设置 OTHER_LINKER_FLAGS="-ObjC"

手动安装

  1. 克隆此仓库
  2. SimulatorRemoteNotifications 目录中的文件添加到您的项目中
  3. 为目标设置 OTHER_LINKER_FLAGS="-ObjC"

用法

监听模拟远程通知

首先将以下代码 #import "UIApplication+SimulatorRemoteNotifications.h" 添加到您的应用程序代理中。

然后实现 application:didReceiveRemoteNotification:application:didReceiveRemoteNotification:fetchCompletionHandler: (后台通知,iOS7)。

最后调用开始监听模拟远程通知

- (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions {

    ...

    #if DEBUG
        [application listenForRemoteNotifications];
    #endif

    return YES;
}

当调用 listenForRemoteNotifications 时,application:didRegisterForRemoteNotificationsWithDeviceToken: 会收到以下格式的令牌,而不是随机字符:simulator-remote-notification=IP:PORT

默认端口号是 9930。如果您想更改端口号,请在调用 listenForRemoteNotifications 之前使用 setRemoteNotificationsPort

application.remoteNotificationsPort = 1234;

现在,要发送一个远程通知,向 localhost:9930 发送一个 UDP 数据包。

请注意,如果您在应用后台发送通知,只有在将应用带到前台时才会调用 application:didReceiveRemoteNotification:fetchCompletionHandler:

使用 iOS Simulator Notifications 应用发送模拟远程通知

该项目附带一个名为 iOS Simulator Notifications 的 OSX 应用程序,用于帮助您向 iOS 模拟器发送通知。

构建并运行目标,你将获得一个友好的界面来向模拟器中的应用程序发送通知(请参阅截图)。

在测试中发送模拟远程通知

首先将 #import "ACSimulatorRemoteNotificationsService.h" 添加到你的测试中

使用以下方法发送你的通知

[[ACSimulatorRemoteNotificationsService sharedService] send:@{@"message":@"message"}];

你可以使用以下方法更改主机(默认:127.0.0.1)和端口号(默认:9930)

[[ACSimulatorRemoteNotificationsService sharedService] setRemoteNotificationsPort:1234];
[[ACSimulatorRemoteNotificationsService sharedService] setRemoteNotificationsHost:@"10.0.0.1"];

从命令行发送模拟远程通知

你也可以通过使用 netcat 从终端发送模拟远程通知

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

截图

screenshots screenshots

示例

您可以通过查看 SimulatorRemoteNotifications.xcodeproj 获取示例

  • SimulatorRemoteNotificationsExample 中,调用 application:didReceiveRemoteNotification: 方法
  • SimulatorRemoteNotificationsBackgroundExample 中,调用 application:didReceiveRemoteNotification:fetchCompletionHandler: 方法

文档

如果你已经安装了 appledoc,你可以运行相应的目标来生成文档

注意

(真实)Apple远程通知的内容长度限制为256字节。