How can I test Apple Push Notification Service without an iPhone?
Asked Answered
S

15

109

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.

Soche answered 3/7, 2009 at 19:26 Comment(2)
It's time to update best answer. Library from my answer is so useful, thanks 'acoomans' for that!Lights
While you can test payloads using the simulator. The simulator has a bug and still doesn't seem to honor the mutable-content key. See hereRogelioroger
D
63

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:

Notification on the iOS simulator

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!

Dromous answered 5/2, 2020 at 23:6 Comment(7)
This is not a release version yetThreadfin
Ok, but this doesn't trigger didReceiveRemoteNotification? I can't get anything from tapping on the message.Pasquale
Did you try using didReceiveLocalNotification (just for simulation purposes)? Apart from that: You should probably just use the methods provided by the UserNotifications framework; I guess those will work...Dromous
do you know a way to simulate this using OneSignal push notification?Delp
@mnemonic23 If you want to test the handling of incoming OneSignal notifications via this method, you will have to find out how the OneSignal push notification payload looks like and replicate that exact payload. But I can't think of a scenario where that would make sense: If you want to test your app's handling of the payload, that's independent from the OneSignal delivery mechanism; if you want to test whether the OneSignal delivery mechanism works, this lokal push mocking won't help you.Dromous
gif: twitter.com/sarunw/status/1465410638663065603Mellie
@Dromous Is it possible to test background notifications in a simulator? I ask this because, system decides the best time to schedule background tasks based on conditions. Due to this, background fetch and background processing cannot be tested in simulator, but what about silent remote notifications (background pushes)?Emanative
M
85

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.

Monica answered 4/7, 2009 at 20:19 Comment(3)
i ran into the same problem, you answer is not clear talking about second part of the question: 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.Buskus
This is tutorial link for support Push notification in Simulator. medium.com/swlh/…Wastepaper
But I have some good news for you. Xcode 11.4 beta is out and the best part about this release for me is that we can finally test push notifications in the iOS Simulator! Following is the sample implementation - medium.com/better-programming/…Cleanlimbed
D
63

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:

Notification on the iOS simulator

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!

Dromous answered 5/2, 2020 at 23:6 Comment(7)
This is not a release version yetThreadfin
Ok, but this doesn't trigger didReceiveRemoteNotification? I can't get anything from tapping on the message.Pasquale
Did you try using didReceiveLocalNotification (just for simulation purposes)? Apart from that: You should probably just use the methods provided by the UserNotifications framework; I guess those will work...Dromous
do you know a way to simulate this using OneSignal push notification?Delp
@mnemonic23 If you want to test the handling of incoming OneSignal notifications via this method, you will have to find out how the OneSignal push notification payload looks like and replicate that exact payload. But I can't think of a scenario where that would make sense: If you want to test your app's handling of the payload, that's independent from the OneSignal delivery mechanism; if you want to test whether the OneSignal delivery mechanism works, this lokal push mocking won't help you.Dromous
gif: twitter.com/sarunw/status/1465410638663065603Mellie
@Dromous Is it possible to test background notifications in a simulator? I ask this because, system decides the best time to schedule background tasks based on conditions. Due to this, background fetch and background processing cannot be tested in simulator, but what about silent remote notifications (background pushes)?Emanative
H
59

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.

Hylozoism answered 13/12, 2012 at 17:48 Comment(4)
is there any sample implementation of this found somewhere?Highpriced
I don't have one (my test implementation takes a different route, although it really should do what I suggested above). Classic "do as I say, not as I do" scenario I guess.Hylozoism
This is a nice answer.Scheldt
Elaboration on You can't test real push notifications. -- on the simulator, you will still fail if you call UIApplication.shared.registerForRemoteNotifications() -- you will get 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}Silvasilvain
L
32

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
Lights answered 15/7, 2013 at 6:43 Comment(0)
U
24

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.

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

Utimer answered 2/4, 2020 at 13:51 Comment(0)
C
14

With macOS 13 and Xcode 14, when on Mac computers with Apple silicon or T2 processors, it’s now possible to receive real push notifications as didRegisterForRemoteNotificationsWithDeviceToken will return a real token.

See Xcode 14 release notes:

Simulator now supports remote notifications in iOS 16 when running in macOS 13 on Mac computers with Apple silicon or T2 processors. Simulator supports the Apple Push Notification Service Sandbox environment. Your server can send a remote notification to your app running in that simulator by connecting to the APNS Sandbox (api.sandbox.push.apple.com). Each simulator generates registration tokens unique to the combination of that simulator and the Mac hardware it’s running on.

https://developer.apple.com/documentation/xcode-release-notes/xcode-14-release-notes

Capitalize answered 23/9, 2022 at 14:12 Comment(2)
Yess!! Finally. I was looking for this answer as I accidentally found it out today.Omdurman
Does this work for watchOS simulators? Or only for iOS?Emanative
A
7

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.

Amara answered 17/8, 2009 at 16:23 Comment(0)
S
4

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

Stop answered 12/2, 2020 at 5:43 Comment(1)
Duplicate to a more clear and concise answer provided a week earlier.Submerge
S
3

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];
Sinkage answered 1/7, 2014 at 15:19 Comment(0)
Y
3

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
Yachtsman answered 9/2, 2020 at 14:52 Comment(0)
K
1

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'
Kwangju answered 21/9, 2018 at 13:46 Comment(3)
please explain more!Ravi
@NegarMoshtaghi Please open the link which I had provided with answer it have full process how to use and how its working and all.Kwangju
This library is 5-6 years old, is it still functional?Annabelleannabergite
D
1

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.

Dante answered 19/11, 2020 at 14:37 Comment(0)
T
1

For a more complete answer since Xcode 11.4 and as of Xcode 12.4:

  1. 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.

  2. Notification Service Extensions do not work in simulated push notifications. The mutable-content key is not honored. (55822721)

Reference: Xcode 11.4 Release Notes

Tomahawk answered 24/2, 2021 at 1:28 Comment(0)
L
0

Most answers suggest using a third-party Cocoapods/SPM dependency. But these dependencies can easily become unsupported for quite a while after a new iOS or Xcode release.

Here is just a lightweight solution that will work in any case and in any weather:

  1. Start any HTTP server on localhost before running the tests (e.g.: ruby/python)
  2. Send a POST request to this localhost from the test when you need to trigger a push notification (e.g. via URLSession)
  3. Get a request on localhost and execute an xcrun simctl push command

For more details feel free to check out this blog post. Hope this helps.

Leasehold answered 8/12, 2022 at 20:12 Comment(0)
O
0

Hurray!! XCode14 now supports real remote push-notifications sent by server in sandbox environment.

Check here release notes for system configurations. https://developer.apple.com/documentation/xcode-release-notes/xcode-14-release-notes

Simulator now supports remote notifications in iOS 16 when running in macOS 13 on Mac computers with Apple silicon or T2 processors. Simulator supports the Apple Push Notification Service Sandbox environment. Your server can send a remote notification to your app running in that simulator by connecting to the APNS Sandbox (api.sandbox.push.apple.com). Each simulator generates registration tokens unique to the combination of that simulator and the Mac hardware it’s running on. See User Notifications for more information.

Ostracize answered 27/7, 2023 at 9:23 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.