Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to send simulated remote notification to iOS simulator?

Tags:

xcode

ios

Xcode 11.4 introduced sending simulated push notification on iOS simulator

How to achieve that?

like image 598
Johnny Avatar asked Feb 06 '20 14:02

Johnny


People also ask

Can we send push notification to iOS 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.

Can push notifications be test on simulator?

Conclusion. Thanks to this new functionality that we can see in the beta version of Xcode 11.4, we can already test the push notifications, and the behavior of our applications when receiving them, from the simulator itself, without the need of a physical device.

How do I get push notifications on iOS?

Go to Settings and tap Notifications. Select an app under Notification Style. Under Alerts, choose the alert style that you want. If you turn on Allow Notifications, choose when you want the notifications delivered — immediately or in the scheduled notification summary.

How do you simulate Location on iPhone simulator?

How to simulate location in Simulator. To simulate location on a Simulator, select Features menu > Location, then you will see a list of location and movement options you can simulate.


1 Answers

Answering my own question :P

Requirement: Xcode 11.4 beta or above

There are 2 ways, both needed a JSON file 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
    }
}

Method 1, Command line

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 2, 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

like image 161
Johnny Avatar answered Oct 20 '22 20:10

Johnny