package com.notifyhub.viewer.push import android.app.NotificationChannel import android.app.NotificationManager import android.app.PendingIntent import android.content.Context import androidx.core.app.NotificationCompat import com.google.firebase.messaging.FirebaseMessagingService import com.google.firebase.messaging.RemoteMessage import com.notifyhub.viewer.NotizViewerApp import com.notifyhub.viewer.R import com.notifyhub.viewer.ui.HomeActivity import kotlinx.coroutines.CoroutineScope import kotlinx.coroutines.Dispatchers import kotlinx.coroutines.launch class NotizMessagingService : FirebaseMessagingService() { override fun onNewToken(token: String) { super.onNewToken(token) val app = application as NotizViewerApp CoroutineScope(Dispatchers.IO).launch { val repo = com.notifyhub.viewer.data.AuthRepository(app.api, app.preferences, "NotizViewer-Android") repo.registerPushToken(token) } } override fun onMessageReceived(message: RemoteMessage) { val data = message.data val type = data["type"] ?: "alert" val title = message.notification?.title ?: data["title"] ?: defaultTitle(type) val body = message.notification?.body ?: data["body"] ?: "Tap to view" ensureChannel() val intent = HomeActivity.deepLinkIntent(this, type, data["alert_id"]?.toIntOrNull(), data["message_id"]?.toIntOrNull()) val pending = PendingIntent.getActivity( this, (data["alert_id"]?.toIntOrNull() ?: data["message_id"]?.toIntOrNull() ?: System.currentTimeMillis().toInt()), intent, PendingIntent.FLAG_UPDATE_CURRENT or PendingIntent.FLAG_IMMUTABLE, ) val notification = NotificationCompat.Builder(this, CHANNEL_ID) .setSmallIcon(android.R.drawable.ic_dialog_info) .setContentTitle(title) .setContentText(body) .setPriority(NotificationCompat.PRIORITY_HIGH) .setAutoCancel(true) .setContentIntent(pending) .build() val manager = getSystemService(NotificationManager::class.java) val id = (data["alert_id"]?.hashCode() ?: data["message_id"]?.hashCode() ?: System.currentTimeMillis().toInt()) manager.notify(id, notification) } private fun defaultTitle(type: String): String = when (type) { "message" -> "New message" else -> "New alert" } private fun ensureChannel() { val manager = getSystemService(NotificationManager::class.java) if (manager.getNotificationChannel(CHANNEL_ID) == null) { val channel = NotificationChannel( CHANNEL_ID, getString(R.string.push_channel_name), NotificationManager.IMPORTANCE_HIGH, ).apply { description = getString(R.string.push_channel_description) } manager.createNotificationChannel(channel) } } companion object { const val CHANNEL_ID = "notiz_viewer_alerts" fun ensureChannel(context: Context) { val manager = context.getSystemService(NotificationManager::class.java) if (manager.getNotificationChannel(CHANNEL_ID) == null) { val channel = NotificationChannel( CHANNEL_ID, context.getString(R.string.push_channel_name), NotificationManager.IMPORTANCE_HIGH, ).apply { description = context.getString(R.string.push_channel_description) } manager.createNotificationChannel(channel) } } } }