Skip to main content

Push Notifications (Android)

OpenCDP supports tracking push notification delivery and open events, plus rich big-picture notifications via image_url.

Registering Device Token​

To send push notifications, you must register the user's FCM token.

import com.google.firebase.messaging.FirebaseMessagingService
import com.google.firebase.messaging.RemoteMessage
import com.opencdp.OpenCDP

class MyFirebaseMessagingService : FirebaseMessagingService() {

override fun onNewToken(token: String) {
super.onNewToken(token)
OpenCDP.registerDeviceToken(token)
}

override fun onMessageReceived(remoteMessage: RemoteMessage) {
super.onMessageReceived(remoteMessage)

if (remoteMessage.data.isNotEmpty()) {
OpenCDP.handlePushDelivery(remoteMessage.data)
}
}
}

Rich push (big-picture)​

When the FCM data payload includes image_url, call showActionableNotification to render a BigPictureStyle notification:

override fun onMessageReceived(remoteMessage: RemoteMessage) {
val data = remoteMessage.data
OpenCDP.handlePushDelivery(data)

Thread {
OpenCDP.showActionableNotification(
context = applicationContext,
data = data,
)
}.start()
}
  • Android 12+ (API 31+): image thumbnail on the right when collapsed; full image when expanded
  • API 24–30: left-side image preview when collapsed
  • Failed download: text-only notification fallback
  • Prefer data-only FCM (no top-level notification block) to avoid duplicate tray entries
  • Action button icons: supported via actions[].icon HTTPS URLs in the payload

Parse the image URL manually with OpenCDPPushPayload.parseImageUrl(data) if you use your own notification plugin.

For full layout diagrams, troubleshooting, and QA checklist, see Rich push images (mobile hub).

Tracking Notification Opens​

Track when a user taps on a notification by calling handlePushOpen.

override fun onCreate(savedInstanceState: Bundle?) {
super.onCreate(savedInstanceState)

intent?.extras?.let { extras ->
val data = mutableMapOf<String, String>()
for (key in extras.keySet()) {
data[key] = extras.get(key).toString()
}

if (data.containsKey("opencdp_payload")) {
OpenCDP.handlePushOpen(data)
}
}
}