Nevin Paul
Nevin Paul

Reputation: 801

How to test PushNotification in IOS simulator Xcode

How to Test push notification in IOS simulators using Xcode 11.4 and above without using an ios device.

Upvotes: 4

Views: 6818

Answers (2)

Ben Butterworth
Ben Butterworth

Reputation: 28532

I've just noticed that I can push notifications via. APNs to iOS 16 simulators running on my mac. It worked without needing to create "files" and emulate push notifications. Perhaps you need Xcode 14.0, that's what I used.

Just push the notification to the device as normal (e.g. using firebase_messaging, or my script: https://github.com/ben-xD/push/blob/main/tools/ios/send_ios_example)

enter image description here

Upvotes: 0

Nevin Paul
Nevin Paul

Reputation: 801

Xcode 11.4 and above support testing of push notifications using simulators.

To test,

Option 1 - Using the "ControlRoom" Mac app created by Paul Hudson (author of https://www.hackingwithswift.com/)

Controlroom is an amazing app which i came across recently which allows to control the simulators. It provides a nice UI to customise the notifications. Special thanks to Paul Hudson for sharing the source code in git. Git URL - https://github.com/twostraws/ControlRoom

enter image description here

Option 2 - Using Terminal

run the following command in the terminal

xcrun simctl push <simulator identifier> <bundle identifier of the app> <pushcontentfile>.apns"

How to get Simulator identifier using Xcode

Xcode Menu => Window => Devices and Simulators

enter image description here

Format of .apns file

  • Save the push notification payload (json format) to a file with an extension ".apns"
{
    "aps": {
        "alert": "Push Notifications Test",
        "sound": "default",
        "badge": 1
    }
}

Option 3 - Drag and Drop .apns file to the simulator

The .apns file should contain the bundle identifier of the app as a part of the payload

{
    "Simulator Target Bundle": "<bundle identifier of the app>",
    "aps": {
        "alert": "Push Notifications Test",
        "sound": "default",
        "badge": 1
    }
}

Upvotes: 20

Related Questions