> ## Documentation Index
> Fetch the complete documentation index at: https://documentation.onesignal.com/llms.txt
> Use this file to discover all available pages before exploring further.

# 社交活动

> 了解如何使用 OneSignal 为点赞、关注和邀请等社交操作以及用户间直接消息发送推送通知。

使用 OneSignal 向用户通知社交活动（点赞、关注、邀请）和用户消息。这些提醒可以在用户当前不活跃在您的应用中时推动重新参与。

<Warning>
  OneSignal 和推送通知并非专为实时通信而设计。它们最适合用作两个用户都不在您的应用中活跃时的备用方案。

  对于实时通信，请考虑专用的实时聊天解决方案，例如：

  * [XMPP](https://xmpp.org/)
  * [Stream](https://getstream.io/chat/)
  * [Firebase Realtime Database](https://firebase.google.com/products/database)
</Warning>

***

## 社交活动通知

当用户参与社交操作时触发推送通知。

### 常见社交操作

| 操作 | 示例消息                 |
| -- | -------------------- |
| 点赞 | "Anna 点赞了你的帖子。"      |
| 提及 | "Leo 在评论中提及了你。"      |
| 标签 | "Sara 在照片中标记了你。"     |
| 邀请 | "Ben 邀请你参加活动。"       |
| 评论 | "Maya 回复："看起来很棒！""   |
| 关注 | "James 开始关注你。"       |
| 分享 | "Alex 与你分享了'夏威夷相册'。" |

### 要求

* 跟踪用户活动和关系的后端或 CRM。
  * [集成](./integrations)
  * [数据库、DMP 和 CRM 集成](./database-dmp-crm-integration)
* 在 OneSignal 中设置外部 ID 以识别用户。
* 使用我们的[创建通知 API](/reference/create-message)。

### 设置

#### 1. 使用外部 ID 映射用户

使用[别名和外部 ID](./users) 在 OneSignal 中唯一标识用户。

#### 2. 检测社交操作

在您的后端中，跟踪谁执行了操作以及它影响了谁。

```json json theme={null}
{
  "action": "like",
  "actor_id": "user_b",
  "recipient_id": "user_a",
  "post_id": "xyz789"
}
```

#### 3. 发送推送通知

使用 OneSignal API 通知接收者：

```json theme={null}
{
  "app_id": "YOUR_APP_ID",
  "contents": { "en": "user_b liked your photo." },
  "include_aliases": { "external_id": ["user_a"] }
}
```

### 附加功能

* 使用[消息个性化](./message-personalization)插入用户名、帖子标题、图片等自定义数据！
* 使用[活动动态](./create-an-activity-feed)显示社交提醒的历史记录。

***

## 直接（用户对用户）消息

让用户知道他们有新消息，可选择包含消息内容或聊天的深度链接。

<Warning>当两个用户都没有在应用中活跃聊天时，推送通知最适合用作备用方案。</Warning>
当用户 A 向用户 B 发送消息且 B 当前不在线或不在聊天屏幕上时，发送推送。

### 设置

#### 1. 使用外部 ID 映射用户

使用[别名和外部 ID](./users) 在 OneSignal 中唯一标识用户。

#### 2. 在消息发送时触发通知

从您的后端，使用[创建通知 API](/reference/push-notification) 和 `include_aliases` 字段向接收者发送推送：

```json theme={null}
{
  "include_aliases": {
    "external_id": ["USER_B_ID"]
  },
  "contents": { "en": "New message from Anna: 'Hey, you around?'" },
  "headings": { "en": "New Message" },
  "data": {
    "sender_id": "user_a",
    "conversation_id": "chat_1234",
    "click_action": "open_chat"
  }
}

```

### 最佳实践

* 避免垃圾信息：不要为每一条消息都发送通知。使用批处理或仅在 X 分钟不活跃后提醒。
* 尊重用户设置：让用户可以根据需要静音或禁用消息通知。
* 在您的应用中使用路由逻辑打开对话。更多信息请参见[深度链接](./deep-linking)。

***

## 相关文章

* [创建活动动态](./create-an-activity-feed)
* [深度链接](./deep-linking)
* [消息个性化](./message-personalization)
* [事务性消息](./transactional-messages)

***
