Problems with scheduled notifications

Asked

Viewed 25 times

0

I am developing an application that must issue a notification with date and time informed by the user and the notification is not being issued.

I’m using Alarmmanager to set the time and another class I inherit from Broadcastreceiver to launch the notification.

I’ve already declared the Receiver on Androidmanifest.

Class scheduler_vaccine.java

                Intent intent = new Intent(getBaseContext(), AlarmeNotificiation.class);

                PendingIntent pendingIntent = PendingIntent.getBroadcast(getBaseContext(), 0, intent, 0);

                Toast.makeText(agendar_vacina.this, "Dia: "+dia+" Mes: "+mes+" Ano: "+ano, Toast.LENGTH_SHORT).show();

                Calendar c1 = Calendar.getInstance();

                Calendar c = (Calendar) c1.clone();

                c.setTimeInMillis(System.currentTimeMillis());

                //c.set(Calendar.MONTH, mes);
                //c.set(Calendar.YEAR, ano);
                //c.set(Calendar.DAY_OF_MONTH, dia);
                //c.set(Calendar.HOUR_OF_DAY, 11);
                //c.set(Calendar.MINUTE, 33);

                c.set(ano, mes, dia, 12, 02);



                AlarmManager alarmManager = (AlarmManager) getSystemService((getBaseContext().ALARM_SERVICE));

                //alarmManager.set(AlarmManager.RTC_WAKEUP, c.getTimeInMillis(),pendingIntent);

                alarmManager.setExact(AlarmManager.RTC_WAKEUP, c.getTimeInMillis(),pendingIntent);

Alarmenotification class.java

public class AlarmeNotificiation extends BroadcastReceiver {

private static final String CHANNEL_ID = "CHANNEL_ID";
private static final String TAG = "main";


@Override
public void onReceive(Context context, Intent intent) {


    int id = (int) (Math.random()*1000);
    createNotificationChannel(context);

    Intent it = new Intent(context.getApplicationContext(), MainActivity_tela_principal.class);

    PendingIntent pendingIntent = PendingIntent.getBroadcast(context.getApplicationContext(), id,  it, 0);

    NotificationCompat.Builder builder = new NotificationCompat.Builder(context.getApplicationContext(), CHANNEL_ID)
            .setSmallIcon(R.drawable.icon)
            .setContentTitle("Notificação")
            .setContentText("Descrição")
            .setPriority(NotificationCompat.PRIORITY_DEFAULT)
            //.addAction(R.drawable.icon, "Ok", pendingIntent);
            ;


    NotificationManagerCompat notificationManagerCompat = NotificationManagerCompat.from(context.getApplicationContext());

    notificationManagerCompat.notify(id, builder.build());




}



private void createNotificationChannel(Context context) {

    if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.O) {
        CharSequence name =  "Channel name";
        String description = "Channel description";
        int importance = NotificationManager.IMPORTANCE_DEFAULT;
        NotificationChannel channel = new NotificationChannel(CHANNEL_ID, name, importance);
        channel.setDescription(description);
        NotificationManager notificationManager = context.getSystemService(NotificationManager.class);
        notificationManager.createNotificationChannel(channel);
    }
}

}

  • Is there an error? Or just doesn’t show the notification.

  • If the notification has no priority to run at an exact time, you can upgrade the workmanager library....

No answers

Browser other questions tagged

You are not signed in. Login or sign up in order to post.