Java tutorial
/******************************************************************************* * Copyright 2014 Juan Diego Navarre Gonzalez * * Licensed under the Apache License, Version 2.0 (the "License"); * you may not use this file except in compliance with the License. * You may obtain a copy of the License at * * http://www.apache.org/licenses/LICENSE-2.0 * * Unless required by applicable law or agreed to in writing, software * distributed under the License is distributed on an "AS IS" BASIS, * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. * See the License for the specific language governing permissions and * limitations under the License. ******************************************************************************/ package net.navasoft.madcoin.backend.services.push; import java.io.IOException; import java.util.Collection; import org.apache.commons.httpclient.Header; import org.apache.commons.httpclient.HttpClient; import org.apache.commons.httpclient.HttpException; import org.apache.commons.httpclient.methods.PostMethod; /** * net.navasoft.madcoin.backend.services.push Class class * AndroidPushNotificationServiceImpl. Description: * * @author Juan Diego Navarre Gonzalez - (<a * href="mailto:jdnavarreg@outlook.com">{@literal jdnavarreg@outlook.com} * </a>) * @version 1.0 * @since 27/07/2014 06:48:42 PM */ public class AndroidPushNotificationServiceImpl extends PushNotificationService<AndroidPushNotificationConfiguration> { /** * service. * * @since 27/07/2014, 06:48:42 PM */ private HttpClient service; /** * token. * * @since 27/07/2014, 06:48:42 PM */ private String token; /** * Instantiates a new android push notification service impl. * * @param settings * the settings * @throws PushNotificationException * the push notification exception * @since 27/07/2014, 06:48:42 PM */ public AndroidPushNotificationServiceImpl(AndroidPushNotificationConfiguration settings) throws PushNotificationException { service = (HttpClient) settings.createService(); } /** * Send notifications. * * @param notifications * the notifications * @throws PushNotificationException * the push notification exception * @since 27/07/2014, 06:48:42 PM */ public void sendNotifications(Collection<Notification> notifications) throws PushNotificationException { for (Notification notification : notifications) { notification.setGeneratedToken(token); doSendNotification(notification); } } /** * Do send notification. * * @param notification * the notification * @throws PushNotificationException * the push notification exception * @since 27/07/2014, 06:48:42 PM */ protected void doSendNotification(Notification notification) throws PushNotificationException { try { PostMethod method = new PostMethod("https://android.apis.google.com/c2dm/send"); method.addParameter("registration_id", notification.getDeviceToken()); method.addParameter("collapse_key", "collapse"); method.addParameter("data.payload", String.valueOf(notification.getBadge())); Header header = new Header("Authorization", "GoogleLogin auth=" + notification.getGeneratedToken()); method.addRequestHeader(header); service.executeMethod(method); byte[] responseBody = method.getResponseBody(); String response = new String(responseBody); System.out.println(response); } catch (HttpException e) { e.printStackTrace(); throw new PushNotificationException("HTTP ", e); } catch (IOException e) { e.printStackTrace(); throw new PushNotificationException("IO ", e); } } }