Send messages to the scene

I saw onnoffitacation in the Behavior configuration of Reality Composer pro, which asked me to enter the Nofficatition name, that is to say, this requires swift in Xcode to send a message. There is a message name in the message, so I hope you can write a list for me how to use Swift in Xcode to send a message containing the message name.(There is an answer in https://developer.apple.com/forums/thread/756978, but it doesn't work.)

and in the time line in Reality Composer Pro, there is a Notification action, which is used to send messages to swift. How can I ask swift to detect whether the Notification action has sent a message?(There is an answer in https://developer.apple.com/videos/play/wwdc2024/10102/, but it doesn't work.)

I have asked this question before (https://developer.apple.com/forums/thread/756978). Those answers were available before, but now they are all invalid in the latest system. I hope you can help me. Thank you.

Hello @lijiaxu,

I'm not able to replicate the issues you are encountering. I'm able to trigger a timeline from a SwiftUI button, and receive notifications fired within a Timeline in my SwiftUI ImmersiveView.

In the sample code project that is associated with WWDC24 session 10102: Compose interactive 3D content in Reality Composer Pro, onReceive(_:perform:) is used to listen to the notifications fired. For instance, in the MoveToPoppy Timeline, the ReachToPoppy notification is fired 4 seconds in and the hero robot's HeroRobotRuntimeComponent is updated.

Additionally, I'm able to add an OnNotification Behavior to the Hero robot to manually trigger a timeline within SwiftUI. I have specified this component to use the Notification Name of MoveToPoppySwiftUINotification, with an Action of "MoveToPoppy". I'm able to trigger this from SwiftUI using an attachment on the RealityView in ImmersiveView. This allows it to receive the correct scene from the realityKitScene environment variable:

RealityView { content, attachments in
    // Add the initial RealityKit content.
    if let immersiveContentEntity = try? await Entity(named: "Immersive", in: realityKitContentBundle) {
        content.add(immersiveContentEntity)
    }

    if let movePoppyButton = attachments.entity(for: "MovePoppyButton") {
        movePoppyButton.position = [0, 1.0, -1.0]
        content.add(movePoppyButton)
    }
} attachments: {
    Attachment(id: "MovePoppyButton") {
        Button {
            NotificationCenter.default.post(
              name: NSNotification.Name("RealityKit.NotificationTrigger"),
              object: nil,
              userInfo: [
                "RealityKit.NotificationTrigger.Scene": scene,
                "RealityKit.NotificationTrigger.Identifier": "MoveToPoppySwiftUINotification"
              ]
            )
        } label: {
            Text("Move Poppy")
        }.padding(20).glassBackgroundEffect()
    }
}

Are you having difficulties replicating this behavior in the sample code? Could you provide more information as to the issues that you are encountering?

Thanks,
Michael

Send messages to the scene
 
 
Q