package uk.beads.manager import android.app.NotificationChannel import android.app.NotificationManager import android.app.PendingIntent import android.content.Context import android.content.Intent import android.media.RingtoneManager import android.os.Build import androidx.core.app.NotificationCompat import com.google.firebase.messaging.FirebaseMessagingService import com.google.firebase.messaging.RemoteMessage class MyFirebaseMessagingService : FirebaseMessagingService() { override fun onMessageReceived(message: RemoteMessage) { val title = message.notification?.title ?: "New Order!" val body = message.notification?.body ?: "A new order has been placed" val orderId = message.data["order_id"]?.toIntOrNull() showNotification(title, body, orderId) } override fun onNewToken(token: String) { ApiClient.registerDevice(this, token) } private fun showNotification(title: String, body: String, orderId: Int?) { val channelId = "new_orders" val manager = getSystemService(Context.NOTIFICATION_SERVICE) as NotificationManager if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.O) { val channel = NotificationChannel( channelId, "New Orders", NotificationManager.IMPORTANCE_HIGH ).apply { description = "Instant alerts for new paid orders" enableVibration(true) setSound(RingtoneManager.getDefaultUri(RingtoneManager.TYPE_NOTIFICATION), null) } manager.createNotificationChannel(channel) } val intent = if (orderId != null) { Intent(this, OrderDetailActivity::class.java).putExtra("order_id", orderId) } else { Intent(this, OrdersActivity::class.java) } val pending = PendingIntent.getActivity( this, 0, intent, PendingIntent.FLAG_UPDATE_CURRENT or PendingIntent.FLAG_IMMUTABLE ) val notification = NotificationCompat.Builder(this, channelId) .setSmallIcon(android.R.drawable.ic_dialog_info) .setContentTitle(title) .setContentText(body) .setAutoCancel(true) .setPriority(NotificationCompat.PRIORITY_HIGH) .setSound(RingtoneManager.getDefaultUri(RingtoneManager.TYPE_NOTIFICATION)) .setContentIntent(pending) .build() manager.notify(System.currentTimeMillis().toInt(), notification) } }