# Web Push SDK

## Các hàm trong Web Push SDK

### `initApp`

Tải Web Push SDK để có thể sử dụng các hàm. Bạn cần gắn đoạn mã tải SDK trên tất cả các trang web mà bạn sử dụng hàm của Web Push SDK. Mỗi file JS SDK được sinh ra trên Pushdy Dashboard đều đã bao gồm định danh và các cấu hình cho trang web của bạn.\
Vì vậy, sau khi tải Web Push SDK, bạn chỉ cần gọi hàm `initApp()` để có thể bắt đầu sử dụng các hàm của SDK.

{% code title="HTML" %}

```markup
<script src="https://sdk.pushdi.com/js/generated/xxxxxxxxx.js" async=""></script>
<script>
    var PushdyIns = window.PushdyIns || [];
    PushdyIns.push(function() {
        PushdyIns.initApp();
    });
</script>
```

{% endcode %}

### `show`

Sử dụng để hiển thị cửa sổ xin quyền Push Notification. Để tỷ lệ user đồng ý nhận Push Notification cao, bạn nên cung cấp cho user thông tin ngắn gọn về lợi ích của việc bật Push Notification. Thông tin này sẽ được hiển thị trên cửa sổ xin quyền Web Push.

Mặc định, Web Push SDK sẽ tự động hiển thị cửa sổ này ngay sau khi user truy cập trang Web mà không phải gọi hàm `show()` . Tuy nhiên bạn có thể tắt tính năng tự động này và sử dụng hàm `show()` để chỉ hiển thị cửa sổ theo Logic của riêng bạn.

{% code title="JavaScript" %}

```javascript
PushdyIns.push(function() {
  PushdyIns.show();
});
```

{% endcode %}

### `isPushNotificationSupported`

Kiểm tra xem trình duyệt có hỗ trợ Web Push hay không.

{% code title="JavaScript" %}

```javascript
PushdyIns.push(function() {
  var isSupported = PushdyIns.isPushNotificationSupported();
});
```

{% endcode %}

### `isPushNotificationEnabled`

Kiểm tra xem user có đăng ký nhận Web Push không.

{% code title="JavaScript" %}

```javascript
PushdyIns.push(function() {
  var isEnabled = PushdyIns.isPushNotificationEnabled();
});
```

{% endcode %}

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

{% code title="JavaScript" %}

```javascript
PushdyIns.push(function() {
    PushdyIns.setAttribute('sport_visits', 123);
});
```

{% endcode %}

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

{% code title="JavaScript" %}

```javascript
PushdyIns.push(function() {
    PushdyIns.pushAttribute('sport_item_favourited', ['shoe', 'clother']);
});

// Nếu bạn chỉ muốn thuộc tính lưu trữ 10 giá trị thì bạn có thể thêm 
// tham số như sau:
PushdyIns.push(function() {
    PushdyIns.pushAttribute('sport_item_favourited', ['shoe', 'clother'], 10);
});
```

{% endcode %}

### `pushAttributeByDay`

Tương tự hàm `pushAttribute` nhưng có thể thiết lập số ngày gần nhất mà dữ liệu được lưu. Vd: bạn muốn lưu những sản phẩm mà user xem trong 3 ngày gần nhất.

{% code title="JavaScript" %}

```javascript
PushdyIns.push(function() {
    PushdyIns.pushAttributeByDay("sport_item_viewed", ['shoe', 'clother'], 3);
});
```

{% endcode %}

### `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ú ý, trên 1 thiết bị thì người dùng truy cập các trình duyệt khác nhau sẽ có các Player ID khác nhau.

{% code title="JavaScript" %}

```javascript
PushdyIns.push(function() {
    PushdyIns.getPlayerId().then(function(playerId) {
        console.log("Pushdy Player ID:", playerId);   
    });
});
```

{% endcode %}

## Các sự kiện trong Web Push SDK

### `onAllowSubscription`

Sự kiện xảy ra khi user nhấp đồng ý nhận thông báo đẩy từ cửa sổ tuỳ chỉnh xin quyền Push Notification.

{% code title="JavaScript" %}

```javascript
document.getElementsByTagName("BODY")[0]
    .addEventListener('onAllowSubscription', function (event) {
    // ...
}, false);
```

{% endcode %}

### `onDenySubscription`

Sự kiện xảy ra khi user nhấp từ chối nhận thông báo đẩy từ cửa sổ tuỳ chỉnh xin quyền Push Notification.

{% code title="JavaScript" %}

```javascript
document.getElementsByTagName("BODY")[0]
    .addEventListener('onDenySubscription', function (event) {
    // ...
}, false);
```

{% endcode %}

### `onShowSubscriptionBox`

Sự kiện xảy ra mỗi lần cửa sổ tuỳ chỉnh xin quyền Push Notification được hiển thị trên trang web của bạn.

{% code title="JavaScript" %}

```javascript
document.getElementsByTagName("BODY")[0]
    .addEventListener('onShowSubscriptionBox', function (event) {
    // ...
}, false);
```

{% endcode %}

### `onSubscribe`

Sự kiện xảy ra khi người dùng bật nhận thông báo Push Notifcation từ cửa sổ hệ thống của trình duyệt.

{% code title="JavaScript" %}

```javascript
document.getElementsByTagName("BODY")[0]
    .addEventListener('onSubscribe', function (event) {
    // ...
}, false);
```

{% endcode %}


---

# 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/web-push-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.
