How can I test Apple Push Notification Service without an iPhone?
This answer is outdated. As of 2020 / Xcode 11.4 it's now possible to test push notifications in the simulator
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. That also means you'll probably have to be accepted into the apple iPhone developer program and pay your $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.
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.
To trigger this from a different class (like a 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.
Here's an example of how to provide the testNotification
above:
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.
Testing push notifications using the 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. The Xcode 11.4 release notes have the following to say about the new feature:
Simulator supports simulating remote push notifications, including background content fetch notifications. In Simulator, drag and drop an APNs file onto the target simulator. The file must be a JSON file with a valid Apple Push Notification Service payload, including the “aps” key. 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. If the file contains “Simulator Target Bundle” the bundle identifier is not required, otherwise you must provide it as an argument (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:
{
"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:
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!