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

ghz 1years ago ⋅ 4112 views

Question

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.


Answer

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 theSimulator Target Bundle to your own app's identifier and adjust the payload to your debugging needs!