> ## 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.

# Web SDK 映射

> 比较旧版 Player 模型和新 User 模型之间的 OneSignal Web SDK 方法。通过基于 TypeScript 的代码示例和更新的方法参考，学习如何迁移您的实现。

<Warning>
  OneSignal 已从以设备为中心的模型（播放器 ID）更新为以用户为中心的模型（OneSignal ID）。有关迁移指导，请参阅 [用户模型迁移指南](./user-model-migration-guide)。

  有关旧版以设备为中心的实现文档，请参见 [版本 9](/v9.0/docs/web-push-sdk)。
</Warning>

## 概述

本文档将 OneSignal 的旧版 **Player 模型 Web SDK** 的方法、属性和事件映射到较新的 **User 模型** SDK。每个部分都包含匹配的 TypeScript 代码示例，清楚地演示如何更新您的集成。

所有示例都为演示目的而简化。有关完整和最新的实现，请参阅每个方法或事件下提供的文档链接。

## OneSignal 服务工作线程

更新您的 `OneSignalSDKWorker.js` 文件中的导入：

**Player 模型:**

```javascript theme={null}
importScripts('https://cdn.onesignal.com/sdks/OneSignalSDKWorker.js');
```

**User 模型:**

```javascript theme={null}
importScripts("https://cdn.onesignal.com/sdks/web/v16/OneSignalSDK.sw.js");
```

保持文件路径相同。只需更新 `importScripts` URL。

查看 [OneSignal Service Worker](./onesignal-service-worker) 了解更多信息。

## 初始化

### `init()`

**Player 模型:** [文档](/v9.0/docs/web-push-sdk)

```html theme={null}
<script src="https://cdn.onesignal.com/sdks/OneSignalSDK.js" async></script>
<script>
window.OneSignal = window.OneSignal || [];
OneSignal.push(function() {
  OneSignal.init({
    appId: "YOUR_APP_ID"
  });
});
</script>
```

**User 模型:** [文档](./mobile-sdk-reference#initialize)

```html theme={null}
<script src="https://cdn.onesignal.com/sdks/web/v16/OneSignalSDK.page.js" defer></script>
<script>
window.OneSignalDeferred = window.OneSignalDeferred || [];
OneSignalDeferred.push(async function(OneSignal) {
  await OneSignal.init({
    appId: "YOUR_APP_ID",
  });
});
</script>
```

### `provideUserConsent()`

**Player 模型:** [文档](/v9.0/docs/web-push-sdk)

```typescript theme={null}
OneSignal.provideUserConsent(true)
```

**User 模型:** [文档](./mobile-sdk-reference#setconsentgiven)

```typescript theme={null}
OneSignal.setConsentGiven(true)
```

## 注册推送

### `showNativePrompt()`

**Player 模型:**

```typescript theme={null}
OneSignal.showNativePrompt()
```

**User 模型:** [文档](./mobile-sdk-reference#requestpermission-fallbacktosettings-push)

```typescript theme={null}
OneSignal.Notifications.requestPermission()
```

### `registerForPushNotifications()` — User 模型中已弃用

```typescript theme={null}
OneSignal.registerForPushNotifications()
```

### `#permissionPromptDisplay`

**Player 模型:**

```typescript theme={null}
OneSignal.on('permissionPromptDisplay', () => ...)
```

**User 模型:** [文档](./web-sdk-reference#addeventlistener-push-notification)

```typescript theme={null}
OneSignal.Notifications.addEventListener('permissionPromptDisplay', event => { ... })
```

### `showSlidedownPrompt()`

**Player 模型:**

```typescript theme={null}
OneSignal.showSlidedownPrompt()
```

**User 模型:** [文档](./web-sdk-reference#promptpush)

```typescript theme={null}
OneSignal.Slidedown.promptPush()
```

### `showHttpPrompt()` — User 模型中已弃用

```typescript theme={null}
OneSignal.showHttpPrompt()
```

### `showCategorySlidedown()`

**Player 模型:**

```typescript theme={null}
OneSignal.showCategorySlidedown()
```

**User 模型:** [文档](./web-sdk-reference#promptpushcategories)

```typescript theme={null}
OneSignal.Slidedown.promptPushCategories()
```

### `#getNotificationPermission`

**Player 模型:**

```typescript theme={null}
OneSignal.on('getNotificationPermission', (permission) => ...)
```

**User 模型:** [文档](./web-sdk-reference#addeventlistener-push-subscription)

```typescript theme={null}
OneSignal.User.PushSubscription.addEventListener('change', ({ optedIn }) => { ... })
```

### `isPushNotificationsSupported()`

**Player 模型:**

```typescript theme={null}
OneSignal.isPushNotificationsSupported()
```

**User 模型:** [文档](./web-sdk-reference#ispushsupported)

```typescript theme={null}
OneSignal.Notifications.isPushSupported()
```

### `isPushNotificationsEnabled()`

**Player 模型:**

```typescript theme={null}
await OneSignal.isPushNotificationsEnabled()
```

**User 模型:** [文档](./web-sdk-reference#optedin)

```typescript theme={null}
OneSignal.User.PushSubscription.optedIn
```

### `#subscriptionChange`

**Player 模型:**

```typescript theme={null}
OneSignal.on('subscriptionChange', (subscribed) => ...)
```

**User 模型:**

```typescript theme={null}
OneSignal.User.PushSubscription.addEventListener('change', ({ token }) => { ... })
```

## 分析

### `#notificationPermissionChange`

**Player 模型:**

```typescript theme={null}
OneSignal.on('notificationPermissionChange', ({ to }) => ...)
```

**User 模型:** [文档](./web-sdk-reference#permissionchange)

```typescript theme={null}
OneSignal.Notifications.addEventListener('permissionChange', permission => { ... })
```

### `#popoverShown`

**Player 模型:**

```typescript theme={null}
OneSignal.on('popoverShown', () => ...)
```

**User 模型:** [文档](./web-sdk-reference#addeventlistener-slidedown)

```typescript theme={null}
OneSignal.Slidedown.addEventListener('slidedownShown', presented => { ... })
```

### `#customPromptClick`

**Player 模型:**

```typescript theme={null}
OneSignal.on('customPromptClick', ({ result }) => ...)
```

**User 模型:** [文档](./web-sdk-reference#click)

```typescript theme={null}
OneSignal.Notifications.addEventListener('click', ({notification, result}) => { ... })
```

## 用户 ID

### `getUserId()`

**Player 模型:**

```typescript theme={null}
OneSignal.getUserId()
```

**User 模型:** [文档](./web-sdk-reference#id)

```typescript theme={null}
OneSignal.User.PushSubscription.id;
```

### `setExternalUserId()`

**Player 模型:** [文档](./users)

```typescript theme={null}
OneSignal.setExternalUserId("external id")
```

**User 模型:** [文档](./web-sdk-reference#login-external-id)

```typescript theme={null}
OneSignal.login("external id")
```

### `removeExternalUserId()`

**Player 模型:**

```typescript theme={null}
OneSignal.removeExternalUserId()
```

**User 模型:** [文档](./web-sdk-reference#logout)

```typescript theme={null}
OneSignal.logout()
```

### `getExternalUserId()`

**Player 模型:**

```typescript theme={null}
await OneSignal.getExternalUserId()
```

**User 模型:** [文档](./web-sdk-reference#externalid)

```typescript theme={null}
OneSignal.User.externalId
```

## 标签

### `sendTag()`

**Player 模型:**

```typescript theme={null}
OneSignal.sendTag("key", "value")
```

**User 模型:**

```typescript theme={null}
OneSignal.User.addTag("key", "value")
```

**User 模型** [文档](./web-sdk-reference#addtag-%2C-addtags)

### `sendTags()`

**Player 模型:**

```typescript theme={null}
OneSignal.sendTags({ key1: 'value1', key2: 'value2' })
```

**User 模型:**

```typescript theme={null}
OneSignal.User.addTags({ key1: 'value1', key2: 'value2' })
```

### `getTags()`

**Player 模型:**

```typescript theme={null}
await OneSignal.getTags()
```

**User 模型:**

```typescript theme={null}
OneSignal.User.getTags()
```

### `deleteTag()`

**Player 模型:**

```typescript theme={null}
OneSignal.deleteTag("key")
```

**User 模型:**

```typescript theme={null}
OneSignal.User.removeTag("key")
```

### `deleteTags()`

**Player 模型:**

```typescript theme={null}
OneSignal.deleteTags(["key1", "key2"])
```

**User 模型:**

```typescript theme={null}
OneSignal.User.removeTags(["key1", "key2"])
```

## 推送通知

### `sendSelfNotification()` — User 模型中已弃用

```typescript theme={null}
OneSignal.sendSelfNotification('title', 'message', 'url')
```

### `setSubscription()`

**Player 模型:**

```typescript theme={null}
OneSignal.setSubscription(false)
```

**User 模型:**

```typescript theme={null}
OneSignal.Notifications.permission = false
```

## 接收通知

### `#notificationDisplay`

**Player 模型:**

```typescript theme={null}
OneSignal.on('notificationDisplay', (event) => { ... })
```

**User 模型:**

```typescript theme={null}
OneSignal.Notifications.addEventListener('foregroundWillDisplay', ({ notification }) => { ... })
```

### `#notificationDismiss`

**Player 模型:**

```typescript theme={null}
OneSignal.on('notificationDismiss', (event) => { ... })
```

**User 模型:**

```typescript theme={null}
OneSignal.Notifications.addEventListener('dismiss', ({ notification }) => { ... })
```

### `#addListenerForNotificationOpened`

**Player 模型:**

```typescript theme={null}
OneSignal.on('addListenerForNotificationOpened', (event) => { ... })
```

## 邮件

### `setEmail()`

**User Model** [文档](./web-sdk-reference#addemail-%2C-removeemail)

**Player 模型:**

```typescript theme={null}
OneSignal.setEmail('email@example.com')
```

**User 模型:**

```typescript theme={null}
OneSignal.User.addEmail('email@example.com')
```

### `logoutEmail()`

**Player 模型:**

```typescript theme={null}
OneSignal.logoutEmail()
```

**User 模型:**

```typescript theme={null}
OneSignal.User.removeEmail('email@example.com')
```

### `getEmailId()` — User 模型中已弃用

## 短信

### `setSMSNumber()`

**Player 模型:**

```typescript theme={null}
OneSignal.setSMSNumber('+11234567890')
```

**User 模型:**

```typescript theme={null}
OneSignal.User.addSms('+11234567890')
```

### `logoutSMSNumber()`

**Player 模型:**

```typescript theme={null}
OneSignal.logoutSMS()
```

**User 模型:**

```typescript theme={null}
OneSignal.User.removeSms('+11234567890')
```
