Get Push-notification in simulator iOS

Xcode 11.4 onwards Testing push notifications in the iOS simulator make it a lot easier to add support for remote notifications.

Required – Xcode 11.4 +

Step 1:

Enable permission to use push notifications

1. create our UNUserNotificationCenter object

2. requests authorization from the user to display our notifications:

Complete Code

func application(_ application: UIApplication, didFinishLaunchingWithOptions launchOptions: [UIApplication.LaunchOptionsKey: Any]?) -> Bool {
    // Override point for customization after application launch.
    let notificationCenter = UNUserNotificationCenter.current()
    notificationCenter.requestAuthorization(options: [.alert, .sound]) { granted, error in
        // handle permissions here
    }

    return true
}

There are multiple ways to send notification:

Method 1: using .apns file drag n drop to simulator

create .apns file using textEditor or by using terminal

//for terminal
nano payload.apns

paste following code if in textEditor/ terminal

{
    "Simulator Target Bundle": "com.sandesh.app",
    "aps": {
        "alert": {
            "body": "New alert please check",
            "title": "test"
        },
        "category": "file_added",
        "mutable-content": 1
    },
    
    "event_type": "file_added"
}

and save it. if you are creating using terminal then press CTRL X, Y, Enter to save it.

And you are Done. Just drag drop payload.apns file in simulator

Method 2 – Using JSON file

Create payload.json file and paste the following code in that

{
    "Simulator Target Bundle": "com.sandesh.app",
    "aps": {
        "alert": {
            "body": "New alert please check",
            "title": "test"
        },
        "category": "file_added",
        "mutable-content": 1
    },
    
    "event_type": "file_added"
}

save file and run following command in terminal

xcrun simctl push booted com.sandesh.app /Users/Sandesh/Desktop/payload.json (you should put your payload.json file path here)

Method 3 – Using poes

Follow the steps in terminal

Install Mint

$ git clone https://github.com/yonaskolb/Mint.git
$ cd Mint
$ make

You can install Poes using Mint:
$ mint install AvdLee/Poes

After which you can easily send a notification as follows:
$ poes send --bundle-identifier com.sandesh.app --verbose

note: replace com.sandesh.app with your bundle identifier 

You will get result like bellow and push-notification in simulator 
$ poes send --bundle-identifier com.sandesh.app --verbose
Generated payload:

{
  "aps" : {
    "alert" : {
      "title" : "Default title",
      "body" : "Default body"
    },
    "mutable-content" : false
  }
}

Sending push notification...
Push notification sent successfully

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out /  Change )

Google photo

You are commenting using your Google account. Log Out /  Change )

Twitter picture

You are commenting using your Twitter account. Log Out /  Change )

Facebook photo

You are commenting using your Facebook account. Log Out /  Change )

Connecting to %s