APNSConnector implements PushNotificationProvider { @Override public PushToken push(@Nonnull final Notification notification, @Nonnull final PushToken pushToken) throws APNSGatewayConnectionException, InvalidPushTokenException { assert notification != null; assert pushToken != null; LOG.debug("push: pushToken={}", logId(pushToken.getToken())); if (!apnsProperties.isEnabled()) { LOG.debug("push: notification not sent because connector is disabled, pushToken={}", logId(pushToken.getToken())); return pushToken; } if (pushConnection == null) { LOG.debug("push: fetching new connection, pushToken={}", logId(pushToken.getToken())); pushConnection = apnsConnectionFactory.createPushConnection(); try { sendPush(notification, pushToken); } catch (final APNSGatewayConnectionException e) { LOG.warn("push: exception sending notification, pushToken={}", logId(pushToken.getToken())); assert pushConnection != null; pushConnection.close(); pushConnection = null; throw e; } } else { LOG.debug("push: using existing connection, pushToken={}", logId(pushToken.getToken())); try { sendPush(notification, pushToken); } catch (final APNSGatewayConnectionException ignored) { LOG.debug("push: existing connection possibly faulty, creating new one and retrying, pushToken={}", logId(pushToken.getToken())); assert pushConnection != null; pushConnection.close(); pushConnection = apnsConnectionFactory.createPushConnection(); try { sendPush(notification, pushToken); } catch (final APNSGatewayConnectionException e) { LOG.warn("push: exception sending notification, pushToken={}", logId(pushToken.getToken())); assert pushConnection != null; pushConnection.close(); pushConnection = null; throw e; } } } return pushToken; } APNSConnector(@Nonnull final APNSProperties apnsProperties); @Override PushToken push(@Nonnull final Notification notification, @Nonnull final PushToken pushToken); @Nonnull @Override NotificationChannelType getSupportedNotificationChannelType(); @Nonnull @Override Set<PushToken> getObsoletePushTokens(); @Override boolean canGetObsoletePushTokens(); @Override boolean isEnabled();  }
@SuppressWarnings("JUnitTestMethodWithNoAssertions") @Ignore @Test public void testPush() throws APNSGatewayConnectionException, InvalidPushTokenException { LOG.info("testPush"); final Notification notification = new Notification( new Uid<Notification>(), "MESSAGE", Duration.standardHours(1), "Test message - ignore", new TestPayLoad("123-122", "test message", 4), 1); apnsConnector.push(notification, TestToken.PUSH_CHANNEL_ID.getToken()); try { Thread.sleep(5000); } catch (final InterruptedException ignored) { LOG.error("testPush: sleeping problems"); } apnsConnector.push(notification, TestToken.PUSH_CHANNEL_ID.getToken()); }
@Test(expected = APNSGatewayConnectionException.class) public void testPushBadConnection() throws APNSGatewayConnectionException, InvalidPushTokenException { LOG.info("testPushBadConnection"); final APNSProperties apnsProperties = new APNSProperties( "gateway.sandbox.push.apple.com", 2195, "feedback.sandbox.push.apple.com", 2196, "/com/tomtom/speedtools/push/implementation/apns/tomtomspeedtools-apns-sandbox.p12", "wronggggg", 2, true); final APNSConnector faultyAPNSConnector = new APNSConnector(apnsProperties); final Notification notification = new Notification( new Uid<Notification>(), "TRIP_STATUS_CHANGE", Duration.standardHours(6), "Happy New Year!", null, 0); faultyAPNSConnector.push(notification, TestToken.PUSH_CHANNEL_ID.getToken()); }