Push Notifications
Register the device with Segmentify and report views and clicks of push campaigns.
Segmentify sends push campaigns through Firebase Cloud Messaging (FCM). Your app receives and displays the notifications; the SDK registers the device and reports what happens to each notification.
Set up
Section titled “Set up”Set the push data center before config:
SegmentifyManager.setPushConfig("https://gimli-eu.segmentify.com") // your push data center URLSegmentifyManager.config(this, "YOUR_API_KEY", "https://gandalf-eu.segmentify.com", "www.yourdomain.com")Add Firebase Cloud Messaging to your app as described in the Firebase documentation. The SDK does not include Firebase; your app receives the messages and passes them on.
Register the device token
Section titled “Register the device token”Send the token to Segmentify whenever you receive or refresh it.
import com.segmentify.segmentifyandroidsdk.model.NotificationModelimport com.segmentify.segmentifyandroidsdk.model.NotificationType
override fun onNewToken(token: String) { val model = NotificationModel() model.type = NotificationType.PERMISSION_INFO model.deviceToken = token SegmentifyManager.sendNotification(model)}Send the token again on app start as well, so a token issued before the SDK was configured is not lost.
Without a registered token the user cannot be targeted by push campaigns.
Report views and clicks
Section titled “Report views and clicks”Build a SegmentifyPushPayload from the incoming message and hand it to the SDK.
import com.google.firebase.messaging.FirebaseMessagingServiceimport com.google.firebase.messaging.RemoteMessageimport com.segmentify.segmentifyandroidsdk.model.SegmentifyPushPayload
class MyFirebaseMessagingService : FirebaseMessagingService() { override fun onMessageReceived(remoteMessage: RemoteMessage) { if (remoteMessage.data.isEmpty()) return
val payload = SegmentifyPushPayload.from(remoteMessage.data) if (payload.title.isNotEmpty() || payload.message.isNotEmpty()) { showNotification(payload) // your own code: build and show the notification } SegmentifyManager.handlePushReceived(payload) }}When the user taps the notification, report the click from the activity that opens:
SegmentifyManager.handlePushClicked(payload)Keep the values you need (at least instanceId) in the notification’s intent extras so you can rebuild the payload there, for example with SegmentifyPushPayload.from(mapOf("instanceId" to instanceId)).
handlePushReceived reports a view and handlePushClicked reports a click. Both need the payload’s instanceId; a message without one is not a Segmentify campaign and is ignored.
The payload
Section titled “The payload”| Property | Type | Description |
|---|---|---|
title, message |
String |
Text of the notification. Empty when not sent. |
image, icon |
String? |
Image and icon URLs. |
deeplink, redirectUrl |
String? |
Where a tap should lead. |
targetUrl |
String? |
deeplink if present, otherwise redirectUrl. |
instanceId |
String? |
ID of the push campaign. Needed to report views and clicks. |
tag |
String? |
Notification tag. |
actions |
List<SegmentifyPushAction> |
Action buttons, each with action, title, icon, redirectUrl, deeplink. |
isV2 |
Boolean |
Whether the message uses the current payload format. |
Attribute what happens next
Section titled “Attribute what happens next”After a click, the SDK remembers the campaign so you can attribute the visit that follows:
val utm = SegmentifyManager.getTrackingParameters()// utm.utm_source = "segmentify", utm.utm_medium = "push", utm.utm_campaign = the campaign's instanceIdAdd these to the deep link or to your own analytics events if you track campaign sources.
Checklist
Section titled “Checklist”- The push data center URL is set.
- The token is sent on first launch and on every refresh.
- Views are reported when a notification arrives, and clicks when it is tapped.
- The notification opens
targetUrlwhen one is present.