# SDK Callbacks

{% hint style="danger" %}
We recommend putting all SDK callbacks within your application's main `onCreate` method to ensure proper handling of all events.
{% endhint %}

## SDK Lifecycle Callbacks

These callbacks can be implemented to observe SDK loading and exiting.

In order to implement these callbacks you must have a class that calls the SDK's `registerSDKLifecycleCallbacks` method and implements the callback(s) within it.

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

```kotlin
PushSDK.registerPushSDKLifecycleCallbacks(object : PNPushSDKLifecycleCallbacks {
    // Add function overrides here
})
```

{% endtab %}

{% tab title="Java" %}

```java
PushSDK.registerPushSDKLifecycleCallbacks(new PNPushSDKLifecycleCallbacks() {
    // Add function overrides here
});
```

{% endtab %}
{% endtabs %}

The following SDK Lifecycle callbacks are available:

### Finished Loading

This callback is executed when the SDK finishes its initialization and is ready for interaction.

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

```kotlin
override fun onPushSDKDidFinishLoading(configuration: PNApplicationConfig, subscriberStatus: PNSubscriberStatus) {
    // Add code to execute after SDK finishes loading
}
```

{% endtab %}

{% tab title="Java" %}

```java
@Override
public void onPushSDKDidFinishLoading(@NonNull PNApplicationConfig configuration, @NonNull PNSubscriberStatus subscriberStatus) {
    // Add code to execute after SDK finishes loading
}
```

{% endtab %}
{% endtabs %}

## Permission Lifecycle Callbacks

These callbacks can be implemented to observe to a user's permission authorization changes.

In order to implement these callbacks you must have a class that calls the SDK's `registerPermissionLifecycleCallbacks` method and implements the callback(s) within it.

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

```kotlin
PushSDK.registerPermissionLifecycleCallbacks(object : PNPermissionLifecycleCallbacks {
    // Add function overrides here
})
```

{% endtab %}

{% tab title="Java" %}

```java
PushSDK.registerPermissionLifecycleCallbacks(new PNPermissionLifecycleCallbacks() {
    // Add function overrides here
});
```

{% endtab %}
{% endtabs %}

The following Permission Lifecycle callbacks are available:

### Permission Status Changed

This callback is executed when a user's permission status/authorization has changed.

The `response` variable represents the user's **new** permission status.

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

```kotlin
override fun onPushSDKDidReceivePermissionResponse(response: PNPermissionResponse) {
    // Add code to execute after a user's notification permission has changed
}
```

{% endtab %}

{% tab title="Java" %}

```java
@Override
public void onPushSDKDidReceivePermissionResponse(@NonNull PNPermissionResponse response) {
    // Add code to execute after a user's notification permission has changed
}

```

{% endtab %}
{% endtabs %}

## Notification Lifecycle Callbacks

These callbacks can be implemented to observe events like impressions, opens, and other interactions with notifications.

In order to implement these callbacks you must have a class that calls the SDK's `registerNotificationLifecycleCallbacks` method and implements the callback(s) within it.

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

```kotlin
PushSDK.registerNotificationLifecycleCallbacks(object : PNNotificationLifecycleCallbacks {
    // Add function overrides here
})
```

{% endtab %}

{% tab title="Java" %}

```java
PushSDK.registerNotificationLifecycleCallbacks(new PNNotificationLifecycleCallbacks() {
    // Add function overrides here
});
```

{% endtab %}
{% endtabs %}

The following Notification Lifecycle callbacks are available:

### Notification Received

This callback is executed when a user receives a notification

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

```kotlin
override fun onPushSDKDidReceiveRemoteNotification(notification: PNNotification) {
    // Add code to execute after the subscriber's device receives a notification
}
```

{% endtab %}

{% tab title="Java" %}

```java
@Override
public void onPushSDKDidReceiveRemoteNotification(@NonNull PNNotification notification) {
    // Add code to execute after the subscriber's device receives a notification
}
```

{% endtab %}
{% endtabs %}

### Notification Opened With URI/URL Destination

This callback is executed when a notification is opened that has a landing URL attached. The subscriber should be navigated to the view that represents the URL.

This method expects a Boolean response. If the response is `true` then the SDK will perform no additional actions for this notification open. If the response is `false` then the SDK will attempt to navigate the subscriber to the destination URL.

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

```kotlin
override fun onPushSDKDidReceiveNotificationDestination(destination: String, interaction: PNNotificationInteraction): Boolean {
    // Navigate to a activity within your application using the destination (the Landing URL)

    // Return true to inform the SDK that it should not navigate
    return true
}
```

{% endtab %}

{% tab title="Java" %}

```java
@Override
public boolean onPushSDKDidReceiveNotificationDestination(@NonNull String destination, @NonNull PNNotificationInteraction pnNotificationInteraction) {
    // Navigate to a activity within your application using the destination (the Landing URL)

    // Return true to inform the SDK that it should not navigate
    return true;
}
```

{% endtab %}
{% endtabs %}

See the [Deep Linking](https://documentation.pushly.com/integration/implementation-steps/android/sdk-kotlin-java/deep-linking) documentation for a complete example.

## App Message Lifecycle Callbacks

These callbacks can be implemented to observe events like impressions, opens, and other interactions with app messages.

In order to implement these callbacks you must have a class that calls the SDK's `registerAppMessageLifecycleCallbacks` method and implements the callback(s) within it.

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

```kotlin
PushSDK.registerAppMessageLifecycleCallbacks(object : PNAppMessageLifecycleCallbacks {
    // Add function overrides here
})
```

{% endtab %}

{% tab title="Java" %}

```java
PushSDK.registerAppMessageLifecycleCallbacks(new PNAppMessageLifecycleCallbacks() {
    // Add function overrides here
});
```

{% endtab %}
{% endtabs %}

The following App Message Lifecycle callbacks are available:

### App Message Will Present

This callback is executed when an App Message is presented to a user.

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

```kotlin
override fun onPushSDKWillPresentAppMessage(appMessage: PNAppMessage) {
    // Add code to execute when a user's device presents an app message
}
```

{% endtab %}

{% tab title="Java" %}

```java
@Override
public void onPushSDKWillPresentAppMessage(@NonNull PNAppMessage appMessage) {
    // Add code to execute when a user's device presents an app message
}
```

{% endtab %}
{% endtabs %}

### App Message Received User Interaction

This callback is executed when a user interacts with an App Message.

This method expects a Boolean response. If the response is `true` then the SDK will not handle the interaction or attempt to perform navigation when the interaction is an Open URL action. If the response is `false` then the SDK will process the interaction and if the interaction is an Open URL action will attempt to navigate the user to the destination URL by using the `open` method.

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

```kotlin
override fun onPushSDKDidReceiveAppMessageInteraction(interaction: PNAppMessageInteraction, appMessage: PNAppMessage): Boolean {
    // Add code to execute when a user interacts with an app message
    
    // Return true to inform the SDK that it should not handle the interaction
    return true
}
```

{% endtab %}

{% tab title="Java" %}

```java
@Override
public boolean onPushSDKDidReceiveAppMessageInteraction(@NonNull PNAppMessageInteraction interaction, @NonNull PNAppMessage appMessage) {
    // Navigate to a activity within your application using the destination (the Landing URL)

    // Return true to inform the SDK that it should not navigate
    return true;
}
```

{% endtab %}
{% endtabs %}
