SimulatorRemoteNotifications
SimulatorRemoteNotifications is a library to send mock remote notifications to the iOS simulator.
The library extends UIApplication by embedding a mini server that listen for UDP packets containing JSON-formated payload, and a service to send notifications to the mini server.
This project includes the iOS Simulator Notifications MacOSX app to help you send the mock notifications.
Note that SimulatorRemoteNotifications does not send notification through Apple's Push Service.
Install
CocoaPods
Install withAdd a pod entry to your Podfile:
pod 'SimulatorRemoteNotifications', '~> 0.0.3'
Install the pod(s) by running:
pod install
Install the static library
- Copy the project file in your project
- Link your binary with the library, under Target > Build Phases > Link binary with libraries then add the libSimulatorRemoteNotifications.a
- set
OTHER_LINKER_FLAGS="-ObjC"
for your target
Install manually
- clone this repository
- add the files in the SimulatorRemoteNotifications directory to your project
- set
OTHER_LINKER_FLAGS="-ObjC"
for your target
Usage
Listening for mock remote notifications
First add #import "UIApplication+SimulatorRemoteNotifications.h"
to your application delegate.
Then implement either application:didReceiveRemoteNotification: or application:didReceiveRemoteNotification:fetchCompletionHandler: (background notification, iOS7).
Finally call start listening for mock remote notifications:
- (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions {
...
#if DEBUG
[application listenForRemoteNotifications];
#endif
return YES;
}
When listenForRemoteNotifications is called, application:didRegisterForRemoteNotificationsWithDeviceToken: receives a token in the following format instead of random characters: simulator-remote-notification=IP:PORT
The default port number is 9930. If you want to change the port, use setRemoteNotificationsPort before calling listenForRemoteNotifications:
application.remoteNotificationsPort = 1234;
Now, to send a remote notification, send an udp packet to localhost:9930.
Note that if you send a notification while the app is in the background, application:didReceiveRemoteNotification:fetchCompletionHandler: will only be called when you bring the app to the foreground.
Sending a mock remote notification with the iOS Simulator Notifications app
The project comes with a OSX app called iOS Simulator Notifications to help you send notifications to the iOS Simulator.
Build and run the target and you'll have a nice interface to send notification to your app in the simulator (see screenshots).
Sending a mock remote notification in tests
First add #import "ACSimulatorRemoteNotificationsService.h"
to your test
Send you notification with
[[ACSimulatorRemoteNotificationsService sharedService] send:@{@"message":@"message"}];
You can change the host (default: 127.0.0.1) and port (default: 9930) with
[[ACSimulatorRemoteNotificationsService sharedService] setRemoteNotificationsPort:1234];
[[ACSimulatorRemoteNotificationsService sharedService] setRemoteNotificationsHost:@"10.0.0.1"];
Sending a mock remote notification from the command line
You can also send mock remote notifications from the terminal by using netcat:
echo -n '{"message":"message"}' | nc -4u -w1 localhost 9930
Screenshot
Examples
You can look at SimulatorRemoteNotifications.xcodeproj for examples:
- in SimulatorRemoteNotificationsExample, the application:didReceiveRemoteNotification: method is called
- in SimulatorRemoteNotificationsBackgroundExample, the application:didReceiveRemoteNotification:fetchCompletionHandler: method is called
Documentation
If you have appledoc installed, you can generate the documentation by running the corresponding target.
Note
(Real) Apple remote notifications are limited to 256 bytes content length.