diff --git a/app/src/main/java/com/example/pap_teste/ReservationNotificationService.java b/app/src/main/java/com/example/pap_teste/ReservationNotificationService.java new file mode 100644 index 0000000..55c3e9c --- /dev/null +++ b/app/src/main/java/com/example/pap_teste/ReservationNotificationService.java @@ -0,0 +1,141 @@ +package com.example.pap_teste; + +import android.app.NotificationChannel; +import android.app.NotificationManager; +import android.app.PendingIntent; +import android.app.Service; +import android.content.Intent; +import android.content.pm.PackageManager; +import android.os.Build; +import android.os.IBinder; +import androidx.annotation.NonNull; +import androidx.annotation.Nullable; +import androidx.core.app.NotificationCompat; +import androidx.core.content.ContextCompat; + +import com.example.pap_teste.models.Reserva; +import com.google.firebase.auth.FirebaseAuth; +import com.google.firebase.auth.FirebaseUser; +import com.google.firebase.database.ChildEventListener; +import com.google.firebase.database.DataSnapshot; +import com.google.firebase.database.DatabaseError; +import com.google.firebase.database.DatabaseReference; +import com.google.firebase.database.FirebaseDatabase; + +public class ReservationNotificationService extends Service { + + private static final String CHANNEL_ID = "ReservaNotifications"; + private DatabaseReference reservasRef; + private ChildEventListener childEventListener; + private String currentUserEmail; + + @Override + public void onCreate() { + super.onCreate(); + createNotificationChannel(); + } + + @Override + public int onStartCommand(Intent intent, int flags, int startId) { + FirebaseUser user = FirebaseAuth.getInstance().getCurrentUser(); + if (user != null && user.getEmail() != null) { + currentUserEmail = user.getEmail(); + startListeningForReservations(); + } else { + stopSelf(); + } + return START_STICKY; + } + + private void startListeningForReservations() { + if (reservasRef != null && childEventListener != null) { + return; // Already listening + } + + reservasRef = FirebaseDatabase.getInstance().getReference("reservas"); + childEventListener = new ChildEventListener() { + @Override + public void onChildAdded(@NonNull DataSnapshot snapshot, @Nullable String previousChildName) { + // Not needed for new reservations created by the user + } + + @Override + public void onChildChanged(@NonNull DataSnapshot snapshot, @Nullable String previousChildName) { + Reserva reserva = snapshot.getValue(Reserva.class); + if (reserva != null && currentUserEmail.equals(reserva.getClienteEmail())) { + String estado = reserva.getEstado(); + if (estado != null && (estado.startsWith("Confirmada") || estado.equals("Recusada"))) { + sendNotification(reserva); + } + } + } + + @Override + public void onChildRemoved(@NonNull DataSnapshot snapshot) {} + + @Override + public void onChildMoved(@NonNull DataSnapshot snapshot, @Nullable String previousChildName) {} + + @Override + public void onCancelled(@NonNull DatabaseError error) {} + }; + + reservasRef.addChildEventListener(childEventListener); + } + + private void sendNotification(Reserva reserva) { + if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.TIRAMISU && + ContextCompat.checkSelfPermission(this, android.Manifest.permission.POST_NOTIFICATIONS) != PackageManager.PERMISSION_GRANTED) { + return; // Permission not granted + } + + Intent intent = new Intent(this, MainActivity.class); // Or deep link to reservations + intent.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK | Intent.FLAG_ACTIVITY_CLEAR_TASK); + PendingIntent pendingIntent = PendingIntent.getActivity(this, 0, intent, PendingIntent.FLAG_IMMUTABLE); + + String title = "Atualização de Reserva"; + String message = "A sua reserva no " + reserva.getRestauranteName() + " foi " + reserva.getEstado().toLowerCase() + "."; + + NotificationCompat.Builder builder = new NotificationCompat.Builder(this, CHANNEL_ID) + .setSmallIcon(R.drawable.na_mesa) // Assuming na_mesa is a valid drawable + .setContentTitle(title) + .setContentText(message) + .setPriority(NotificationCompat.PRIORITY_HIGH) + .setContentIntent(pendingIntent) + .setAutoCancel(true); + + NotificationManager notificationManager = getSystemService(NotificationManager.class); + if (notificationManager != null) { + notificationManager.notify(reserva.getId().hashCode(), builder.build()); + } + } + + private void createNotificationChannel() { + if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.O) { + CharSequence name = "Notificações de Reservas"; + String description = "Notificações sobre o estado das suas reservas"; + int importance = NotificationManager.IMPORTANCE_HIGH; + NotificationChannel channel = new NotificationChannel(CHANNEL_ID, name, importance); + channel.setDescription(description); + + NotificationManager notificationManager = getSystemService(NotificationManager.class); + if (notificationManager != null) { + notificationManager.createNotificationChannel(channel); + } + } + } + + @Override + public void onDestroy() { + super.onDestroy(); + if (reservasRef != null && childEventListener != null) { + reservasRef.removeEventListener(childEventListener); + } + } + + @Nullable + @Override + public IBinder onBind(Intent intent) { + return null; + } +} diff --git a/app/src/main/res/layout/item_category_row.xml b/app/src/main/res/layout/item_category_row.xml new file mode 100644 index 0000000..f07080f --- /dev/null +++ b/app/src/main/res/layout/item_category_row.xml @@ -0,0 +1,32 @@ + + + + + + +