Push notification OK + test PN

This commit is contained in:
Sylvain Berfini 2012-09-07 16:30:23 +02:00
parent df071cf987
commit 8d6d776a01
5 changed files with 107 additions and 4 deletions

View file

@ -2,7 +2,7 @@
<resources> <resources>
<!-- Push notification settings --> <!-- Push notification settings -->
<bool name="enable_push_id">true</bool> <bool name="enable_push_id">true</bool>
<string name="push_reg_id_key">175894229314</string> <string name="push_reg_id_key">push_reg_id_key</string>
<string name="push_sender_id">175894229314</string> <string name="push_sender_id">175894229314</string>
<string name="default_domain">sip.linphone.org</string> <string name="default_domain">sip.linphone.org</string>

View file

@ -45,7 +45,7 @@ public class GCMIntentService extends GCMBaseIntentService {
@Override @Override
protected void onMessage(Context context, Intent intent) { protected void onMessage(Context context, Intent intent) {
Log.d("Push notification received");
} }
@Override @Override

View file

@ -25,9 +25,11 @@ import org.linphone.mediastream.Version;
import android.app.Activity; import android.app.Activity;
import android.content.Intent; import android.content.Intent;
import android.content.SharedPreferences;
import android.content.pm.ActivityInfo; import android.content.pm.ActivityInfo;
import android.os.Bundle; import android.os.Bundle;
import android.os.Handler; import android.os.Handler;
import android.preference.PreferenceManager;
import com.google.android.gcm.GCMRegistrar; import com.google.android.gcm.GCMRegistrar;
@ -65,7 +67,11 @@ public class LinphoneLauncherActivity extends Activity {
if (regId.equals("")) { if (regId.equals("")) {
GCMRegistrar.register(this, getString(R.string.push_sender_id)); GCMRegistrar.register(this, getString(R.string.push_sender_id));
} else { } 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();
} }
} }

View file

@ -621,9 +621,11 @@ public final class LinphoneManager implements LinphoneCoreListener {
LinphoneProxyConfig proxycon = LinphoneCoreFactory.instance().createProxyConfig(identity, proxy, null, true); 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 regId = getPrefString(R.string.push_reg_id_key, null);
String appId = getString(R.string.push_sender_id);
if (regId != null) { 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); proxycon.setContactParameters(contactInfos);
} }
mLc.addProxyConfig(proxycon); mLc.addProxyConfig(proxycon);

View file

@ -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<LinphoneActivity> {
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<NameValuePair> nameValuePairs = new ArrayList<NameValuePair>(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();
}
}