# React Native SDK

## Các hàm trong React Native SDK

### `initPushdy`

Kích hoạt React Native SDK chạy và định danh người dùng cho Device. Mỗi Device sẽ có một Device ID duy nhất.

{% tabs %}
{% tab title="Javascript" %}

```javascript
await Pushdy.initPushdy({ deviceId: 'YOUR_DEVICE_ID' });
```

{% endtab %}
{% endtabs %}

### `getDeviceId`

Với hàm này bạn có thể&#x20;truy xuất **Device ID** của bạn.

{% tabs %}
{% tab title="Javascript" %}

```javascript
var myDeviceId = await getDeviceId();
```

{% endtab %}
{% endtabs %}

### `isNotificationEnabled`

Kiểm tra xem người dùng

&#x20;có bật Push Notification cho App của bạn hay không.

{% tabs %}
{% tab title="Javascript" %}

```javascript
 var isEnabled = await isNotificationEnabled();
```

{% endtab %}
{% endtabs %}

### `getDeviceToken`

Lấy chuỗi Push Token. Token này chỉ có khi người dùng bật Push Notification cho App.

{% tabs %}
{% tab title="Javascript" %}

```java
var deviceToken = await getDeviceToken();
```

{% endtab %}
{% endtabs %}

### `getPendingNotification`

Lấy Notification mới nhất từ danh sách các Notification chưa được xử lý. Giá trị trả về là đối tượng Notification.

{% tabs %}
{% tab title="Javascript" %}

```javascript
var notification = await getPendingNotification();
```

{% endtab %}
{% endtabs %}

### `getPendingNotifications`

Lấy danh sách toàn bộ Notification từ danh sách hàng đợi chưa được xử lý. Giá trị trả về là mảng các đối tượng Notification.

{% tabs %}
{% tab title="Javascript" %}

```javascript
var notifications = await getPendingNotifications();
```

{% endtab %}
{% endtabs %}

### `setAttribute`

Tracking dữ liệu để gửi lên Pushdy nhằm phục vụ phân đoạn người dùng khi gửi Push. Với hàm này, dữ liệu mới sẽ đè lên dữ liệu cũ. Bạn có thể xem thêm chi tiết cách định nghĩa thuộc tính tại phần [Tạo thuộc tính](https://guide.pushdy.com/i/huong-dan-su-dung/tao-thuoc-tinh).

{% tabs %}
{% tab title="Javascript" %}

```javascript
await setAttribute("your_attribute_name", "your_value");
```

{% endtab %}
{% endtabs %}

### `pushAttribute`

Tương tự hàm `setAttribute` nhưng chỉ hoạt động với thuộc tính được định nghĩa là kiểu mảng (Array). Mảng giá trị mới sẽ được gắn thêm vào sau giá trị cũ.

{% tabs %}
{% tab title="Javascript" %}

```javascript
await pushAttribute("your_attribute_name", ["your_value"])
```

{% endtab %}
{% endtabs %}

### `getPlayerID`

Lấy Player ID của người dùng, ID này do Pushdy sinh ra cho mỗi người dùng đầu cuối. Chú ý, mỗi 1 thiết bị sẽ có 1 Player ID duy nhất, trường hợp gỡ ứng dụng và cài lại thì Player ID vẫn giữ nguyên.&#x20;

{% tabs %}
{% tab title="Javascript" %}

```javascript
var playerID = await getPlayerID();
```

{% endtab %}
{% endtabs %}

### `setReadyForHandlingNotification`

Nếu thiết lập giá trị là **false** có nghĩa là Push Notification sẽ không được xử lý ngay tại thời điểm nhận được mà sẽ được đưa vào hàng đợi. Bạn chỉ có thể lấy và xử lý Push Notification này bằng cách sử dụng hàm `getPendingNotification`. Mặc định giá trị được thiết lập sẽ là **true,** có nghĩa là Push Notification sẽ được xử lý ngay khi Device của bạn nhận được.

{% tabs %}
{% tab title="Javascript" %}

```javascript
var manualHandlingNotification = true
await setReadyForHandlingNotification(manualHandlingNotification);
```

{% endtab %}
{% endtabs %}

## Các Event trong React Native SDK

Để lắng nghe được các Event, bạn cần đăng ký như dưới đây:

{% tabs %}
{% tab title="Javascript" %}

```javascript
Pushdy.startSubscribers({
  onNotificationOpened: ({notification, fromState}) => {},
  onNotificationReceived: ({notification, fromState}) => {},
  onTokenUpdated: ({ deviceToken }) => {},
});
```

{% endtab %}
{% endtabs %}

### `onNotificationReceived`

Được gọi khi app của bạn nhận được Push Notification. Chú ý, sự kiện này chỉ xảy ra khi App của bạn đang được mở.

{% tabs %}
{% tab title="Javascript" %}

```javascript
onNotificationReceived({notification, fromState}) {
  console.log('Received notification: ', {notification, fromState});
}
```

{% endtab %}
{% endtabs %}

### `onNotificationOpened`

Được gọi khi người dùng nhấp vào Push Notification.

{% tabs %}
{% tab title="Javascript" %}

```javascript
onNotificationOpened({notification, fromState}) {
  console.log('Opened notification: ', {notification, fromState});
}
```

{% endtab %}
{% endtabs %}

### `onTokenUpdated`

Được gọi khi Device Token bị thay đổi.

{% tabs %}
{% tab title="Javascript" %}

```java
onTokenUpdated({ deviceToken }) {
  console.log('Received new Device Token: ', deviceToken)
}
```

{% endtab %}
{% endtabs %}

## Một số hàm dùng để tuỳ biến hiển thị Push Notification

### `setPushBannerAutoDismiss`

Bật/tắt tự động ẩn cửa sổ cho Push Notification khi bạn đang mở App. Nếu bạn tắt điều khiển tự động, người dùng sẽ phải chủ động nhấp vào "x" button trên cửa sổ Push để tắt thông báo.

{% tabs %}
{% tab title="Javascript" %}

```javascript
await setPushBannerAutoDismiss(true);
```

{% endtab %}
{% endtabs %}

### `setPushBannerDismissDuration`

Thiết lập thời gian hiển thị cửa sổ Push Notification khi bạn đang mở App. Mặc định, thời gian hiển thị là 5 giây. Hàm này chỉ hoạt động khi bạn bật chức năng tự động ẩn cửa sổ Push.

{% tabs %}
{% tab title="Javascript" %}

```javascript
await setPushBannerDismissDuration(5); // 5 seconds
```

{% endtab %}
{% endtabs %}

### `setCustomMediaKey`

Tuỳ chỉnh Json key cho media url để hiển thị ảnh trên cửa sổ Push Notification khi đang bật App. Mặc định, media\_url sẽ là key được sử dụng.

{% tabs %}
{% tab title="Javascript" %}

```javascript
await setCustomMediaKey("your_custom_media_key");
```

{% endtab %}
{% endtabs %}


---

# Agent Instructions: Querying This Documentation

If you need additional information that is not directly available in this page, you can query the documentation dynamically by asking a question.

Perform an HTTP GET request on the current page URL with the `ask` query parameter:

```
GET https://guide.pushdy.com/i/tham-chieu-sdk-api/react-native-sdk.md?ask=<question>
```

The question should be specific, self-contained, and written in natural language.
The response will contain a direct answer to the question and relevant excerpts and sources from the documentation.

Use this mechanism when the answer is not explicitly present in the current page, you need clarification or additional context, or you want to retrieve related documentation sections.
