# Android Native SDK

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

### `initWith`

Khởi tạo Pushdy SDK với Client Key. Bạn có thể sao chép Client Key tại Dashboard của Pushdy sau khi bạn tạo ứng dụng. Mỗi ứng dụng sẽ có 1 Client Key duy nhất.\
Hàm `initWith` được sử dụng trong hàm `onCreate` trong class `Application` của bạn.

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

```java
String clientKey = "YOUR_CLIENT_KEY";
Pushdy.initWith(this, clientKey, this);
```

{% endtab %}

{% tab title="Kotlin" %}

```kotlin
val clientKey = "YOUR_CLIENT_KEY"
Pushdy.initWith(this, clientKey, this)
```

{% endtab %}
{% endtabs %}

### `setDeviceID`

Bạn có thể tuỳ chỉnh **Device ID** theo logic của bạn. Nếu bạn không sử dụng hàm này, **Device ID** sẽ được tự động sinh ra để đảm bảo định danh user là duy nhất.\
Hàm này phải được gọi ngay trước hàm `initWith` để đảm bảo *Custom Device ID* được lưu lại trước khi Pusdy SDK gửi yêu cầu tới máy chủ.

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

```java
String customDeviceID = "YOUR CUSTOM ID";
Pushdy.setDeviceID(customDeviceID);
```

{% endtab %}

{% tab title="Kotlin" %}

```kotlin
val customDeviceID = "YOUR CUSTOM ID"
Pushdy.setDeviceID(customDeviceID)
```

{% 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="Java" %}

```java
String *yourDeviceID = [Pushdy getDeviceID]
```

{% endtab %}

{% tab title="Kotlin" %}

```kotlin
var yourDeviceID = Pushdy.getDeviceID()
```

{% endtab %}
{% endtabs %}

### `isNotificationEnabled`

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

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

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

```java
 boolean isEnabled = Pushdy.isNotificationEnabled();
```

{% endtab %}

{% tab title="Kotlin" %}

```kotlin
var isEnabled = Pushdy.isNotificationEnabled()
```

{% endtab %}
{% endtabs %}

### `getDeviceToken`

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

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

```java
String deviceToken = Pushdy.getDeviceToken();
```

{% endtab %}

{% tab title="Kotlin" %}

```kotlin
var deviceToken = Pushdy.getDeviceToken()
```

{% endtab %}
{% endtabs %}

### `getPendingNotification`

Lấy Notification mới nhất từ danh sách các Notification chưa được xử lý.

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

```java
Map<String, Object> latestNotification = Pushdy.getPendingNotification();
```

{% endtab %}

{% tab title="Kotlin" %}

```kotlin
Map<String, Any> latestNotification = Pushdy.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ý.

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

```java
List<Map<String, Object>> pendingNotifications = Pushdy.getPendingNotifications();
```

{% endtab %}

{% tab title="Kotlin" %}

```kotlin
List<Map<String, Any>> pendingNotifications = Pushdy.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="Java" %}

```java
Pushdy.setAttribute("your_attribute_name", "your_value");
```

{% endtab %}

{% tab title="Kotlin" %}

```kotlin
Pushdy.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="Java" %}

```java
String[] your_values_array = {"new_value_1", "new_value_2"};
Pushdy.pushAttribute("your_attribute_name", your_values_array);
```

{% endtab %}

{% tab title="Kotlin" %}

```kotlin
val your_values_array = arrayOf("new_value_1", "new_value_2")
Pushdy.pushAttribute("your_attribute_name", your_values_array)
```

{% 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. Player ID chỉ thay đổi khi bạn thay đổi Device ID bằng hàm `setDeviceID`.&#x20;

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

```objectivec
String playerID = Pushdy.getPlayerID();
```

{% endtab %}

{% tab title="Kotlin" %}

```kotlin
var playerID = Pushdy.getPlayerID()
```

{% endtab %}
{% endtabs %}

## Các Delegation trong Android Native SDK

Để lắng nghe được các Pushdy Callback, bạn phải implement `PushdyDelegate` trong `AppDelegate` của bạn.

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

```java
import com.pushdy.Pushdy;
public class TestApplication extends Application 
                        implements Pushdy.PushdyDelegate {
   // ...
}
```

{% endtab %}

{% tab title="Kotlin" %}

```kotlin
import com.pushdy.Pushdy
class ExampleApplication : Application(), Pushdy.PushdyDelegate {
    // ...
}
```

{% endtab %}
{% endtabs %}

### `readyForHandlingNotification`

Nếu hàm trả về 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 trả về 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="Java" %}

```java
@Override
public boolean readyForHandlingNotification() {
    boolean already = true;
    // your custom logic
    if (my_screen_name == "tutorial") {
        // không hiển thị push ở màn hình Tutorial mà sẽ chủ động hiển thị khi 
        // sau khi user hoàn thành tutorial
        already = false;
    }
    return already;
}
```

{% endtab %}

{% tab title="Kotlin" %}

```kotlin
override fun readyForHandlingNotification() : Boolean {
    var already = true
    // your custom logic
    if (my_screen_name == "tutorial") {
        // không hiển thị push ở màn hình Tutorial mà sẽ chủ động hiển thị khi 
        // sau khi user hoàn thành tutorial
        already = false;
    }
    return already
}
```

{% 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="Java" %}

```java
@Override
public void onNotificationReceived(@NotNull Map<String, ?> notification, 
    @NotNull String fromState) {
    System.out.println("Received notification from state: " + fromState);
}
```

{% endtab %}

{% tab title="Kotlin" %}

```kotlin
override fun onNotificationReceived(notification: Map<String, Any>, 
   fromState: String) {
   println("Received notification from state: " + fromState) 
}
```

{% endtab %}
{% endtabs %}

### `onNotificationOpened`

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

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

```java
@Override
public void onNotificationOpened(@NotNull Map<String, ?> notification, 
    @NotNull String fromState) {
    System.out.println("Opened notification from state: " + fromState);
}
```

{% endtab %}

{% tab title="Kotlin" %}

```kotlin
override fun onNotificationOpened(notification: Map<String, Any>, fromState: String) {
    println("Opened notification from state: " + fromState)   
}
```

{% 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="Java" %}

```java
Pushdy.setPushBannerAutoDismiss(true);
```

{% endtab %}

{% tab title="Kotlin" %}

```kotlin
Pushdy.setPushBannerAutoDismiss(true)
```

{% endtab %}
{% endtabs %}

### `setPushBannerAutoDismissDuration`

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="Java" %}

```java
Pushdy.setPushBannerDismissDuration(5);
```

{% endtab %}

{% tab title="Kotlin" %}

```kotlin
Pushdy.setPushBannerDismissDuration(5)
```

{% endtab %}
{% endtabs %}

### `setCustomPushBanner`

Tuỳ chỉnh cửa sổ hiển thị Push Notification.

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

```java
Pushdy.setCustomPushBanner(yourUIView);
```

{% endtab %}

{% tab title="Kotlin" %}

```kotlin
Pushdy.setCustomPushBanner(yourUIView)
```

{% 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="Java" %}

```java
PDYNotificationView.setCustomMediaKey("your_custom_media_key");
```

{% endtab %}

{% tab title="Kotlin" %}

```kotlin
PDYNotificationView.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/android-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.
