Mobile SDK

Mobile SDK

Send push notifications to Android and iOS apps via Firebase Cloud Messaging (FCM). Notirix acts as a unified backend — one API for all platforms.

FCM Setup

Mobile push is powered by Firebase Cloud Messaging. You need a Firebase project and a service account key to connect it to Notirix.

1
Create a Firebase project at console.firebase.google.com and enable Cloud Messaging.
2
Go to Project Settings → Service Accounts and click Generate new private key. Download the JSON file.
3
In Notirix Admin, open Settings → Integrations → FCM and paste the contents of the JSON file.
⚠️
Keep the service account JSON secret — it has full send access to your Firebase project. Never commit it to source control.

Android

Add the Firebase SDK to your Android project, then register the FCM token with Notirix.

Dependencies

gradle
// app/build.gradle
dependencies {
    implementation 'com.google.firebase:firebase-messaging:23.4.0'
}

Register token

Call this once after the user opens the app (or after login if you want to attach a user ID):

kotlin
import com.google.firebase.messaging.FirebaseMessaging
import okhttp3.*
import okhttp3.MediaType.Companion.toMediaType
import org.json.JSONObject

FirebaseMessaging.getInstance().token.addOnSuccessListener { token ->
    val body = JSONObject().apply {
        put("fcmToken",    token)
        put("platform",    "android")
        put("externalId",  currentUserId)   // optional — your user ID
    }
    val request = Request.Builder()
        .url("https://push.notirix.com/api/apps/APP_ID/mobile/register")
        .addHeader("X-API-Key", "YOUR_API_KEY")
        .post(RequestBody.create("application/json".toMediaType(), body.toString()))
        .build()
    OkHttpClient().newCall(request).enqueue(/* callback */)
}
💡
externalId is your internal user identifier (e.g. database ID or email). Pass it to link the push token to a user so you can send targeted pushes via the API.

Handle incoming messages

kotlin
class MyFirebaseService : FirebaseMessagingService() {

    override fun onMessageReceived(message: RemoteMessage) {
        val title   = message.notification?.title ?: message.data["title"] ?: return
        val body    = message.notification?.body  ?: message.data["body"]  ?: ""
        val clickUrl = message.data["url"]

        val intent = Intent(this, MainActivity::class.java).apply {
            clickUrl?.let { putExtra("open_url", it) }
        }
        val pi = PendingIntent.getActivity(this, 0, intent, PendingIntent.FLAG_IMMUTABLE)

        NotificationCompat.Builder(this, CHANNEL_ID)
            .setSmallIcon(R.drawable.ic_notification)
            .setContentTitle(title)
            .setContentText(body)
            .setContentIntent(pi)
            .setAutoCancel(true)
            .build()
            .also { NotificationManagerCompat.from(this).notify(msgId, it) }
    }

    override fun onNewToken(token: String) {
        // re-register when token rotates
        registerWithNotirix(token)
    }
}

iOS

iOS push uses APNs under the hood. FCM bridges APNs → Notirix, so setup is the same on your backend. You only need to configure APNs inside Firebase.

APNs key in Firebase

1
In Apple Developer, create an APNs Auth Key (.p8 file).
2
Upload it in Firebase Console → Project Settings → Cloud Messaging → APNs Authentication Key.

Register token (Swift)

swift
import FirebaseMessaging
import UserNotifications

func application(_ application: UIApplication,
                 didFinishLaunchingWithOptions opts: [UIApplication.LaunchOptionsKey: Any]?) -> Bool {
    UNUserNotificationCenter.current().requestAuthorization(options: [.alert, .badge, .sound]) { _, _ in }
    application.registerForRemoteNotifications()
    Messaging.messaging().delegate = self
    return true
}

extension AppDelegate: MessagingDelegate {
    func messaging(_ messaging: Messaging, didReceiveRegistrationToken fcmToken: String?) {
        guard let token = fcmToken else { return }
        // POST to Notirix
        var req = URLRequest(url: URL(string: "https://push.notirix.com/api/apps/APP_ID/mobile/register")!)
        req.httpMethod = "POST"
        req.setValue("application/json", forHTTPHeaderField: "Content-Type")
        req.setValue("YOUR_API_KEY",     forHTTPHeaderField: "X-API-Key")
        req.httpBody = try? JSONSerialization.data(withJSONObject: [
            "fcmToken":   token,
            "platform":   "ios",
            "externalId": currentUserId,   // optional
        ])
        URLSession.shared.dataTask(with: req).resume()
    }
}
ℹ️
Make sure Push Notifications and Background Modes → Remote notifications are enabled in your Xcode project's Signing & Capabilities.

Event tracking

Track delivery, opens, and custom events by sending events to the Notirix API. The Worker updates MessageDelivery records automatically for web push; for mobile you report events manually.

kotlin
// Example: report "clicked" when user taps a notification
val deliveryId = message.data["deliveryId"] ?: return

val request = Request.Builder()
    .url("https://push.notirix.com/api/mobile/events")
    .addHeader("X-API-Key", "YOUR_API_KEY")
    .post(RequestBody.create(
        "application/json".toMediaType(),
        JSONObject().apply {
            put("deliveryId", deliveryId)
            put("event",      "clicked")
        }.toString()
    ))
    .build()
OkHttpClient().newCall(request).enqueue(/* callback */)
EventWhen to send
deliveredFCM delivery receipt received (onMessageReceived)
clickedUser taps the notification
dismissedUser swipes the notification away