Tích hợp push notification Android (Kotlin, FCM)

Kotlin/Java, minSdk 23. Firebase Cloud Messaging, màn hình cuộc gọi toàn màn hình dựng sẵn.

🤖 Android

Kotlin/Java, minSdk 23. Firebase Cloud Messaging, màn hình cuộc gọi toàn màn hình dựng sẵn.

SDK miễn phí, mã nguồn mở (MIT) trên GitHub. Tạo tài khoản miễn phí để có App ID.

  1. Cấu hình Firebase

    • Tạo app Android trên Firebase Console, tải google-services.json đặt vào thư mục app/.
    • Trên TingPush: upload service account JSON của cùng project Firebase đó.
  2. Thêm SDK

    SDK phát hành qua JitPack: thêm kho JitPack và dependency:

    Gradle (Kotlin DSL)
    // settings.gradle.kts
    dependencyResolutionManagement {
        repositories {
            google()
            mavenCentral()
            maven("https://jitpack.io")
        }
    }
    
    // app/build.gradle.kts
    plugins {
        id("com.google.gms.google-services")   // Firebase
    }
    dependencies {
        implementation("com.github.DaoChiMinh:tingpush-sdk:1.1.1")
    }

    Yêu cầu minSdk 23. Quyền, service FCM và màn hình cuộc gọi đã khai báo sẵn trong SDK — không cần sửa manifest.

  3. Khởi tạo trong Application

    Khởi tạo và gán listener trong Application.onCreate để không lỡ sự kiện khi app được đánh thức lúc đang tắt.

    App.kt
    class App : Application() {
        override fun onCreate() {
            super.onCreate()
            TingPush.init(
                this,
                appId = "YOUR_APP_ID",
                options = TingPush.Options(
                    notificationIcon = R.drawable.ic_notification,
                    callTexts = CallTexts(answer = "Nghe", decline = "Từ chối"),
                ),
            )
            TingPush.listener = object : TingPushListener {
                override fun onCallAccepted(call: CallInfo) {
                    // Người dùng bấm Nghe → kết nối cuộc gọi (call.id, call.data["room"])
                }
                override fun onCallDeclined(call: CallInfo) { }
                override fun onCallMissed(call: CallInfo) { }
                override fun onCallEnded(callId: String) { }
                // Người dùng bấm vào thông báo (kể cả lúc app đang tắt)
                override fun onNotificationOpened(notification: TingPushNotification) {
                    // điều hướng theo notification.data["screen"]...
                }
                // Nhận thông báo khi app đang mở
                override fun onNotificationReceived(notification: TingPushNotification) { }
            }
        }
    }
    
    // AndroidManifest.xml: <application android:name=".App" ...>
  4. Activity chính

    MainActivity.kt
    class MainActivity : AppCompatActivity() {
        override fun onCreate(savedInstanceState: Bundle?) {
            super.onCreate(savedInstanceState)
            TingPush.requestNotificationPermission(this)  // Android 13+
            TingPush.handleIntent(intent)                 // mở từ thông báo / bấm Nghe
        }
    
        override fun onNewIntent(intent: Intent) {
            super.onNewIntent(intent)
            TingPush.handleIntent(intent)
        }
    }
  5. Gắn user khi đăng nhập

    Kotlin
    TingPush.login("user-123")                 // sau khi đăng nhập
    TingPush.logout()                          // khi đăng xuất
    TingPush.setTags(mapOf("plan" to "vip"))
    TingPush.endCall(callId)                   // tắt màn hình đổ chuông

    Java: TingPush.init(...), TingPush.login(...), TingPush.INSTANCE.setListener(...).

  6. Để cuộc gọi đổ chuông ổn định

    Kotlin
    // Màn hình "Cài đặt nhận cuộc gọi" trong app
    if (!TingPush.canUseFullScreenIntent(this)) {
        TingPush.openFullScreenIntentSettings(this)      // Android 14+
    }
    if (!TingPush.isIgnoringBatteryOptimizations(this)) {
        TingPush.openBatteryOptimizationSettings(this)   // Xiaomi, Oppo, Vivo…
    }

    Xiaomi, Oppo, Vivo, Realme: hướng dẫn người dùng bật Tự khởi động và tắt tối ưu pin (giống Zalo).

✅ Kiểm tra sau khi tích hợp

  1. Chạy app trên máy thật, bật log: thấy [TingPush] Đã đăng ký thiết bị ….
  2. Tab Thiết bị của dự án: thiết bị xuất hiện; máy iOS có badge VoIP nếu dùng cuộc gọi.
  3. Tab Gửi thông báo: gửi thử tới thiết bị đó.
  4. Chọn 📞 Cuộc gọi, gửi thử khi app đang mở, khi đã tắt app và khi màn hình đang khoá.

Sẵn sàng tích hợp?

Tạo tài khoản để nhận App ID và API Key. Code mẫu trong bảng điều khiển được điền sẵn thông tin dự án của bạn.