package com.gramiatechnologies.gramia import android.app.Notification import android.app.Service import android.content.Intent import android.os.IBinder import androidx.core.app.NotificationCompat import java.util.Timer import java.util.TimerTask import okhttp3.OkHttpClient import okhttp3.Request import okhttp3.FormBody import okhttp3.Call import okhttp3.Callback import okhttp3.Response import java.io.IOException class NotificationService : Service() { private var institutionUserId: String? = null private var userId: String? = null private var apiToken: String? = null private lateinit var timer: Timer override fun onStartCommand(intent: Intent?, flags: Int, startId: Int): Int { institutionUserId = intent?.getStringExtra("institution_user_id") userId = intent?.getStringExtra("user_id") apiToken = intent?.getStringExtra("api_token") if (userId.isNullOrBlank()) { val prefs = getSharedPreferences("easiwrap_prefs", MODE_PRIVATE) userId = prefs.getString("user_id", "") if (institutionUserId.isNullOrBlank()) { institutionUserId = prefs.getString("last_institution_user_id", "") } if (apiToken.isNullOrBlank()) { apiToken = prefs.getString("api_token", "") } } startForeground(1, showNativeNotification("Easiwrap", "Notification service running")) timer = Timer() timer.scheduleAtFixedRate(object : TimerTask() { override fun run() { institutionUserId?.let { instUserId -> checkForNotifications(instUserId) } } }, 0, 10 * 60 * 1000) // Every 10 minutes return START_STICKY } private fun checkForNotifications(institutionUserId: String) { val url = "${ApiConfig.BASE_URL}/notification-count" val bodyBuilder = FormBody.Builder() .add("institution_user", institutionUserId) .add("api", "true") .add("clear", "true") if (!userId.isNullOrBlank()) { bodyBuilder.add("user", userId!!) } val body = bodyBuilder.build() val requestBuilder = Request.Builder().url(url).post(body) if (!apiToken.isNullOrBlank()) { requestBuilder.addHeader("X-Gramia-Api-Token", apiToken!!) } val request = requestBuilder.build() val client = OkHttpClient() client.newCall(request).enqueue(object : Callback { override fun onFailure(call: Call, e: IOException) {} override fun onResponse(call: Call, response: Response) { val responseText = response.body?.string() if (response.isSuccessful && responseText?.contains("record") == true) { showNativeNotification("Easiwrap", "You have new notifications") } } }) } private fun showNativeNotification(title: String, message: String): Notification { NotificationUtils.ensureNotificationChannel(this) val contentIntent = NotificationUtils.buildLaunchAppPendingIntent(this, 1) val builder = NotificationCompat.Builder(this, "easiwrap_channel") .setContentTitle(title) .setContentText(message) .setSmallIcon(R.drawable.ic_notification) .setContentIntent(contentIntent) .setAutoCancel(true) .setPriority(NotificationCompat.PRIORITY_HIGH) return builder.build() } override fun onBind(intent: Intent?): IBinder? = null override fun onDestroy() { timer.cancel() super.onDestroy() } }