1 /*******************************************************************************
2 * Copyright © Igor Barma, Alexandre Desoubeaux, Christian Martel, Eric Brun, Mathieu Amblard, Gwenael Gevet, Pierre Guillot, 2012
4 * This file is part of the work and learning management system Pentila Nero.
6 * Pentila Nero is free software. You can redistribute it and/or modify since
7 * you respect the terms of either (at least one of the both license) :
8 * - under the terms of the GNU Affero General Public License as
9 * published by the Free Software Foundation, either version 3 of the
10 * License, or (at your option) any later version.
11 * - the CeCILL-C as published by CeCILL-C; either version 1 of the
12 * License, or any later version
13 * - the GNU Lesser General Public License as published by the
14 * Free Software Foundation, either version 3 of the license,
15 * or (at your option) any later version.
17 * There are special exceptions to the terms and conditions of the
18 * licenses as they are applied to this software. View the full text of
19 * the exception in file LICENSE-PROJECT.txt in the directory of this software
22 * Pentila Nero is distributed in the hope that it will be useful,
23 * but WITHOUT ANY WARRANTY; without even the implied warranty of
24 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
25 * Licenses for more details.
27 * You should have received a copy of the GNU Affero General Public License
28 * and the CeCILL-C and the GNU Lesser General Public License along with
29 * Pentila Nero. If not, see :
30 * <http://www.gnu.org/licenses/> and
31 * <http://www.cecill.info/licences.fr.html>.
32 ******************************************************************************/
33 package com.pentila.entSavoie.mobileNotification.androidUtils;
35 import java.io.UnsupportedEncodingException;
36 import java.util.ArrayList;
37 import java.util.List;
39 import org.apache.http.Header;
40 import org.apache.http.HttpEntity;
41 import org.apache.http.HttpResponse;
42 import org.apache.http.NameValuePair;
43 import org.apache.http.client.entity.UrlEncodedFormEntity;
44 import org.apache.http.client.methods.HttpPost;
45 import org.apache.http.impl.client.DefaultHttpClient;
46 import org.apache.http.message.BasicHeader;
47 import org.apache.http.message.BasicNameValuePair;
48 import org.apache.http.protocol.BasicHttpContext;
49 import org.apache.http.protocol.HttpContext;
53 public class AndroidPushDispatcher implements PushDispatcher {
56 public void sendNotification(AndroidMessage message) {
58 System.out.println("In AndroidPushDispatcher:sendMessage with message "+ message.getMessage());
60 DefaultHttpClient client = new DefaultHttpClient();
61 HttpPost httppost = new HttpPost("https://android.apis.google.com/c2dm/send");
62 List<NameValuePair> formparams = new ArrayList<NameValuePair>();
63 formparams.add(new BasicNameValuePair("registration_id",
64 message.getRegistrationKey()));
65 System.out.println("message registration_ID :: "+ message.getRegistrationKey());
66 formparams.add(new BasicNameValuePair("data.payload", message.getMessage()));
67 formparams.add(new BasicNameValuePair("collapse_key", "0"));
69 if (null != message.getRegistrationKey()) {
70 formparams.add(new BasicNameValuePair("id", message
71 .getRegistrationKey()));
73 System.out.println("Registration Key is null ");
75 UrlEncodedFormEntity entity = null;
77 entity = new UrlEncodedFormEntity(formparams, "UTF-8");
78 } catch (UnsupportedEncodingException e1) {
79 System.out.println("Exception occured in AndroidPushDispatcher:sendMessage "+e1);
81 httppost.setEntity(entity);
82 Header header = new BasicHeader("Authorization",
83 "GoogleLogin auth=" + message.getAuthKey());
84 System.out.println("message.getAuthKey : "+ message.getAuthKey());
85 Header header2 = new BasicHeader("Content-length=", ""
86 + message.getMessage().length());
87 httppost.addHeader(header);
88 httppost.addHeader(header2);
89 httppost.getParams().setParameter("Content-length:",
90 "" + message.getMessage().length());
91 HttpContext localContext = new BasicHttpContext();
92 HttpResponse response = null;
94 response = client.execute(httppost, localContext);
96 HttpEntity entity2 = response.getEntity();
97 System.out.println("Response from post call is "+ entity2.getContent().toString());
99 } catch (Exception e) {
100 System.out.println("Error occured while posting the message "+ e);