package com.gramiatechnologies.gramia import android.Manifest import android.app.NotificationChannel import android.app.NotificationManager import android.app.PendingIntent import android.content.Context import android.content.Intent import android.content.pm.PackageManager import android.os.Build import androidx.core.app.ActivityCompat import androidx.core.app.NotificationCompat import androidx.core.app.NotificationManagerCompat object NotificationUtils { private const val CHANNEL_ID = "easiwrap_channel" private const val CHANNEL_NAME = "Gramia Notifications" fun ensureNotificationChannel(context: Context) { if (Build.VERSION.SDK_INT < Build.VERSION_CODES.O) return val manager = context.getSystemService(NotificationManager::class.java) ?: return val existing = manager.getNotificationChannel(CHANNEL_ID) if (existing != null) return val channel = NotificationChannel( CHANNEL_ID, CHANNEL_NAME, NotificationManager.IMPORTANCE_HIGH ) manager.createNotificationChannel(channel) } fun buildLaunchAppPendingIntent( context: Context, requestCode: Int = 0, openAppUpdate: Boolean = false ): PendingIntent { val launchIntent = context.packageManager.getLaunchIntentForPackage(context.packageName) ?: Intent(context, MainActivity::class.java) launchIntent.apply { action = Intent.ACTION_MAIN addCategory(Intent.CATEGORY_LAUNCHER) putExtra("opened_from_notification", true) putExtra("open_app_update", openAppUpdate) addFlags(Intent.FLAG_ACTIVITY_NEW_TASK or Intent.FLAG_ACTIVITY_CLEAR_TOP or Intent.FLAG_ACTIVITY_SINGLE_TOP) } return PendingIntent.getActivity( context, requestCode, launchIntent, PendingIntent.FLAG_UPDATE_CURRENT or PendingIntent.FLAG_IMMUTABLE ) } fun showNotification( context: Context, title: String, message: String, notificationId: Int = System.currentTimeMillis().toInt(), openAppUpdate: Boolean = false ) { ensureNotificationChannel(context) val contentIntent = buildLaunchAppPendingIntent(context, notificationId, openAppUpdate) val builder = NotificationCompat.Builder(context, CHANNEL_ID) .setSmallIcon(R.drawable.ic_notification) .setContentTitle(title) .setContentText(message) .setContentIntent(contentIntent) .setAutoCancel(true) .setPriority(NotificationCompat.PRIORITY_HIGH) val manager = NotificationManagerCompat.from(context) if (ActivityCompat.checkSelfPermission( context, Manifest.permission.POST_NOTIFICATIONS ) != PackageManager.PERMISSION_GRANTED ) { // TODO: Consider calling // ActivityCompat#requestPermissions // here to request the missing permissions, and then overriding // public void onRequestPermissionsResult(int requestCode, String[] permissions, // int[] grantResults) // to handle the case where the user grants the permission. See the documentation // for ActivityCompat#requestPermissions for more details. return } manager.notify(notificationId, builder.build()) } }