diff options
Diffstat (limited to 'juick-notifications/src/main/java/com/juick/components')
3 files changed, 64 insertions, 4 deletions
diff --git a/juick-notifications/src/main/java/com/juick/components/CleanUp.java b/juick-notifications/src/main/java/com/juick/components/CleanUp.java new file mode 100644 index 00000000..06d96471 --- /dev/null +++ b/juick-notifications/src/main/java/com/juick/components/CleanUp.java @@ -0,0 +1,52 @@ +package com.juick.components; + +import com.juick.server.helpers.TokensList; +import com.notnoop.apns.ApnsService; +import org.slf4j.Logger; +import org.slf4j.LoggerFactory; +import org.springframework.core.ParameterizedTypeReference; +import org.springframework.http.HttpEntity; +import org.springframework.http.HttpHeaders; +import org.springframework.http.HttpMethod; +import org.springframework.http.MediaType; +import org.springframework.scheduling.annotation.Scheduled; +import org.springframework.stereotype.Component; +import org.springframework.web.client.RestTemplate; + +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 + RestTemplate rest; + + @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(String.format("%d tokens to delete", count)); + TokensList list = new TokensList(); + list.setType("apns"); + list.setTokens(new ArrayList<>(devices)); + HttpHeaders headers = new HttpHeaders(); + headers.setContentType(MediaType.APPLICATION_JSON_UTF8); + rest.exchange("http://api.juick.com/notifications", + HttpMethod.DELETE, new HttpEntity<>(list, headers), new ParameterizedTypeReference<Void>() { + }); + } else { + logger.info("No APNS tokens to delete"); + } + } +} diff --git a/juick-notifications/src/main/java/com/juick/components/Notifications.java b/juick-notifications/src/main/java/com/juick/components/Notifications.java index ebc69bd1..cb120ce9 100644 --- a/juick-notifications/src/main/java/com/juick/components/Notifications.java +++ b/juick-notifications/src/main/java/com/juick/components/Notifications.java @@ -48,6 +48,7 @@ import rocks.xmpp.core.session.XmppSessionConfiguration; import rocks.xmpp.extensions.component.accept.ExternalComponent; import javax.annotation.PostConstruct; +import javax.inject.Inject; import java.io.IOException; import java.util.ArrayList; import java.util.Arrays; @@ -70,10 +71,12 @@ public class Notifications implements AutoCloseable { private final int xmppPort; private final String xmppPushPassword; + @Inject + private ApnsService apns; + public Notifications(final Environment env, final RestTemplate rest) { this.rest = rest; - wns_application_sip = env.getProperty("wns_application_sip", ""); wns_client_secret = env.getProperty("wns_client_secret", ""); GCMSender = new Sender(env.getProperty("gcm_key", ""), Endpoint.GCM); @@ -191,12 +194,10 @@ public class Notifications implements AutoCloseable { }).getBody()); } if (!tokens.isEmpty()) { - ApnsService service = APNS.newService().withCert("/etc/juick/ios.p12", "juick") - .withSandboxDestination().build(); for (String token : tokens) { String payload = APNS.newPayload().alertTitle("@" + jmsg.getUser().getName()).alertBody(jmsg.getText()).build(); logger.info("APNS: " + token); - service.push(token, payload); + apns.push(token, payload); } } else { logger.info("APNS: no recipients"); diff --git a/juick-notifications/src/main/java/com/juick/components/configuration/NotificationsAppConfiguration.java b/juick-notifications/src/main/java/com/juick/components/configuration/NotificationsAppConfiguration.java index c7747aa1..1bb4694a 100644 --- a/juick-notifications/src/main/java/com/juick/components/configuration/NotificationsAppConfiguration.java +++ b/juick-notifications/src/main/java/com/juick/components/configuration/NotificationsAppConfiguration.java @@ -2,6 +2,8 @@ package com.juick.components.configuration; import com.juick.components.Notifications; import com.juick.configuration.DataConfiguration; +import com.notnoop.apns.APNS; +import com.notnoop.apns.ApnsService; import org.springframework.context.annotation.Bean; import org.springframework.context.annotation.Configuration; import org.springframework.context.annotation.Import; @@ -41,4 +43,9 @@ public class NotificationsAppConfiguration { public Notifications push() { return new Notifications(env, rest()); } + @Bean + public ApnsService apns() { + return APNS.newService().withCert(env.getProperty("ios_pkcs12_file"), env.getProperty("ios_pkcs12_password")) + .withSandboxDestination().build(); + } } |