Android (Kotlin)
Official pushtifly-android SDK on top of Firebase Messaging, or direct integration with FCM.
The official SDK com.daertech.pushtifly:pushtifly-android runs on Android 7.0 (API 24) or later and can be used from Kotlin or Java. It builds on Firebase Messaging and adds:
- device registration, with an offline queue;
- the Android 13+ permission and channel;
- notifications while the app is open, with image and buttons;
- reported opens and clicks;
- users, tags and topics.
Requirements
- The app registered in a Firebase project, with its
google-services.jsoninapp/and thecom.google.gms.google-servicesplugin. - The Firebase credential uploaded to Pushtifly.
Installation
// app/build.gradle.kts
dependencies {
implementation("com.daertech.pushtifly:pushtifly-android:0.1.0")
}
Until the SDK is on a public Maven repository, there are two options:
- Local repository: publish it with
./gradlew :pushtifly:publishReleasePublicationToLocalRepositoryinsdks/androidand addmaven { url = uri("…/sdks/android/pushtifly/build/repo") }to your repositories. - Source code: include it with
includeBuild, as in theexamples/androidsample.
The SDK brings Firebase Messaging (BoM 35.1.0), its own FirebaseMessagingService and the POST_NOTIFICATIONS permission.
Usage
class MyApp : Application() {
override fun onCreate() {
super.onCreate()
Pushtifly.init(this, PushtiflyOptions(
appKey = "pk_live_YOUR_PUBLIC_KEY", // PUBLIC key only
androidChannel = AndroidChannel(id = "general", name = "General notices"),
))
Pushtifly.addNotificationOpenedListener { n ->
// n.deepLink, n.actionId (tapped button), n.data (your data)
}
}
}
// In an activity (ComponentActivity), once the user understands what it's for:
Pushtifly.requestPermission(this) { state -> /* GRANTED, DENIED */ }
Pushtifly.identify("customer-123") // on sign-in
Pushtifly.setTags(mapOf("plan" to "gold"))
Pushtifly.subscribe("offers")
Pushtifly.pairTestDevice("483920") { ok -> }
Pushtifly.logout() // on sign-out
Opens are detected automatically when the activity resumes. If your activity is singleTop or singleTask, call setIntent(intent) in onNewIntent(), or Pushtifly.handleIntent(intent).
Calls don't wait for the network: they're stored in a persistent queue. They're sent when there's a token and a connection, and retried when the user comes back to the app.
PushtiflyOptions options
| Option | Default | Description |
|---|---|---|
appKey |
— | Public key pk_live_… or pk_test_…. The SDK rejects secret keys. |
appId |
— | Only if the key isn't restricted to one app. |
autoRegister |
false |
Ask for permission in the first activity. |
foregroundDisplay |
true |
Show notifications while the app is open. |
androidChannel |
— | Channel the SDK creates; it must match the app's default channel in Pushtifly. |
debug |
false |
Logs in Logcat (tag Pushtifly). |
If you already have a FirebaseMessagingService
Android delivers messages to a single service. Remove the SDK's in your manifest and call the SDK from yours:
<service android:name="com.daertech.pushtifly.PushtiflyMessagingService" tools:node="remove" />
override fun onNewToken(token: String) = Pushtifly.onNewToken(this, token)
override fun onMessageReceived(message: RemoteMessage) = Pushtifly.onMessageReceived(this, message)
Icon
The small icon must be monochrome, white on transparent; otherwise a white square appears. The SDK uses Firebase's if you declare it:
<meta-data android:name="com.google.firebase.messaging.default_notification_icon" android:resource="@drawable/ic_notification" />
<meta-data android:name="com.google.firebase.messaging.default_notification_channel_id" android:value="general" />
Images and buttons
- Images: Pushtifly sends the image in FCM's
imagefield and Android shows it by itself. While the app is open, the SDK downloads and shows it. - Buttons: they appear in the notifications the SDK shows while the app is open. With the app in the background, Android shows the notification without buttons.
Firebase Installation ID
If you turn on Firebase's Installation ID mode in the app, the SDK detects it and registers the FID instead of the registration token, with no changes to your code. See Firebase Installation ID.
What the SDK reports
| Event | When |
|---|---|
received |
The notification arrives while the app is open. |
opened |
The user taps the notification. |
clicked |
The user taps a button (includes actionId). |
permission_granted / permission_denied |
The permission changes. |
Without the SDK (REST)
With Firebase Messaging, register the token yourself with POST /v1/devices, with platform: "android", provider: "fcm" and framework: "native_android". On open, read _phid and deepLink from intent.extras and report opened to /v1/events.