How can I test Apple Push Notification Service without an iPhone?
IosTestingApple Push-NotificationsIos SimulatorIos Problem Overview
Is it possible test the Apple Push Notification Services without an iPhone application? (Creating an emulator on windows?)
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.
Ios Solutions
Solution 1 - Ios
> 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.
Solution 2 - Ios
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.
Solution 3 - Ios
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!
Solution 4 - Ios
Nowadays, we can test push notifications with this library.
It's pretty easy to send push via terminal:
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
Solution 5 - Ios
Apple has supporting push notification for simulators. iOS 13.4 and above or Xcode 11.4 and above.
as usually create Xcode project and implement a user notification and authorization permission.
run your application in simulator iOS 13.4 and above.
put your application in background.
- Create an APNS payload file named "payload.apns"
{
"aps": {
"alert": {
"title": "Test Push",
"body": "Success! Push notification in simulator! 🎉",
"sound": "default"
},
"badge": 10
},
"Simulator Target Bundle": "com.company.app"
}
- Drag and drop to your iOS simulator.
right now your push notification will appear on simulator.
And also you can simulate push notification by terminal
get your simulator identifier by opening Window->Devices and Simulators and choose your targeted simulator and right click and copy your identifier.
Now build a terminal command like
xcrun simctl push <simulator-identifier> <path-to-payload-file>
ex:
xcrun simctl push 27A23727-45A9-4C12-BE29-8C0E6D1E5360 payload.apns
run this command and simulate push notification in simulator
Solution 6 - Ios
The simulator does not do Push Notifications.
And to push from a server, you have to have device(s) to push to as well as your app on that device.
The token contains the app identity as well as the device ID.
Solution 7 - Ios
Xcode 11.4 Beta starts supporting push notification in Simulator
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
Release Notes: https://developer.apple.com/documentation/xcode_release_notes/xcode_11_4_beta_release_notes
Solution 8 - Ios
you have to use
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];
Solution 9 - Ios
Apart from @fredpi's answer, you can also use the Poes command-line tool. It allows us to quickly test remote push notifications on the iOS simulator without the hassle of creating a JSON payload file.
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
The following command could be enough to send out a simple push notification with a default title and body:
$ 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
Solution 10 - Ios
Yes, you can check push notification on the simulator, but you have to use a library in your app named SimulatorRemoteNotifications. By which, by using just 4-5 steps, you can test push notification on the simulator.
They also provide PODs too:
pod 'SimulatorRemoteNotifications', '~> 0.0.3'
Solution 11 - Ios
It looks like now we have very powerful lib https://github.com/ctreffs/SwiftSimctl
At least it's much more power than lib SimulatorRemoteNotifications
which were mentioned here. And which was obsoleted also.
Solution 12 - Ios
For a more complete answer since Xcode 11.4 and as of Xcode 12.4:
-
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 that matches the target application’s bundle identifier. simctl also supports sending simulated push notifications.
-
Notification Service Extensions do not work in simulated push notifications. The mutable-content key is not honored. (55822721)
Reference: Xcode 11.4 Release Notes
Solution 13 - Ios
iOS:Push Notification testing from Terminal to App :
https://medium.com/@lokesh.vyas4/ios-push-notification-from-terminal-to-app-d01019934758