iosxcode

How to send simulated remote notification to iOS simulator?


Xcode 11.4 introduced sending simulated push notification on iOS simulator

How to achieve that?


Solution

  • Answering my own question :P

    Requirement: Xcode 11.4 beta or above

    Method 1, Online tool

    Use online tool to test push notifications, I personally found it more useful than methods below, it's more straightforward and does not require remembering simctl command

    Website: http://pushplayground.com/

    Method 2, Command line

    There are 2 ways, both needed a JSON formatted with a valid Apple Push Notification Service payload with .apns extension

    Example apns: (Save it as XXX.apns file)

    {
        "aps":{
            "alert":"Test",
            "sound":"default",
            "badge":1
        }
    }
    

    Using simctl to send simulated notification

    $ xcrun simctl push <SIMULATOR_DEVICE_ID> <YOUR_APP_BUNDLE_ID> <APNS_FILE_NAME>
    

    Usage:

    $ xcrun simctl push A0AF405F-FE73-45DA-8D7D-F1FE37821992 com.example.my-app test.apns
    

    You can find the simulator device id with

    xcrun simctl list
    

    Example: enter image description here

    Method 3, Drag and Drop .apns file to simulator

    When using drag and drop, you .apns file needed to include a key that indicates your app bundle id

    Key name: Simulator Target Bundle

    Value: Your app bundle id Example:

    {
        "Simulator Target Bundle": "YOUR_APP_BUNDLE_ID",
        "aps":{
            "alert":"Test",
            "sound":"default",
            "badge":1
        }
    }
    

    After that, simply drag and drop the file into the simulator, and voilà! enter image description here