# 管理用户同意

Pushwoosh 支持遵守隐私法规（例如 GDPR、CCPA），允许开发者控制 SDK 何时开始与 Pushwoosh 服务器通信。这有助于确保在用户明确同意之前不会收集任何数据。

### 概述

默认情况下，Pushwoosh SDK 在初始化后会立即开始通信并收集设备数据。但是，您可以更改此行为，以便在用户同意之前不进行任何通信。

通过此设置：

*   如果用户同意，SDK 将初始化并开始收集数据。

*   如果用户不同意，SDK 将保持非活动状态，不收集任何数据。

*   如果用户稍后撤回同意，SDK 将停止所有活动并与服务器断开连接。

Pushwoosh 提供了在 iOS、Android 和 Unity 中管理此流程的机制。

### iOS

#### 在启动时禁用 SDK 服务器通信

默认情况下，与 SDK 的通信是启用的。要在用户明确授予同意之前禁用与 Pushwoosh 服务器的所有通信，请将以下键添加到您的 **`Info.plist`** 中：

```xml
<key>Pushwoosh_ALLOW_SERVER_COMMUNICATION</key>
<false/>

```

#### 检查通信状态

使用以下方法来确定当前是否允许通信：

<Tabs>
<TabItem label="SwiftUI">

```swift
import SwiftUI
import PushwooshFramework

var serverCommunicationAllowed = PWCoreServerCommunicationManager.sharedInstance.isServerCommunicationAllowed
print("isServerCommunicationAllowed: ", serverCommunicationAllowed)
```
</TabItem>
<TabItem label="Swift">

```swift
import PushwooshFramework

var serverCommunicationAllowed = PWCoreServerCommunicationManager.sharedInstance.isServerCommunicationAllowed
print("isServerCommunicationAllowed: ", serverCommunicationAllowed)
```
</TabItem>
<TabItem label="Objective-C">

```objective-c
#import <PushwooshFramework/PushwooshFramework.h>

BOOL serverCommunicationAllowed = [PWCoreServerCommunicationManager sharedInstance].isServerCommunicationAllowed;
NSLog(@"isServerCommunicationAllowed: %d", serverCommunicationAllowed);
```
</TabItem>
</Tabs>

#### 同意后启用 SDK 通信

用户授予同意后，按如下方式启用通信：

<Tabs>
<TabItem label="SwiftUI">

```swift
import SwiftUI
import PushwooshFramework

Pushwoosh.sharedInstance().startServerCommunication()
```
</TabItem>
<TabItem label="Swift">

```swift
import PushwooshFramework

Pushwoosh.sharedInstance().startServerCommunication()
```
</TabItem>
<TabItem label="Objective-C">

```objective-c
#import <PushwooshFramework/PushwooshFramework.h>

[[Pushwoosh sharedInstance] startServerCommunication];
```
</TabItem>
</Tabs>

#### 注册推送通知

通信启用后，明确注册推送通知：

<Tabs>
<TabItem label="SwiftUI">

```swift
import SwiftUI
import PushwooshFramework

Pushwoosh.sharedInstance().registerForPushNotifications()
```
</TabItem>
<TabItem label="Swift">

```swift
import PushwooshFramework

Pushwoosh.sharedInstance().registerForPushNotifications()
```
</TabItem>
<TabItem label="Objective-C">

```objective-c
#import <PushwooshFramework/PushwooshFramework.h>

[[Pushwoosh sharedInstance] registerForPushNotifications];
```
</TabItem>
</Tabs>

#### 禁用通信

要停止与 Pushwoosh 服务器的通信（例如，如果用户撤销同意）：

<Tabs>
<TabItem label="SwiftUI">

```swift
import SwiftUI
import PushwooshFramework

Pushwoosh.sharedInstance().stopServerCommunication()
```
</TabItem>
<TabItem label="Swift">

```swift
import PushwooshFramework

Pushwoosh.sharedInstance().stopServerCommunication()
```
</TabItem>
<TabItem label="Objective-C">

```objective-c
#import <PushwooshFramework/PushwooshFramework.h>

[[Pushwoosh sharedInstance] stopServerCommunication];
```
</TabItem>
</Tabs>

### Android

#### 在启动时禁用 SDK 服务器通信

默认情况下，通信是启用的。为防止在获得用户同意之前将任何数据发送到 Pushwoosh 服务器，请将以下内容添加到您的 `AndroidManifest.xml` 中：

```xml
<meta-data
  android:name="com.pushwoosh.allow_server_communication"
  android:value="false" />
```

#### 检查通信状态

使用以下方法来确定当前是否允许服务器通信：

<Tabs>
<TabItem label="Java">

```java
import com.pushwoosh.Pushwoosh;

boolean isCommunicationEnabled = Pushwoosh.getInstance().isServerCommunicationAllowed();
Log.d("Pushwoosh", "Communication enabled = " + isCommunicationEnabled);
```
</TabItem>
<TabItem label="Kotlin">

```kotlin
import com.pushwoosh.Pushwoosh

val isCommunicationEnabled = Pushwoosh.getInstance().isServerCommunicationAllowed()
Log.d("Pushwoosh", "Communication enabled = $isCommunicationEnabled")
```
</TabItem>
</Tabs>

#### 同意后启用 SDK 通信

用户授予同意后，按如下方式启用通信：

<Tabs>
<TabItem label="Java">

```java
import com.pushwoosh.Pushwoosh;

Pushwoosh.getInstance().startServerCommunication();
```
</TabItem>
<TabItem label="Kotlin">

```kotlin
import com.pushwoosh.Pushwoosh

Pushwoosh.getInstance().startServerCommunication()
```
</TabItem>
</Tabs>

#### 注册推送通知

通信启用后，明确注册推送通知：

<Tabs>
<TabItem label="Java">

```java
import com.pushwoosh.Pushwoosh;

Pushwoosh.getInstance().registerForPushNotifications();
```
</TabItem>
<TabItem label="Kotlin">

```kotlin
import com.pushwoosh.Pushwoosh

Pushwoosh.getInstance().registerForPushNotifications()
```
</TabItem>
</Tabs>

#### 禁用通信

要停止与 Pushwoosh 服务器的通信（例如，如果用户撤销同意）：

<Tabs>
<TabItem label="Java">

```java
import com.pushwoosh.Pushwoosh;

Pushwoosh.getInstance().stopServerCommunication();
```
</TabItem>
<TabItem label="Kotlin">

```kotlin
import com.pushwoosh.Pushwoosh

Pushwoosh.getInstance().stopServerCommunication()
```
</TabItem>
</Tabs>

### Unity

#### 在启动时禁用 SDK 服务器通信

默认情况下，与 SDK 的通信是启用的。要在用户明确授予同意之前禁用与 Pushwoosh 服务器的所有通信，请应用特定于平台的设置：

**Android**

将以下内容添加到您的 Unity 项目的 `AndroidManifest.xml` 中：

```xml
 <meta-data android:name="com.pushwoosh.allow_server_communication" android:value="false" />
```

**iOS**

修改 `Info.plist`：

```xml
<key>Pushwoosh_ALLOW_SERVER_COMMUNICATION</key>
<false/>
```

**注意：** 您必须在调用 `RegisterForPushNotifications` 之前启用通信。

#### 检查通信状态

使用以下方法检查当前是否允许服务器通信：

```c#
bool enabled = Pushwoosh.Instance.IsCommunicationEnabled();
```

#### 同意后启用 SDK 通信

要在同意后启用通信：

```c#
Pushwoosh.Instance.SetCommunicationEnabled(true);
```

#### 注册推送通知

通信启用后，您可以为设备注册推送通知：

```c#
Pushwoosh.Instance.RegisterForPushNotifications();
```

#### 禁用通信

要停止与 Pushwoosh 服务器的通信（例如，如果用户撤销同意）：

```c#
Pushwoosh.Instance.SetCommunicationEnabled(false);
```

### Web Push

#### 在启动时禁用自动订阅

默认情况下，Pushwoosh SDK 在初始化后会立即显示原生订阅提示。为防止 SDK 在初始化时自动显示订阅提示，请在 `init` 调用中将 `communicationEnabled` 参数设置为 `false`。

```html
<script type="text/javascript" src="//cdn.pushwoosh.com/webpush/v3/pushwoosh-web-notifications.js" async></script>
<script type="text/javascript">
  var Pushwoosh = Pushwoosh || [];
  Pushwoosh.push(['init', {
    // other initialization parameters...
    communicationEnabled: false, // Disable communication to prevent automatic subscription prompts
  }]);
</script>
```

#### 同意后启用订阅

禁用自动订阅后，您可以随时提示用户订阅。当用户同意接收推送通知时（例如，通过点击您自定义 UI 上的“订阅”按钮），您可以通过调用 `setCommunicationEnabled` 方法来启用通信。调用 `Pushwoosh.setCommunicationEnabled(true)` 会启用与 Pushwoosh 服务的通信。启用后，SDK 将继续显示原生浏览器权限提示。

```javascript
Pushwoosh.setCommunicationEnabled(true)
  .then(() => {
    console.log('User is subscribed to push notifications.');
  })
  .catch((error) => {
    console.error('Error subscribing user:', error);
  });
```

#### 禁用通信

要停止与 Pushwoosh 服务的通信（例如，如果用户撤销同意），请使用 `false` 调用 `setCommunicationEnabled`。

```javascript
Pushwoosh.setCommunicationEnabled(false);
```