# 在线状态事件通知 (/zh-CN/realtime-media/rtm/build/state-and-attributes/presence-events/flutter) > For AI agents: see the complete documentation index at [llms.txt](/llms.txt). ## 添加事件监听 [#添加事件监听] 接收 Presence 事件通知,你需要实现事件监听程序,详见[事件监听](/zh-CN/realtime-media/rtm/build/messaging/add-event-listener)。除此之外,你需要在订阅或加入频道时将 `withPresence` 参数设置成 `true`。`PresenceEvent` 数据结构包含以下属性: | 参数 | 描述 | | ----------- | ---- | `type` | Presence 事件类型。详见 [`RtmPresenceEventType`](/zh-CN/api-reference/rtm/flutter/enumv#presencetype)。 | | `channelType` | 频道类型,可能是 Message Channel 或 Stream Channel。 | | `channelName` | 事件发生的频道名称。 | | `publisher` | 触发此事件的用户 ID。 | | `stateItems` | 用户临时状态信息列表,`remoteStateChanged` 事件发生时有效。 | | `interval` | Interval 模式下,当前频道在上一个周期内用户加入、离开、超时、状态变更等事件通知的聚合增量信息。 | | `snapshot` | 快照详情。用户首次加入或订阅频道触发了 `snapshot` 事件时有效。 | | `timestamp` | 事件触发时间戳。 | ## 事件通知类型 [#事件通知类型] Presence 事件通知有七种类型,它们分别是: | 枚举值 | 描述 | | ------- | ----------------- | | `snapshot` | `1`: 用户第一次订阅或加入频道时触发,本地用户会收到此类事件通知。 | | `interval` | `2`: 当频道内人数达到设定值后,频道内的事件通知将由实时通知转为定时通知。 | | `remoteJoinChannel` | `3`: 当用户订阅或加入频道时触发,频道中的其他人会收到此类通知。 | | `remoteLeaveChannel` | `4`: 当用户取消订阅或离开频道时触发,频道中的其他人会收到此类通知。 | | `remoteTimeout` | `5`: 当用户掉线超时时触发,频道中的其他人会收到此类通知。 | | `remoteStateChanged` | `6`: 当用户临时状态数据发生变更时触发,频道中的其他人会收到此类通知。 | | `errorOutOfService` | `7`: 用户加入频道时未启用 Presence,本地用户会收到此类事件通知。 | ## 事件通知模式 [#事件通知模式] ### 实时通知模式 [#实时通知模式] 如果频道中的在线人数小于实时通知最大人数参数设定的值(默认值为 50),则 Presence 事件通知处于实时通知模式。在此模式下,`remoteJoinChannel`、`remoteLeaveChannel`、`remoteTimeout`、`remoteStateChanged` 事件在触发时会立即发送到客户端。 ```dart tab="Join" { type: RtmPresenceEventType.remoteJoinChannel, channelType: RtmChannelType.message, channelName: 'chat_room', publisher: 'Tony', stateItems: [], interval: [], snapshot: [], timestamp: 1710487149497, } ``` ```dart tab="Leave" { type: RtmPresenceEventType.remoteLeaveChannel, channelType: RtmChannelType.message, channelName: 'chat_room', publisher: 'Tony', stateItems: [], interval: [], snapshot: [], timestamp: 1710487149497, } ``` ```dart tab="Timeout" { type: RtmPresenceEventType.remoteTimeout, channelType: RtmChannelType.message, channelName: 'chat_room', publisher: 'Tony', stateItems: [], interval: [], snapshot: [], timestamp: 1710487149497, } ``` ```dart tab="Snapshot" { type: RtmPresenceEventType.snapshot, channelType: RtmChannelType.message, channelName: 'chat_room', publisher: 'Tony', stateItems: [], interval: [], snapshot: {[ { userId: "user_a", states: []}, { userId: "user_b", states: [{ key: 'mic', value : 'on'}]}, { userId: "yourSelf", states: [{}]}, ]}, timestamp: 1710487149497 } ``` ```dart tab="State Change" { type: RtmPresenceEventType.remoteStateChanged, channelType: RtmChannelType.message, channelName: 'chat_room', publisher: 'Tony', stateItems: [{ key: 'mic', value : 'on'},{ key: 'mode', value : 'happy'}], interval: [], snapshot: [], timestamp: 1710487149497 } ``` ### 定时通知模式 [#定时通知模式] 当频道中的在线人数超过实时通知最大人数的值时,频道会进入定时通知模式。在这种模式下,`remoteJoinChannel`、`remoteLeaveChannel`、`remoteTimeout`、`remoteStateChanged` 事件会被 `interval` 事件代替,并以一定的时间间隔发送给频道中所有用户。 ```dart { type : RtmPresenceEventType.interval, channelType : RtmChannelType.message,, channelName : 'chat_room', publisher : 'Tony', interval : { joinUserList : {users:['Tony','Lily']}, leaveUserList :{users:['Jason']}, timeoutUserList : {users:['Wang']}, userStateList : [ { userId : 'Harvard', states : [{ key: 'mic', value : 'on'},{ key: 'mode', value : 'happy'}], }, { userId : 'Harvard', states : [{ key: 'mic', value : 'off'},{ key: 'mode', value : 'sad'}], } ] } } ```