我在我的Android应用程序中使用GCM服务。
在Google API控制台中注册,获得了projectID(senderID)和API密钥(浏览器密钥)。
使用以下代码创建新应用。
客户代码:
MainActivity
GCMRegistrar.checkDevice(this);
GCMRegistrar.checkManifest(this);
final String regId = GCMRegistrar.getRegistrationId(this);
if (regId.equals("")) {
GCMRegistrar.register(this, senderID);
} else {
Log.v(TAG, "Already registered");
}
GCMIntentService代码:
public class GCMIntentService extends GCMBaseIntentService {
private static final String TAG = "GCMIntentService";
public GCMIntentService() {
super(SENDER_ID);
}
@Override
protected void onRegistered(Context context, String registrationId) {
Log.i(TAG, "Device registered: regId = " + registrationId);
displayMessage(context, getString(R.string.gcm_registered));
}
@Override
protected void onUnregistered(Context context, String registrationId) {
Log.i(TAG, "Device unregistered");
displayMessage(context, getString(R.string.gcm_unregistered));
}
@Override
protected void onMessage(Context context, Intent intent) {
Log.i(TAG, "Received message");
String message = getString(R.string.gcm_message);
displayMessage(context, message);
generateNotification(context, message);
}
@Override
protected void onDeletedMessages(Context context, int total) {
Log.i(TAG, "Received deleted messages notification");
String message = getString(R.string.gcm_deleted, total);
displayMessage(context, message);
generateNotification(context, message);
}
@Override
public void onError(Context context, String errorId) {
Log.i(TAG, "Received error: " + errorId);
displayMessage(context, getString(R.string.gcm_error, errorId));
}
@Override
protected boolean onRecoverableError(Context context, String errorId) {
// log message
Log.i(TAG, "Received recoverable error: " + errorId);
displayMessage(context, getString(R.string.gcm_recoverable_error,
errorId));
return super.onRecoverableError(context, errorId);
}
static void displayMessage(Context context, String message) {
Intent intent = new Intent(DISPLAY_MESSAGE_ACTION);
intent.putExtra(EXTRA_MESSAGE, message);
context.sendBroadcast(intent);
}
static final String EXTRA_MESSAGE = "message";
static final String DISPLAY_MESSAGE_ACTION =
"com.google.android.gcm.demo.app.DISPLAY_MESSAGE";
private static void generateNotification(Context context, String message) {
int icon = R.drawable.ic_launcher;
long when = System.currentTimeMillis();
NotificationManager notificationManager = (NotificationManager)
context.getSystemService(Context.NOTIFICATION_SERVICE);
Notification notification = new Notification(icon, message, when);
String title = context.getString(R.string.app_name);
Intent notificationIntent = new Intent(context, MainActivity.class);
// set intent so it does not start a new activity
notificationIntent.setFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP |
Intent.FLAG_ACTIVITY_SINGLE_TOP);
PendingIntent intent =
PendingIntent.getActivity(context, 0, notificationIntent, 0);
notification.setLatestEventInfo(context, title, message, intent);
notification.flags |= Notification.FLAG_AUTO_CANCEL;
notificationManager.notify(0, notification);
}
清单文件:
<uses-sdk
android:minSdkVersion="8"
android:targetSdkVersion="16" />
<uses-permission android:name="android.permission.INTERNET"/>
<permission android:name="com.example.samplegcm.permission.C2D_MESSAGE"
android:protectionLevel="signature" />
<uses-permission android:name="com.example.samplegcm.permission.C2D_MESSAGE" />
<!-- App receives GCM messages. -->
<uses-permission android:name="com.google.android.c2dm.permission.RECEIVE" />
<!-- GCM requires a Google account. -->
<uses-permission android:name="android.permission.GET_ACCOUNTS" />
<!-- Keeps the processor from sleeping when a message is received. -->
<uses-permission android:name="android.permission.WAKE_LOCK" />
<application
android:icon="@drawable/ic_launcher"
android:label="@string/app_name"
android:theme="@style/AppTheme" >
<activity
android:name=".MainActivity"
android:label="@string/title_activity_main" >
<intent-filter>
<action android:name="android.intent.action.MAIN" />
<category android:name="android.intent.category.LAUNCHER" />
</intent-filter>
</activity>
<receiver android:name="com.google.android.gcm.GCMBroadcastReceiver"
android:permission="com.google.android.c2dm.permission.SEND" >
<intent-filter>
<action android:name="com.google.android.c2dm.intent.RECEIVE" />
<action android:name="com.google.android.c2dm.intent.REGISTRATION" />
<category android:name="com.example.samplegcm" />
</intent-filter>
</receiver>
<service android:name=".GCMIntentService" />
</application>
3.将gcm.jar添加到libs文件夹中。执行并成功获得registerId。
4.为服务器端修复了另一个新的应用程序。
5.将gcm-server.jar文件添加到libs文件夹。
服务器端代码:
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_gcmserver);
regID = (EditText)findViewById(R.id.editText1);
data1 =(EditText)findViewById(R.id.editText3);
data2 =(EditText)findViewById(R.id.editText4);
btn1 = (Button)findViewById(R.id.button1);
btn1.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
sendmessage2Device(v, regID.toString(), data1.getText().toString(), data2.getText().toString());
}
});
}
@Override
public boolean onCreateOptionsMenu(Menu menu) {
getMenuInflater().inflate(R.menu.activity_gcmserver, menu);
return true;
}
public void sendmessage2Device(View v,String regisID,String msg1,String msg2) {
HttpClient client = new DefaultHttpClient();
HttpPost post = new HttpPost(
"https://android.googleapis.com/gcm/send");
try {
List<NameValuePair> nameValuePairs = new ArrayList<NameValuePair>(1);
nameValuePairs.add(new BasicNameValuePair("registration_id",regisID));
nameValuePairs.add(new BasicNameValuePair("data1",msg1));
nameValuePairs.add(new BasicNameValuePair("data2", msg2));
post.setHeader("Authorization","key=AIzaxxxxxxxxxxxxxxxxxxxxxxx134");
post.setHeader("Content-Type", "application/x-www-form-urlencoded;charset=UTF-8");
post.setEntity(new UrlEncodedFormEntity(nameValuePairs));
HttpResponse response = client.execute(post);
InputStreamReader inputst = new InputStreamReader(response.getEntity().getContent());
BufferedReader rd = new BufferedReader(inputst);
String line = "";
while ((line = rd.readLine()) != null) {
Log.e("HttpResponse", line);
String s = line.substring(0);
Log.i("GCM response",s);
}
} catch (IOException e) {
e.printStackTrace();
}
这些是我使用过的GCM的步骤和代码。问题是我正在执行服务器端代码。我没有从服务器收到通知并获得logcat(服务器端),如下所示:
12-10 15:09:40.371: I/global(945): Default buffer size used in BufferedReader constructor. It would be better to be explicit if an 8k-char buffer is required.
12-10 15:09:40.381: E/HttpResponse(945): id=0:1355132381549743%77148e76f9fd7
我不知道这里有什么问题。我在客户端或服务器端做错了吗?