Bonjour à tous,

Je suis nouveau sur ce forum, et je débute en développement Android (je m'y suis mis pendant le confinement ). J'ai créé une appli de randonnée et je voudrais afficher des notifications à une date et heure précises (environ une par jour), même si l'appli est fermée.
J'ai codé quelque chose qui fonctionne (avec des alarm managers et un receiver), mais uniquement si je mets un intervalle de 2-5 minutes entre chaque notif. Si je mets un temps plus long, les notifs ne s'affiche plus...par contre elles se créent bien, puisque quand je relance l'appli, elles s'affichent toutes d'un coup dès l'ouverture.

Voici ma classe NotificationService qui lance le receveur via des alarm managers :

Code : Sélectionner tout - Visualiser dans une fenêtre à part
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
 
 public int onStartCommand(Intent intent, int flags, int startId) {
        Log.e(TAG, "onStartCommand");
        super.onStartCommand(intent, flags, startId);
 
        Calendar calendar3 = creerDate(7,18,12,3);
        Calendar calendar4 = creerDate(7,18,12,5);
        Calendar calendar5 = creerDate(7,18,12,15);
        Calendar calendar6 = creerDate(7,18,12,20);
        Calendar calendar8 = creerDate(7,18,12,25);
        Calendar calendar9 = creerDate(7,18,12,30);
        Calendar calendar10 = creerDate(7,17,11,45);
        Calendar calendar11 = creerDate(7,17,12,0);
        Calendar calendar12 = creerDate(7,17,12,30);
        Calendar calendar13 = creerDate(8,1,17,30);
        Calendar calendar14 = creerDate(8,2,17,30);
        Calendar calendar15 = creerDate(8,3,17,30);
        Calendar calendar16 = creerDate(8,4,17,30);
        Calendar calendar17 = creerDate(8,5,17,30);
 
 
        if (System.currentTimeMillis() <= calendar3.getTimeInMillis()) {
         programmerNotification(3,calendar3);
        }
 
        if (System.currentTimeMillis()<= calendar4.getTimeInMillis()) {
            programmerNotification(4,calendar4);
        }
        if (System.currentTimeMillis() <= calendar5.getTimeInMillis()) {
            programmerNotification(5,calendar5);
        }
        if (System.currentTimeMillis() <= calendar6.getTimeInMillis()) {
            programmerNotification(6,calendar6);
        }
        if (System.currentTimeMillis() <= calendar8.getTimeInMillis()) {
            programmerNotification(8, calendar8);
        }
        if (System.currentTimeMillis() <= calendar9.getTimeInMillis()) {
            programmerNotification(9,calendar9);
        }
        if (System.currentTimeMillis() <= calendar10.getTimeInMillis()) {
            programmerNotification(10,calendar10);
        }
        if (System.currentTimeMillis() <= calendar11.getTimeInMillis()) {
            programmerNotification(11,calendar11);
        }
        if (System.currentTimeMillis() <= calendar12.getTimeInMillis()) {
            programmerNotification(12,calendar12);
        }
        if (System.currentTimeMillis() <= calendar13.getTimeInMillis()) {
            programmerNotification(13,calendar13);
        }
        if (System.currentTimeMillis() <= calendar14.getTimeInMillis()) {
            programmerNotification(14,calendar14);
        }
        if (System.currentTimeMillis() <= calendar15.getTimeInMillis()) {
            programmerNotification(15,calendar15);
        }
        if (System.currentTimeMillis() <= calendar16.getTimeInMillis()) {
            programmerNotification(16,calendar16);
        }
 
        if (System.currentTimeMillis() <= calendar17.getTimeInMillis()) {
            programmerNotification(17,calendar17);
        }
 
        return START_STICKY;
    }
 
    private Calendar creerDate(int mois, int jour, int heure, int minute) {
        Calendar calendar = Calendar.getInstance();
        calendar.set(Calendar.YEAR, 2020);
        calendar.set(Calendar.MONTH, mois);
        calendar.set(Calendar.DAY_OF_MONTH, jour);
        calendar.set(Calendar.HOUR_OF_DAY, heure);
        calendar.set(Calendar.MINUTE, minute);
 
        return calendar;
 
    }
 
    private void programmerNotification(int notification, Calendar calendar) {
 
        Intent notificationIntent = new Intent(getApplicationContext(), Receveur.class);
        notificationIntent.putExtra("notification", notification);
        PendingIntent pendingIntent = PendingIntent.getBroadcast(getApplicationContext(), notification, notificationIntent, PendingIntent.FLAG_ONE_SHOT);
 
        AlarmManager alarmManager = (AlarmManager)getSystemService(Context.ALARM_SERVICE);
        alarmManager.setExact(AlarmManager.RTC_WAKEUP, calendar.getTimeInMillis(), pendingIntent);
 
    }
Ma classe Receveur qui crée la notif :

Code : Sélectionner tout - Visualiser dans une fenêtre à part
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
 
 public void onReceive(Context context, Intent intent) {
 
 
        if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.O) {
            CharSequence name = "Notification channel name";
            int importance = NotificationManager.IMPORTANCE_HIGH;
            NotificationChannel channel = new NotificationChannel(CHANNEL_ID, name, importance);
            channel.setSound(null, null);
            channel.enableLights(true);
            channel.setDescription("Notification channel description");
            channel.setLockscreenVisibility(NotificationCompat.VISIBILITY_PUBLIC);
            NotificationManager notificationManager = (NotificationManager) context.getSystemService(NotificationManager.class);
            Objects.requireNonNull(notificationManager).createNotificationChannel(channel);
        }
 
        Intent notificationIntent = new Intent(context, MainActivity.class);
        notificationIntent.setFlags(Intent.FLAG_ACTIVITY_SINGLE_TOP);
        notificationIntent.putExtra("tabb",tab);
        PendingIntent pendingIntent = PendingIntent.getActivity(context,
                tab, notificationIntent, PendingIntent.FLAG_ONE_SHOT);
 
        NotificationManagerCompat notificationManager = NotificationManagerCompat.from(context);
 
        NotificationCompat.Builder notifBuilder = new NotificationCompat.Builder(context, CHANNEL_ID)
                .setSmallIcon(R.drawable.ic_notification)
 
 
                .setContentTitle("Bien arrivés " + ville + " ?")
                .setAutoCancel(true)
                .setLights(0xff0000ff,500,100)
                .setContentIntent(pendingIntent)
                .setContentText("Enregistrer l'étape d'aujourd'hui comme effectuée !")
                .setCategory(NotificationCompat.CATEGORY_TRANSPORT)
                .setColor(context.getResources().getColor(R.color.bleuvert))
                .setPriority(NotificationCompat.PRIORITY_MAX);
 
        notificationManager.notify(tab, notifBuilder.build());
 
 
    }
Et le MainActivity dans lequel je lance le Service :

Code : Sélectionner tout - Visualiser dans une fenêtre à part
1
2
 
startService(new Intent(this, NotificationService.class));

Donc si je crée mes calendars toutes les 2 minutes (12:05, 12:07, 12:09, 12:11...) : ça marche, les notifs s'affichent bien même si l'appli est fermée !
Si je crée mes calendars toutes les 15 minutes (12:15, 12:30, 12:45, 13:00...) : pas de notif ! Pourtant les notifs se créent bien puisque si je relance l'appli plus tard, elles s'affichent toutes d'un coup.

Une idée quelqu'un ?