# Push Notifications for the Android SDK

How to configure your application to receive and respond to notifications.

## Push Provider Setup

Configure a push provider to enable push notifications on Android devices.

### FCM

1. Follow [FCM Android Setup](https://firebase.google.com/docs/android/setup) to configure your
   Android application to connect to Firebase.

1. Add the `urbanairship-fcm` dependency to your application's build.gradle file:


#### Gradle Kotlin


```kotlin
implementation("com.urbanairship.android:urbanairship-fcm:$airshipVersion")
```



#### Gradle Groovy


```groovy
implementation "com.urbanairship.android:urbanairship-fcm:$airshipVersion"
```




### ADM

1. Follow [Amazon's documentation](https://developer.amazon.com/docs/adm/integrate-your-app.html#store-your-api-key-as-an-asset) to store your API key as an asset.

1. Add the `urbanairship-adm` dependency to your application's build.gradle file:


#### Gradle Kotlin


```kotlin
implementation("com.urbanairship.android:urbanairship-adm:$airshipVersion")
```



#### Gradle Groovy


```groovy
implementation "com.urbanairship.android:urbanairship-adm:$airshipVersion"
```




### HMS

1. Follow [Huawei's documentation](https://developer.huawei.com/consumer/en/doc/development/HMSCore-Guides/android-integrating-sdk-0000001050040084)
      to set up the HMS SDK.
   > **Note:** Airship requires HMS Core Push SDK 6.3.0.304 or newer.


1. Add the `urbanairship-hms` dependency to your application's build.gradle file:

   
#### Gradle Kotlin


   ```kotlin
implementation("com.urbanairship.android:urbanairship-hms:$airshipVersion")
```

   

#### Gradle Groovy


   ```groovy
implementation "com.urbanairship.android:urbanairship-hms:$airshipVersion"
```


   


## Enable User Notifications

Enabling `userNotificationsEnabled` will prompt the user for permission to send notifications. To increase the likelihood that the user will accept, you should avoid prompting the user for permission immediately, and instead wait for a more appropriate time in the app.

The Airship SDK makes a distinction between `user notifications`, which can be seen by the user, and other forms of push that allow you to send data to your app silently, or in the background. Enabling or disabling user notifications is a preference often best left up to the user, so by default, user notifications are disabled.


#### Kotlin


```kotlin
Airship.push.userNotificationsEnabled = true
```



#### Java


```java
Airship.getPush().setUserNotificationsEnabled(true);
```




> **Note:** For apps that target Android 13 (API 33) and above, enabling user notifications will display a runtime permission prompt to allow notifications to be sent.
> 
> To increase the likelihood that the user will accept, you should avoid prompting the user for permission immediately on app startup, and instead wait for a more appropriate time to prompt for notification permission.


## Handle Notification Events

The Airship SDK provides several callbacks for when a push is received or a notification is interacted with. Apps can use these callbacks to do custom push processing. Registering for a callback is optional, the SDK will automatically launch the application without the need to set a callback.


#### Kotlin


```kotlin
Airship.push.addPushListener { message: PushMessage, notificationPosted: Boolean ->
    // Called when a message is received
}

Airship.push.notificationListener = object : NotificationListener {
    override fun onNotificationPosted(notificationInfo: NotificationInfo) {
        // Called when a notification is posted
    }

    override fun onNotificationOpened(notificationInfo: NotificationInfo): Boolean {
        // Called when a notification is tapped.
        // Return false here to allow Airship to auto launch the launcher activity
        return false
    }

    override fun onNotificationForegroundAction(
        notificationInfo: NotificationInfo,
        actionButtonInfo: NotificationActionButtonInfo
    ): Boolean {
        // Called when a notification action button is tapped.
        // Return false here to allow Airship to auto launch the launcher activity
        return false
    }

    override fun onNotificationBackgroundAction(
        notificationInfo: NotificationInfo,
        actionButtonInfo: NotificationActionButtonInfo
    ) {
        // Called when a background notification action button is tapped.
    }

    override fun onNotificationDismissed(notificationInfo: NotificationInfo) {
        // Called when a notification is dismissed
    }
}
```



#### Java


```java
Airship.getPush().addPushListener((message, notificationPosted) -> {
    // Called when any push is received
});

Airship.getPush().setNotificationListener(new NotificationListener() {
    @Override
    public void onNotificationPosted(@NonNull NotificationInfo notificationInfo) {
        // Called when a notification is posted
    }

    @Override
    public boolean onNotificationOpened(@NonNull NotificationInfo notificationInfo) {
        // Called when a notification is tapped.
        // Return false here to allow Airship to auto launch the launcher activity
        return false;
    }

    @Override
    public boolean onNotificationForegroundAction(@NonNull NotificationInfo notificationInfo, @NonNull NotificationActionButtonInfo actionButtonInfo) {
        // Called when a notification action button is tapped.
        // Return false here to allow Airship to auto launch the launcher activity
        return false;
    }

    @Override
    public void onNotificationBackgroundAction(@NonNull NotificationInfo notificationInfo, @NonNull NotificationActionButtonInfo actionButtonInfo) {
        // Called when a background notification action button is tapped.
    }

    @Override
    public void onNotificationDismissed(@NonNull NotificationInfo notificationInfo) {
        // Called when a notification is dismissed
    }
});
```




## Silent Notifications

Silent notifications are push messages that do not present a notification to the user. These are typically used to briefly wake the app from a background state to perform processing tasks or fetch remote content.

> **Important:** We recommend that you thoroughly test your implementation to confirm that silent notifications do not generate any device notifications.


For Android, all push messages are delivered in the background, but default Airship will treat messages without an `alert` as silent.

> **Note:** Pushes sent without an `alert` property do not have guaranteed delivery. Factors affecting delivery include battery life, whether the device is connected to WiFi, and the number of silent pushes sent within a recent time period. These metrics are determined solely by Android and FCM. Therefore, this feature is best used for supplementing the regular behavior of the app rather than providing critical functionality. For instance, an app could use a silent push to pre-fetch new data ahead of time in order to reduce load times when the app is later launched by the user.


## Next Steps

- [Notification Channels](https://www.airship.com/docs/developer/sdk-integration/android/push-notifications/advanced-customizations/#notification-channels) - Create custom notification channels for Android
- [Custom Notification Provider](https://www.airship.com/docs/developer/sdk-integration/android/push-notifications/advanced-customizations/#custom-notification-provider) - Customize how notifications are displayed
- [Interactive Notifications](https://www.airship.com/docs/developer/sdk-integration/android/push-notifications/advanced-customizations/#interactive-notifications) - Add action buttons to notifications

If push notifications aren't working as expected, see [Troubleshooting Push Notifications](https://www.airship.com/docs/developer/sdk-integration/android/troubleshooting/push-notifications/) to check notification status and fix common issues.
