diff --git a/res/values/non_localizable_custom.xml b/res/values/non_localizable_custom.xml index 16a209e19..2d76ac910 100644 --- a/res/values/non_localizable_custom.xml +++ b/res/values/non_localizable_custom.xml @@ -2,7 +2,7 @@ true - 175894229314 + push_reg_id_key 175894229314 sip.linphone.org diff --git a/src/org/linphone/GCMIntentService.java b/src/org/linphone/GCMIntentService.java index 27f87206a..d0cac16bc 100644 --- a/src/org/linphone/GCMIntentService.java +++ b/src/org/linphone/GCMIntentService.java @@ -45,7 +45,7 @@ public class GCMIntentService extends GCMBaseIntentService { @Override protected void onMessage(Context context, Intent intent) { - + Log.d("Push notification received"); } @Override diff --git a/src/org/linphone/LinphoneLauncherActivity.java b/src/org/linphone/LinphoneLauncherActivity.java index 0cebee061..e0f478f95 100644 --- a/src/org/linphone/LinphoneLauncherActivity.java +++ b/src/org/linphone/LinphoneLauncherActivity.java @@ -25,9 +25,11 @@ import org.linphone.mediastream.Version; import android.app.Activity; import android.content.Intent; +import android.content.SharedPreferences; import android.content.pm.ActivityInfo; import android.os.Bundle; import android.os.Handler; +import android.preference.PreferenceManager; import com.google.android.gcm.GCMRegistrar; @@ -65,7 +67,11 @@ public class LinphoneLauncherActivity extends Activity { if (regId.equals("")) { GCMRegistrar.register(this, getString(R.string.push_sender_id)); } else { - Log.e("Already registered"); + Log.e("Already registered = " + regId); + SharedPreferences prefs = PreferenceManager.getDefaultSharedPreferences(this); + SharedPreferences.Editor editor = prefs.edit(); + editor.putString(this.getString(R.string.push_reg_id_key), regId); + editor.commit(); } } diff --git a/src/org/linphone/LinphoneManager.java b/src/org/linphone/LinphoneManager.java index 7b8a64e26..ddfebeb05 100644 --- a/src/org/linphone/LinphoneManager.java +++ b/src/org/linphone/LinphoneManager.java @@ -621,9 +621,11 @@ public final class LinphoneManager implements LinphoneCoreListener { LinphoneProxyConfig proxycon = LinphoneCoreFactory.instance().createProxyConfig(identity, proxy, null, true); + // Add parameters for push notifications String regId = getPrefString(R.string.push_reg_id_key, null); + String appId = getString(R.string.push_sender_id); if (regId != null) { - String contactInfos = "app-id=org.linphone.phone.dev;pn-type=android;pn-tok=" + regId + ";pn-msg-str=IM_MSG;pn-call-str=IC_MSG;pn-call-snd=ring.caf;pn-msg-snd=msg.caf;"; + String contactInfos = "app-id=" + appId + ";pn-type=google;pn-tok=" + regId + ";pn-msg-str=IM_MSG;pn-call-str=IC_MSG;pn-call-snd=ring.caf;pn-msg-snd=msg.caf;"; proxycon.setContactParameters(contactInfos); } mLc.addProxyConfig(proxycon); diff --git a/tests/src/org/linphone/test/PushNotificationTest.java b/tests/src/org/linphone/test/PushNotificationTest.java new file mode 100644 index 000000000..6e13d6d0b --- /dev/null +++ b/tests/src/org/linphone/test/PushNotificationTest.java @@ -0,0 +1,95 @@ +package org.linphone.test; + +import java.util.ArrayList; +import java.util.List; + +import junit.framework.Assert; + +import org.apache.http.HttpVersion; +import org.apache.http.NameValuePair; +import org.apache.http.client.HttpClient; +import org.apache.http.client.entity.UrlEncodedFormEntity; +import org.apache.http.client.methods.HttpPost; +import org.apache.http.conn.ClientConnectionManager; +import org.apache.http.conn.scheme.PlainSocketFactory; +import org.apache.http.conn.scheme.Scheme; +import org.apache.http.conn.scheme.SchemeRegistry; +import org.apache.http.conn.ssl.SSLSocketFactory; +import org.apache.http.impl.client.BasicResponseHandler; +import org.apache.http.impl.client.DefaultHttpClient; +import org.apache.http.impl.conn.tsccm.ThreadSafeClientConnManager; +import org.apache.http.message.BasicNameValuePair; +import org.apache.http.params.BasicHttpParams; +import org.apache.http.params.HttpParams; +import org.apache.http.params.HttpProtocolParams; +import org.apache.http.protocol.HTTP; +import org.linphone.LinphoneActivity; +import org.linphone.R; + +import android.content.SharedPreferences; +import android.preference.PreferenceManager; +import android.test.ActivityInstrumentationTestCase2; + +import com.jayway.android.robotium.solo.Solo; + +public class PushNotificationTest extends + ActivityInstrumentationTestCase2 { + + private Solo solo; + + @SuppressWarnings("deprecation") + public PushNotificationTest() { + super("org.linphone", LinphoneActivity.class); + } + + @Override + protected void setUp() throws Exception { + super.setUp(); + solo = new Solo(getInstrumentation(), getActivity()); + } + + private HttpClient createHttpClient() + { + HttpParams params = new BasicHttpParams(); + HttpProtocolParams.setVersion(params, HttpVersion.HTTP_1_1); + HttpProtocolParams.setContentCharset(params, HTTP.DEFAULT_CONTENT_CHARSET); + HttpProtocolParams.setUseExpectContinue(params, true); + + SchemeRegistry schReg = new SchemeRegistry(); + schReg.register(new Scheme("http", PlainSocketFactory.getSocketFactory(), 80)); + schReg.register(new Scheme("https", SSLSocketFactory.getSocketFactory(), 443)); + ClientConnectionManager conMgr = new ThreadSafeClientConnManager(params, schReg); + + return new DefaultHttpClient(conMgr, params); + } + + public void testIncomingPushNotification() { + solo.assertCurrentActivity("Expected Linphone Activity", LinphoneActivity.class); + + SharedPreferences prefs = PreferenceManager.getDefaultSharedPreferences(getActivity()); + String regId = prefs.getString(getActivity().getString(R.string.push_reg_id_key), null); + + // Send a push notification + HttpClient httpClient = createHttpClient(); + HttpPost httpPost = new HttpPost("https://android.googleapis.com/gcm/send"); + httpPost.setHeader("Content-type", "application/x-www-form-urlencoded;charset=UTF-8"); + httpPost.setHeader("Authorization", "key=AIzaSyBJAhCVeeqIErwTfYwy-t83_EwvZlCFo9I"); + + List nameValuePairs = new ArrayList(2); + nameValuePairs.add(new BasicNameValuePair("data.test", "TEST")); + nameValuePairs.add(new BasicNameValuePair("registration_id", regId)); + try { + httpPost.setEntity(new UrlEncodedFormEntity(nameValuePairs)); + httpClient.execute(httpPost, new BasicResponseHandler()); + } catch (Exception e) { + e.printStackTrace(); + } + + Assert.assertTrue(solo.waitForLogMessage("Push notification received", 3000)); + } + + @Override + public void tearDown() throws Exception { + solo.finishOpenedActivities(); + } +}