GCMConnector implements PushNotificationProvider { @Override public PushToken push(@Nonnull final Notification notification, @Nonnull final PushToken pushToken) throws PushConnectionException, InvalidPushTokenException { assert notification != null; assert pushToken != null; LOG.debug("push: pushToken={}", pushToken); if (!gcmProperties.isEnabled()) { LOG.debug("sendPush: notification not sent because connector is disabled, pushToken={}", logId(pushToken.getToken())); return pushToken; } final Sender sender = new GCMSender(gcmProperties.getEndpoint(), gcmProperties.getApiKey()); final String pushMessage = notification.getMessage(); final JsonRenderable jsonPayload = notification.getPayload(); final Duration timeToLive = notification.getTimeToLive(); Builder builder = new Builder().addData(KEY_TYPE, notification.getType()); if (pushMessage != null) { builder = builder.addData(KEY_MESSAGE, pushMessage); } if (jsonPayload != null) { builder = builder.addData(KEY_PAYLOAD, jsonPayload.toJson()); } if (timeToLive != null) { final long seconds = timeToLive.getStandardSeconds(); if ((seconds >= MIN_SECONDS_TO_LIVE) && (seconds <= MAX_SECONDS_TO_LIVE)) { builder.timeToLive((int) seconds); } } final Message message = builder.build(); final Result result; try { result = sender.send(message, pushToken.getToken(), gcmProperties.getRetries()); LOG.debug("push: notification sent, message={}", message); } catch (final IOException e) { LOG.error("push: unable to send message, pushToken={}", logId(pushToken.getToken()), e); throw new PushConnectionException(e); } if (result.getMessageId() != null) { LOG.debug("push: message delivered to GCM, pushToken={}", logId(pushToken.getToken())); final String canonicalRegId = result.getCanonicalRegistrationId(); if (canonicalRegId != null) { return new PushToken(canonicalRegId); } else { return pushToken; } } else { final String error = result.getErrorCodeName(); LOG.error("push: message could not be delivered, pushToken={}, error={}", logId(pushToken.getToken()), error); if (error.equals(Constants.ERROR_NOT_REGISTERED)) { throw new InvalidPushTokenException(pushToken, "Push token is invalid, application has been removed from device"); } if (error.equals(Constants.ERROR_INVALID_REGISTRATION)) { throw new InvalidPushTokenException(pushToken, "Push token is invalid"); } throw new PushConnectionException("Notification could not be sent: " + error); } } GCMConnector(@Nonnull final GCMProperties gcmProperties); @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 PushConnectionException, 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); gcmConnector.push(notification, TestToken.PUSH_CHANNEL_ID.getToken()); }