8

Since Xcode 11, iOS simulators can now receive push notifications. This can be accomplished by executing a specific command or providing a JSON file to the simulator. I got this information from this blog: Test Notification on Simulator.

However, in my use case, I use Firebase Cloud Messaging to deliver notifications to the user of my app (Android and iOS both). I am unable to test the notification generated by Firebase on a simulator as the payload is different for FCM.

Is there a way to achieve this?
I found a similar question on the topic but there's no answer on it so far.

Prerak Sola
  • 9,517
  • 7
  • 36
  • 67

3 Answers3

8

For Testing push notifiction in simulator just creat a new file and paste following json object and save file in .apns , and then just drag and drop this file in simulator then push will work.

{
    "aps" : {
        "alert" : {
            "title" : "sarunw.com",
            "body" : "A asdfsadfsadf"
        },
        "badge" : 5
    },
    "Simulator Target Bundle": "bundleId"
}

For differenct payload just use following JSON object in Postman

{
 "to" : "ewebWDSSwYc:APA91bGOARO4eq9LlOfgfXPQZMK3IiRFanhzSSgkD6cVqJiPbnyuEgsvQMarDEl6Hg7_UMtFM6Hn90jyTsSENNqLNNLVjLSypR0voAO3j6orIsgabX-t0cpfJ0qpB2SzhZiXvor0r__d",
 "notification":
 {
    "title":"ABC",
    "body":"123",
    "mutable_content" : true,
    "category":"CustomSamplePush"
 },
 
 "data":
    {
        "sender_contact_code" : "+92",
     "sender_contact" : "(555) 564-8583",
     "group_name":"Testing Group",
     "type":"NewMessage",
     "type_id":"1213",
     "notification_type_id":"123213"
    }
}

with this url -> https://fcm.googleapis.com/fcm/send (Type will be POST)and in authorization you need to put Server key in Key Value, in that way you will be able to send push notification with your own formatted object.

also you can use Pusher for testing push notifiction & I have uploaded screenshot for reference.

postman_screenshot

Deryck Lucian
  • 477
  • 6
  • 18
Chandaboy
  • 1,302
  • 5
  • 10
  • 1
    Your first approach generates a notification on the Simulator but I cannot pass any data and moreover no notification methods in AppDelegate.swift are invoked. The second approach can be used to send FCM notification but it does not work with the Simulators. **It only works with a physical iOS device.** – Prerak Sola Sep 21 '20 at 16:24
  • 1
    Yes you are right but I think you need to put custom Data key in { "aps" : { "alert" : { "title" : "sarunw.com", "body" : "A asdfsadfsadf" }, "badge" : 5 }, "Simulator Target Bundle": "bundleId", data: {} } then when you click on notification then it will call usernotification Delegate method – Chandaboy Sep 21 '20 at 16:48
0

Since Xcode 12 (not 11), it's possible to registrer to push and receive via the APNS payload. If the drag & drop it not working, you can use Terminal :

xcrun simctl push booted YOUR_BUNDLE payload.json

With :

  • "booted" = the current simulator
  • "payload.json" = the APNS file on your Mac.

It may look like:

{
    "Simulator Target Bundle": "xxx.xxx.xx ",
    "aps":{
                    "alert":{
                        "title":"XXX",
                        "body":"YYY YYYY"
                    },
                    "badge" : 5,
                    "data":{
                        // stuff
                    },
                    "mutable-content":1
                }
}
 
Medhi
  • 2,656
  • 23
  • 16
  • Yes its the command the OP is talking about. But how do you test that sending a request to firebase arrives normally in the simulator ? – julio Mar 23 '23 at 09:39
0

maybe you found the solution a long time ago but here is how i made it work: to allow FCM notification to be catch in your app you need to add that in your .apns file

"gcm.message_id":"20021"

here is the apns file i use to test FCM notifs on my Iphone simulator:

{
    "Simulator Target Bundle": "XXX.XXXXXX.XXX",
    "aps": {
      "alert" : {
         "title" : "Test notif",
         "body" : "Should update widget && refresh feed"
      },
   },
   "gcm.message_id":"20021",
   "data": {}
}