aboutsummaryrefslogtreecommitdiff
path: root/juick-notifications/src/main/java/com/juick/components/CleanUp.java
blob: 7b800fecfef21ca7cf93970d697d3b0c498fec7b (plain) (blame)
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
package com.juick.components;

import com.juick.components.service.TokenService;
import com.notnoop.apns.ApnsService;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.scheduling.annotation.Scheduled;
import org.springframework.stereotype.Component;

import javax.inject.Inject;
import java.util.ArrayList;
import java.util.Collection;

/**
 * Created by vitalyster on 22.11.2016.
 */
@Component
public class CleanUp {

    private static Logger logger = LoggerFactory.getLogger(CleanUp.class);

    @Inject
    ApnsService apns;
    @Inject
    TokenService tokenService;

    @Inject
    Notifications push;

    @Scheduled(fixedRate = 600000)
    public void cleanupTokens() {
        logger.info("initializing apns tokens cleanup");
        Collection<String> devices = apns.getInactiveDevices().keySet();
        int count = devices.size();
        if (count > 0) {
            logger.info("{} tokens to delete", count);
            tokenService.deleteTokens("apns", new ArrayList<>(devices));
        } else {
            logger.info("No APNS tokens to delete");
        }
        logger.debug("initializing GCM tokens cleanup: {} tokens", push.getInvalidGCMTokens().size());
        tokenService.deleteTokens("gcm", push.getInvalidGCMTokens());
        push.cleanupGCMTokens();
    }
}