Push notifications in simulator - Not working Xcode 11.4beta
Asked Answered
A

3

8

Recently Apple has added push notifications in simulators as well. My existing app notifications are not being received in simulators.

How to simulate push notification in simulator?

Simulator supports simulating remote push notifications, including background content fetch notifications.

Reference: https://developer.apple.com/documentation/xcode_release_notes/xcode_11_4_beta_release_notes

Aimless answered 7/2, 2020 at 9:43 Comment(3)
Can you share the reference from where did you get this infoRegorge
Reference link is already available in the questionAimless
Does this answer your question? Test Rich Notifications in SimulatorBroadsword
B
13

To test remote push simply drag an APNS file onto the target simulator. This method requires the payload to contain the "Simulator Target Bundle" key. Otherwise, you will get this error message:

Invalid push notification: The file does not contain a valid JSON payload or the Simulator Target Bundle key is missing.

So add "Simulator Target Bundle" to your payload file:

{
    "aps" : {
        "alert" : {
            "title" : “Namaste”,
            "body" : “This is iOS development notification test“
        },
    },
    "Simulator Target Bundle": "com.sarunw.example-xcode-11-4"
}

The following simctl command in terminal will send notifications, and since the bundle identifier is included in the command, it does not need "Simulator Target Bundle" to be in the file:

xcrun simctl push <device> <bundle-identifier> <path-to-apns-file>

(<device> can be "booted" if you only have one device running in Simulator. Otherwise use xcrun simctl list to see a list of them, and copy the UUID of the one you want to send to.)

Bastille answered 13/2, 2020 at 10:27 Comment(2)
Just to complement @Arvind's answer, when you simply want to drag the APNS file into simulator the "Simulator Target Bundle" key's is your app target's bundle identifier and this is the same <bundle-identifier> parameter you can use with simctl command.Agraffe
path to APNS file I was missing, Thanks for postCortex
C
9

Yes, you are right. With Xcode 11.4:

Simulator supports simulating remote push notifications, including background content fetch notifications. (xcode 11.4 release notes)

You just need to drag&drop a valid APNs file onto your simulator.

However, if someone is asking why the instance method didReceive(_:withContentHandler:) of UNNotificationServiceExtension is not called, it's because the property "mutable-content" is not taking into account.

Notification Service Extensions do not work in simulated push notifications. The mutable-content key is not honored. (55822721) (xcode 11.4 release notes)

In total, this means that a smooth development use case, namely testing easily rich push notifications on local machine with simulator will not work!

Comeon answered 12/5, 2020 at 0:19 Comment(1)
Thanks god, I stopped for a moment and googled. Thought I was doing something wrong since previous payload json files work fine. 😄Vegetarian
A
7

Simulator supports simulating remote push notifications, including background content fetch notifications...

Apple didn't added push notifications to simulator, they added support for simulating push notifications. So this means that you cannot receive any push notifications, you can just simulate them.

You can read more about this in Xcode Beta Release Notes under the Simulator section.

EDIT:

Since Xcode 14 simulator supports remote push notifications if the following criteria are met.

Acquiescence answered 7/2, 2020 at 12:6 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.