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
Follow FCM Android Setup to configure your Android application to connect to Firebase.
Add the
urbanairship-fcmdependency to your application’s build.gradle file:
implementation("com.urbanairship.android:urbanairship-fcm:$airshipVersion")implementation "com.urbanairship.android:urbanairship-fcm:$airshipVersion"ADM
Follow Amazon’s documentation to store your API key as an asset.
Add the
urbanairship-admdependency to your application’s build.gradle file:
implementation("com.urbanairship.android:urbanairship-adm:$airshipVersion")implementation "com.urbanairship.android:urbanairship-adm:$airshipVersion"HMS
Follow Huawei’s documentation to set up the HMS SDK.
Note Airship requires HMS Core Push SDK 6.3.0.304 or newer.
Add the
urbanairship-hmsdependency to your application’s build.gradle file:implementation("com.urbanairship.android:urbanairship-hms:$airshipVersion")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.
Enable user notifications
Airship.push.userNotificationsEnabled = trueAirship.getPush().setUserNotificationsEnabled(true);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.
Listening for notifications
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
}
}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.
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.
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 - Create custom notification channels for Android
- Custom Notification Provider - Customize how notifications are displayed
- Interactive Notifications - Add action buttons to notifications
Categories