Запланированные уведомления отображаются сразу после сворачивания или закрытия приложения - PullRequest
0 голосов
/ 27 января 2020

Я создаю уведомление и планирую его с помощью AlarmManager. Если я нахожусь в приложении после того, как запланировал уведомление, оно отображается после установленной задержки. К сожалению, если я минимизирую или закрываю приложение, сразу отображается уведомление. (например, я запланировал уведомление на 10 секунд и минимизировал приложение через 3 секунды, показывается уведомление. Что я делаю не так?

FragmentBills.class

private void scheduleNotification(Notification notification, int delay){
    Intent notificationIntent = new Intent(getContext(), NotificationPublisher.class);
    notificationIntent.putExtra(NotificationPublisher.NOTIFICATION_ID, 0);
    notificationIntent.putExtra(NotificationPublisher.NOTIFICATION, notification);
    PendingIntent pendingIntent = PendingIntent.getBroadcast(getContext(), 0, notificationIntent, PendingIntent.FLAG_UPDATE_CURRENT);

    //tutaj pobieram billDate.getTimeInMillis() - oneDayInMillis (billDate mam [chyba] zawsze na 15:00)
    SimpleDateFormat simpleDateFormat = new SimpleDateFormat("E, dd.MM.yyyy", Locale.getDefault());
    Calendar calendar = simpleDateFormat.getCalendar();
    Date date = calendar.getTime();
    long futureInMillis = date.getTime() + delay;
    AlarmManager alarmManager = (AlarmManager) getActivity().getSystemService(Context.ALARM_SERVICE);
    alarmManager.set(AlarmManager.ELAPSED_REALTIME_WAKEUP, futureInMillis, pendingIntent);


}

private Notification getNotification(){
    NotificationCompat.Builder builder = new NotificationCompat.Builder(getContext(), "channel_id")
            .setSmallIcon(R.drawable.ic_launcher_background)
            .setContentTitle("My notificatiop")
            .setContentText("Eluwinp")
            .setPriority(NotificationCompat.PRIORITY_DEFAULT);
    return builder.build();
}


public View onCreateView(...) {
       ...

buttonRefresh.setOnClickListener(new View.OnClickListener() {
        @Override
        public void onClick(View view) {
            scheduleNotification(getNotification(), 5000);
        }
    });

       ...

NotificationPublisher.class

public class NotificationPublisher extends BroadcastReceiver {

public static String NOTIFICATION_ID = "notification_id";
public static String NOTIFICATION = "notification";

@Override
public void onReceive(Context context, Intent intent) {
    NotificationManagerCompat notificationManager = NotificationManagerCompat.from(context);
    Notification notification = intent.getParcelableExtra(NOTIFICATION);
    int id = intent.getIntExtra(NOTIFICATION_ID, 0);
    notificationManager.notify(id, notification);
    }
}
...