Closed Jve386 closed 4 months ago
public class Notificaciones {
private static final String CHANNEL_ID = "my_channel";
private static final String CHANNEL_NAME = "My Channel";
private static final String CHANNEL_DESCRIPTION = "Channel Description";
private Context context;
public Notificaciones(Context context) {
this.context = context;
createNotificationChannel();
}
public void showNotification(String message) {
Notification.Builder builder = new Notification.Builder(context, CHANNEL_ID)
.setContentTitle("Record superado!")
.setContentText(message)
.setSmallIcon(R.drawable.logonativeninjas);
NotificationManager notificationManager = (NotificationManager) context.getSystemService(Context.NOTIFICATION_SERVICE);
notificationManager.notify(0, builder.build());
}
private void createNotificationChannel() {
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.O) {
NotificationChannel channel = new NotificationChannel(CHANNEL_ID, CHANNEL_NAME, NotificationManager.IMPORTANCE_DEFAULT);
channel.setDescription(CHANNEL_DESCRIPTION);
NotificationManager notificationManager = context.getSystemService(NotificationManager.class);
notificationManager.createNotificationChannel(channel);
}
}
}
en la lógica de obtención de la puntuación:
NotificationManager notificationManager = getSystemService(NotificationManager.class); String channelId = "record_channel"; CharSequence channelName = "Record Channel"; int importance = NotificationManager.IMPORTANCE_DEFAULT; NotificationChannel channel = new NotificationChannel(channelId, channelName, importance); notificationManager.createNotificationChannel(channel);
// Obtener la puntuación más alta de la base de datos
controlador = new Controlador();
controlador.addDatos(this);
// Obtener la puntuación más alta de la base de datos desde el intent
int puntuacionMasAltaEnBBDD = getIntent().getIntExtra("puntuacionMasAltaEnBBDD", 0);
// Comparar la puntuación final con la puntuación más alta en la base de datos
if (puntuacionFinal > puntuacionMasAltaEnBBDD) {
// Si la puntuación final es mayor que la puntuación más alta en la base de datos, mostrar "Record superado"
txtRecord.setText("Record superado");
// Prueba 1
Toast.makeText(getApplicationContext(), "Record Superado piruli!", Toast.LENGTH_SHORT).show();
//Prueba 2
// Paso 3: Crear la notificación
NotificationCompat.Builder builder = new NotificationCompat.Builder(this, channelId)
.setSmallIcon(R.drawable.logonativeninjas)
.setContentTitle("¡Nuevo récord personal!")
.setContentText("Has superado tu récord anterior en el juego.")
.setPriority(NotificationCompat.PRIORITY_DEFAULT);
// Paso 4: Mostrar la notificación
notificationManager.notify(1, builder.build());
notificaciones.showNotification("Record superado, FELICIDADES!");
} else {
// Si la puntuación final no es mayor, mostrar "Record no superado"
txtRecord.setText("Record no superado");
}
"Se muestra una notificación básica al conseguir una victoria. La notificación tiene el icono de la app y si se pulsa se muestra el tiempo de resolución."