Skip to content

Integrate Pushwoosh Android SDK

This section contains information on how to integrate the Pushwoosh SDK into your Android application.

Prerequisites

To integrate the Pushwoosh Android SDK into your app, you will need the following:

Integration steps

1. Set up Firebase Cloud Messaging

First, connect your Android project with Firebase:

  • Open your project in Android Studio
  • Navigate to Tools > Firebase > Cloud Messaging and click “Set up Firebase Cloud Messaging”
  • Follow the wizard instructions. In the final step, the wizard will add the necessary dependencies to your build.gradle files

2. Add Pushwoosh SDK

Add the pushwoosh library dependency to your application’s build.gradle:

build.gradle
implementation 'com.pushwoosh:pushwoosh-firebase:6.+'

Replace ”+” with the current version of Pushwoosh Android SDK

3. Add Pushwoosh metadata to AndroidManifest.xml

Open your AndroidManifest.xml file and add the following metadata inside the <application> tag:

<meta-data android:name="com.pushwoosh.appid" android:value="__YOUR_APP_CODE__" />
<meta-data android:name="com.pushwoosh.senderid" android:value="@string/fcm_sender_id" />
<meta-data android:name="com.pushwoosh.apitoken" android:value="__YOUR_API_TOKEN__" />

Where:

  • com.pushwoosh.appid is your Pushwoosh Application Code
  • com.pushwoosh.apitoken is your Pushwoosh API Token

To set com.pushwoosh.senderid, add fcm_sender_id to your project’s res/values/strings.xml file:

res/values/strings.xml
<resources>
<string name="fcm_sender_id">__YOUR_FCM_SENDER_ID__</string>
</resources>

4. Initialization code

Register for push notifications by adding this code to your initialization logic:

Pushwoosh.getInstance().registerForPushNotifications()

5. Run the project

  1. Build and run the project.
  2. Go to the Pushwoosh Control Panel and send a push notification.
  3. You should see the notification in the app.

Extended integration

At this stage, you have already integrated the SDK and can send and receive push notifications. Now, let’s explore the core functionality

Push notifications

In the Pushwoosh SDK, there are two callbacks designed for handling push notifications:

  • onMessageReceived: This method is invoked when a push notification is received.
  • onMessageOpened: This method is called when the user interacts with (opens) the notification

These callbacks enable developers to manage the reception and user interaction with push notifications within their applications:

AndroidManifest.xml
<meta-data
android:name="com.pushwoosh.notification_service_extension"
android:value="com.your.package.YourNotificationServiceExtension" />
YourNotificationServiceExtension.kt
class YourNotificationServiceExtension : NotificationServiceExtension() {
override fun onMessageReceived(data: PushMessage): Boolean {
Log.d("Pushwooshtest", "onMessageReceived: ${data.toJson()}")
return false
}
override fun onMessageOpened(message: PushMessage) {
Log.d("Pushwooshtest", "onMessageOpened: ${message.toJson()}")
}
}

User configuration

By focusing on individual user behavior and preferences, you can deliver personalized content, leading to increased user satisfaction and loyalty

import com.pushwoosh.Pushwoosh
import com.pushwoosh.tags.TagsBundle
import java.util.Date
class Registration {
public void afterUserLogin(User user) {
Pushwoosh pushwoosh = Pushwoosh.getInstance()
// Set user ID
if (user.getUserId() != null) {
pushwoosh.setUserId(user.getUserId())
}
// Set user email
if (user.getEmail() != null) {
pushwoosh.setEmail(user.getEmail())
}
// Setting additional user information as tags for Pushwoosh
if (user.getUserDetails() != null) {
val details = user.getUserDetails()
val tagsBuilder = TagsBundle.Builder()
if (details.getAge() != null) {
tagsBuilder.putInt("age", details.getAge())
}
if (details.getUserName() != null) {
tagsBuilder.putString("name", details.getUserName())
}
if (details.lastLoginDate() != null) {
tagsBuilder.putDate("last_login", details.getLastLoginDate())
}
// Build and send tags
val tags = tagsBuilder.build()
if (!tags.isEmpty()) {
pushwoosh.setTags(tags)
}
}
}
}

Tags

Tags are key-value pairs assigned to users or devices, allowing segmentation based on attributes like preferences or behavior, enabling targeted messaging.

import com.pushwoosh.Pushwoosh
import com.pushwoosh.tags.TagsBundle
import java.util.List
class UpdateUser {
public void afterUserUpdateProfile(User user) {
val pushwoosh = Pushwoosh.getInstance()
val tagsBuilder = TagsBundle.Builder()
// Set list of favorite categories
if (user.favoriteCategories != null && user.favoriteCategories.isNotEmpty()) {
tagsBuilder.putList("favorite_categories", user.getFavoriteCategories())
}
// Set payment information
tagsBuilder.putBoolean("is_subscribed", user.isSubscribed())
tagsBuilder.putString("payment_status", user.getPaymentStatus())
tagsBuilder.putString("billing_address", user.getBillingAddress())
// Build and send tags
pushwoosh.setTags(tagsBuilder.build())
}
}

Events

Events are specific user actions or occurrences within the app that can be tracked to analyze behavior and trigger corresponding messages or actions

import com.pushwoosh.Pushwoosh
import com.pushwoosh.tags.TagsBundle
import java.util.Date
class Registration {
public void afterUserLogin(User user) {
if (user.getUserName() != null && user.getLastLoginDate() != null) {
val attributes = TagsBundle.Builder()
.putString("name", user.getUserName())
.putDate("last_login", user.getLastLoginDate())
.build()
Pushwoosh.getInstance().postEvent("login", attributes)
}
}
public void afterUserPurchase(User user, Product product) {
Pushwoosh pushwoosh = Pushwoosh.getInstance()
// Track purchase event
val purchaseAttributes = TagsBundle.Builder()
.putString("product_id", product.getId())
.putString("product_name", product.getName())
.putDouble("price", product.getPrice())
.putInt("quantity", product.getQuantity())
.build()
Pushwoosh.getInstance().postEvent("purchase", purchaseAttributes)
// Set user tags
val tagsBuilder = TagsBundle.Builder()
tagsBuilder.putDate("last_purchase_date", Date())
val lifetimeSpend = getCurrentLifetimeSpend() + product.getPrice()
tagsBuilder.putDouble("lifetime_spend", getCurrentLifetimeSpend())
pushwoosh.setTags(tagsBuilder.build())
}
}

Rich Media

Rich media refers to interactive and multimedia content, such as images, videos, or HTML, used in notifications and in-app messages to enhance user engagement

To enable the Modal Rich Media feature, you need to configure it in your application’s AndroidManifest.xml file by adding the following metadata entry:

AndroidManifest.xml
<meta-data
android:name="com.pushwoosh.rich_media_type"
android:value="Modal" />
import com.pushwoosh.richmedia.RichMedia
import com.pushwoosh.richmedia.RichMediaManager
import com.pushwoosh.richmedia.RichMediaPresentingDelegate
import com.pushwoosh.inapp.view.config.ModalRichmediaConfig
import com.pushwoosh.inapp.view.config.enums.*
class MainActivity : AppCompatActivity(), RichMediaPresentingDelegate {
override fun onCreate(savedInstanceState: Bundle?) {
super.onCreate(savedInstanceState)
RichMediaManager.setDelegate(this)
// Configure the modal rich media
val config = ModalRichmediaConfig()
.setViewPosition(ModalRichMediaViewPosition.BOTTOM)
.setPresentAnimationType(ModalRichMediaPresentAnimationType.SLIDE_UP)
.setDismissAnimationType(ModalRichMediaDismissAnimationType.SLIDE_DOWN)
// Apply the configuration
RichMediaManager.setDefaultRichMediaConfig(config)
}
override fun shouldPresent(richMedia: RichMedia): Boolean {
Log.d("Pushwoosh", "Rich media will be presented with: ${richMedia.content}")
return true
}
override fun onPresent(richMedia: RichMedia) {
Log.d("Pushwoosh", "Rich media has been presented: ${richMedia.content}")
}
override fun onClose(richMedia: RichMedia) {
Log.d("Pushwoosh", "Rich media has been closed: ${richMedia.content}")
}
override fun onError(richMedia: RichMedia, pushwooshException: PushwooshException) {
Log.e("Pushwoosh", "Failed to present rich media: ${richMedia.content}. Error: ${pushwooshException.message}")
}
}

Troubleshooting

If you encounter any issues during the integration process, please refer to the support and community section.