现在先来完成GCM的客户端,首先创建android项目,名字就是之前在Google Console中注册的那个 DemoProject
创建完成之后,用Android SDK Manager确认你的SDK中已经下载了extras中的Google Cloud Messaging for Android Library已经安装,如果没有,请下载
之后在项目中导入gcm.jar,位置在:..\adt-bundle-windows-x86_64-20131030\sdk\extras\google\gcm\gcm-client\dist
现在开始可以着手写Service了
(1) 在Manifest文件夹定义的package中创建GCMIntentService (一定要是这个名字!!),详细代码如下
package com.example.pushproject;
import com.google.android.gcm.GCMBaseIntentService;
import android.R.anim;
import android.app.Notification;
import android.app.NotificationManager;
import android.app.PendingIntent;
import android.app.Service;
import android.content.Context;
import android.content.Intent;
import android.graphics.Color;
import android.os.Handler;
import android.os.IBinder;
import android.os.Looper;
import android.util.Log;
import android.widget.Toast;
public class GCMIntentService extends GCMBaseIntentService {
public static final String SENDERID="";
public GCMIntentService() {
super(SENDERID);
}
@Override
protected void onError(Context arg0, String arg1) {
Log.v("GCMIntentService", "onError");
}
@Override
protected void onMessage(Context arg0, Intent arg1) {
String titleString=arg1.getStringExtra("title");
String contentString=arg1.getStringExtra("content");
showNotification(titleString,contentString);
}
@Override
protected void onRegistered(Context arg0, String arg1) {
Log.v("GCMIntentService", "onRegistered success ");
}
@Override
protected void onUnregistered(Context arg0, String arg1) {
Log.v("GCMIntentService", "onUnregistered");
}
public void showNotification(String title,String content){
NotificationManager manager=(NotificationManager)this.getSystemService(android.content.Context.NOTIFICATION_SERVICE);
Notification notification=new Notification(R.drawable.ic_launcher,title,System.currentTimeMillis());
notification.flags |= Notification.FLAG_ONGOING_EVENT; // 将此通知放到通知栏的"Ongoing"即"正在运行"组中
notification.flags |= Notification.FLAG_SHOW_LIGHTS;
notification.defaults = Notification.DEFAULT_LIGHTS;
notification.ledARGB = Color.BLUE;
notification.ledOnMS =5000; //闪光时间,毫秒
// 设置通知的事件消息
CharSequence contentTitle =title; // 通知栏标题
CharSequence contentText =content; // 通知栏内容
Intent notificationIntent =new Intent(); // 点击该通知后要跳转的Activity
PendingIntent contentItent = PendingIntent.getActivity(this, 0, notificationIntent, 0);
notification.setLatestEventInfo(this, contentTitle, contentText, contentItent);
// 把Notification传递给NotificationManager
manager.notify(0, notification);
}
}
其中SENDERID就是之前在GOOGLE console中申请的Project Number, 按照我们之前的例子,它应该是1023569414169
(2) 修改MainActivity
在MainActivity中,我们将注册好的Registration ID发送到服务器端存储起来,注意不要在主线程中使用HttpClient发送消息,应该写一个新的AsyncTask发送
另外,registration ID的注册可能存在一点点延迟现象,所以在这里使用定时器,确认返回ID后再发送,代码如下:
package com.example.pushproject;
import java.util.Timer;
import java.util.TimerTask;
import org.apache.http.HttpResponse;
import org.apache.http.client.HttpClient;
import org.apache.http.client.methods.HttpGet;
import org.apache.http.impl.client.DefaultHttpClient;
import android.R.integer;
import android.app.Activity;
import android.os.AsyncTask;
import android.os.Bundle;
import android.view.Menu;
import android.view.View;
import android.widget.Button;
import com.google.android.gcm.GCMRegistrar;
public class MainActivity extends Activity {
private String regIdString;
private Button unButton;
public static String accessUrl="http://请使用详细地址,使用127.0.0.1是发送不出去的:8080";
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
// Register
GCMRegistrar.checkDevice(this);
GCMRegistrar.checkManifest(this);
regIdString = GCMRegistrar.getRegistrationId(this);
if (regIdString.equals("")) {
GCMRegistrar.register(this, GCMIntentService.SENDERID);
// when register device to GCM, it got time delay. So we use Timer send registerID to our app server
Timer timer=new Timer();
timer.schedule(new GetRegistrationID(), 5000);
}
// Test button, if click, you can remove registered and device can not
// get notification any more
unButton = (Button) findViewById(R.id.unreg);
unButton.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
GCMRegistrar.unregister(getBaseContext());
}
});
}
@Override
public boolean onCreateOptionsMenu(Menu menu) {
getMenuInflater().inflate(R.menu.main, menu);
return true;
}
class UpdateRegistrationDTask extends AsyncTask {
@Override
protected Object doInBackground(Object... arg0) {
regIdString=arg0[0].toString();
String urlString = accessUrl+"/DemoProject/GetDeviceId.do"
+ "?deviceId=" + regIdString;
HttpGet httpGet = new HttpGet(urlString);
HttpClient httpClient = new DefaultHttpClient();
try {
HttpResponse response = httpClient.execute(httpGet);
} catch (Exception e) {
e.printStackTrace();
}
return null;
}
}
class GetRegistrationID extends TimerTask{
@Override
public void run() {
String testString=GCMRegistrar.getRegistrationId(getApplicationContext());
new UpdateRegistrationDTask().execute(testString);
}
}
}
(3)修改Manifest.xml文件,添加许可,我的XML文件如下,仅供大家参考
<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
package="com.example.demoprojecgt"
android:versionCode="1"
android:versionName="1.0" >
<uses-sdk
android:minSdkVersion="8"
android:targetSdkVersion="18" />
<permission
android:name="com.example.demoproject.permission.C2D_MESSAGE"
android:protectionLevel="signature"/>
<uses-permission android:name="com.example.demoproject.permission.C2D_MESSAGE"/>
<uses-permission android:name="com.google.android.c2dm.permission.RECEIVE"/>
<uses-permission android:name="android.permission.INTERNET"/>
<uses-permission android:name="android.permission.GET_ACCOUNTS"/>
<uses-permission android:name="android.permission.WAKE_LOCK"/>
<application
android:allowBackup="true"
android:icon="@drawable/ic_launcher"
android:label="@string/app_name"
android:theme="@style/AppTheme" >
<activity
android:name="com.example.pushproject.MainActivity"
android:label="@string/app_name" >
<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.demoproject"/>
</intent-filter>
</receiver>
<service
android:name=".GCMIntentService">
</service>
</application>
</manifest>
下面是一些permission的说明
1. com.google.android.c2dm.permission.RECEIVE
使得Android app可以向GCM注册,且获得信息
2. android.permission.INTERNET
使得Android app可以向第三方服务器发送registration ID
3. android.permission.GET_ACCOUNTS
当Android系统低于4.0.4的时候,使得GCM获得用户的Google账户
4. android.permission.WAKE_LOCK
这是可选项,如果你希望你的app在sleeping状态下也能接收信息的话,请添加该permission
5. applicationPackage + ".permission.C2D_MESSAGE"
该permission防止别的app注册并获得推送消息。该permission必须严格遵守该规范,否则你的app将不会接收任何推送消息
6. 定义了com.google.android.c2dm.intent.RECEIVE
的receiver。 该receiver同时要定义com.google.android.c2dm.SEND
许可,这样就只有GCM框架可以给他发送消息。如果你的app使用IntentService,那么这个receiver必须是WakefulBroadcastReceiver的实例。
7. 最后确定你的app最低版本在8以上,如果想要使用GCM服务的话
注意,在permission和第一个uses-permission中,将其中的包名改为你们自己的,同时需要修改的还有receiver中的category
至此,Client部分结束