package com.gramiatechnologies.gramia import android.util.Log import androidx.core.app.NotificationCompat import androidx.core.app.NotificationManagerCompat import com.google.firebase.messaging.FirebaseMessagingService import com.google.firebase.messaging.RemoteMessage class GramiaFirebaseMessagingService : FirebaseMessagingService() { override fun onNewToken(token: String) { super.onNewToken(token) getSharedPreferences(PREFS_NAME, MODE_PRIVATE) .edit() .putString(TOKEN_KEY, token) .apply() Log.i(TAG, "FCM token refreshed") } override fun onMessageReceived(message: RemoteMessage) { super.onMessageReceived(message) val title = message.notification?.title ?: message.data["title"] ?: "Gramia" val body = message.notification?.body ?: message.data["body"] ?: return val openAppUpdate = message.data["type"].equals("app_update", ignoreCase = true) || message.data["open_app_update"] == "true" val updateVersionCode = message.data["version_code"]?.toLongOrNull() val notificationId = if (openAppUpdate && updateVersionCode != null) { getSharedPreferences("gramia_app_updates", MODE_PRIVATE) .edit() .putLong("last_notified_version_code", updateVersionCode) .apply() ("app-update-$updateVersionCode").hashCode() } else { System.currentTimeMillis().toInt() } NotificationUtils.ensureNotificationChannel(this) val contentIntent = NotificationUtils.buildLaunchAppPendingIntent( this, notificationId, openAppUpdate ) val notification = NotificationCompat.Builder(this, "easiwrap_channel") .setSmallIcon(R.drawable.ic_notification_ee) .setContentTitle(title) .setContentText(body) .setStyle(NotificationCompat.BigTextStyle().bigText(body)) .setContentIntent(contentIntent) .setAutoCancel(true) .setPriority(NotificationCompat.PRIORITY_HIGH) .build() try { NotificationManagerCompat.from(this) .notify(notificationId, notification) } catch (error: SecurityException) { Log.w(TAG, "Notification permission is unavailable", error) } } companion object { const val PREFS_NAME = "gramia_fcm" const val TOKEN_KEY = "registration_token" private const val TAG = "GramiaFCM" } }