注册

gcm推送

一.gcm前期准备


20150727113000901_(1).jpg


 
Apple有apns推送,Google有gcm推送,iOS接收通知调用系统通知栏提示,Android接收通知启动应用调用通知栏提示。
相对于apns,gcm则多了一些限制,需要一些必备条件达到才可以使用。
1.在国内,首先就是Google被墙,无法连接到Google服务器,需要你走VPN或者其它方式可以连接到Google服务器。
2.在你的开发环境下,需要通过Android SDK Manager—>Extras下安装Google Play services,成功之后在你的SDK文件/sdk/extras/google/google_play_services/libproject下会看到google-play-services_lib类库,可以从里面直接复制jar包到你的项目libs下
3.Android客户端,要求安装了Google核心服务Google Play服务,Google Play 商店才能使用gcm


20150727113107623.jpg


以上是Google官方文档描述:
大意就是gcm要求设备运行在Android系统2.2或更高版本并且安装了Google play商店应用,或者虚拟机运行在Android系统2.2版本的并且支持Google API,但是并不限制你的应用必须部署在Google play 商店。
然而,如果你想要使用gcm 新的API就需要设备运行在Android系统2.3或更高版本,或者使用虚拟机运行在Android系统2.3并且支持Google API
在一个现有连接Google服务的设备上,对于前置3.0的设备,就要求在Android设备上设置Google账号,4.0.4或者更高版本则不需要设置Google账号
注:gcm只是简单的推送一个通知到客户端,其消息内容应小于4Kb,Google服务器存储时间为4个星期。在客户端,gcm是通过客户端设备注册一个系统广播来唤醒应用并发出通知提示,所以这个时候客户端不需要一直运行来接收消息,gcm一次发送最多可发送100个用户
 
二.官网创建项目获取信息

去Google Developers Console(https://console.developers.google.com/project)创建项目,输入你的project name和project id,创建成功如下图:


6.jpg


在应用中我们会用到这个project number,再去Credentials下创建一个server key,得到的api key会在自己的服务器端用到,如图

7.jpg


顺便说一下,默认Cloud Messaging for Android是可用的,如果不能用,就需要去API & auth—> API下查看Cloud Messaging for Android是否disable

三.创建一个客户端app

首先要在AndroidManifest.xml文件配置gcm权限等信息

//当GCM需要谷歌账户(设备版本低于4.0.4时需要)


android:protectionLevel="signature" />

applicationPackage要改成自己的报名

android:minSdkVersion="8"不得低于8


8.jpg


根据官网文档介绍:

1.我们应该在配置文件注册一个系统广播
           android:name="com.google.android.gms.gcm.GcmReceiver"  
android:exported="true"
android:permission="com.google.android.c2dm.permission.SEND" >





2.注册监听通知服务(在app中创建一个服务类并继承GcmListenerService类)
            android:name="com.example.MyGcmListenerService"  
android:exported="false" >





public class MyGcmListenerService extends GcmListenerService {

@Override
public void onMessageReceived(String from, Bundle data) {
String message = data.getString("message");
/**
* Production applications would usually process the message here.
* Eg: - Syncing with server.
* - Store message in local database.
* - Update UI.
*/

/**
* In some cases it may be useful to show a notification indicating to the user
* that a message was received.
*/
sendNotification(message);
}
// [END receive_message]

/**
* Create and show a simple notification containing the received GCM message.
*
* @param message GCM message received.
*/
private void sendNotification(String message) {
Intent intent = new Intent(this, MainActivity.class);
intent.addFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP);
PendingIntent pendingIntent = PendingIntent.getActivity(this, 0 /* Request code */, intent,
PendingIntent.FLAG_ONE_SHOT);

Uri defaultSoundUri= RingtoneManager.getDefaultUri(RingtoneManager.TYPE_NOTIFICATION);
NotificationCompat.Builder notificationBuilder = new NotificationCompat.Builder(this)
.setSmallIcon(R.drawable.ic_stat_ic_notification)
.setContentTitle("this is a new new GCM Message")
.setContentText(message)
.setAutoCancel(true)
.setSound(defaultSoundUri)
.setContentIntent(pendingIntent);

NotificationManager notificationManager =
(NotificationManager) getSystemService(Context.NOTIFICATION_SERVICE);

notificationManager.notify(0 /* ID of notification */, notificationBuilder.build());
}
}

 
3.注册监听token改变的服务(在app中创建一个服务类继承InstanceIDListenerService类,本服务存在于新的Google play服务版本上)
 android:name="com.example.MyInstanceIDListenerService"  
android:exported="false">





public class MyInstanceIDListenerService extends InstanceIDListenerService {

@Override
public void onTokenRefresh() {
// Fetch updated Instance ID token and notify our app's server of any changes (if applicable).
}
}

 
gcm通过注册的系统广播可以自行启动GcmListenerService类,所以不需要在app创建一个广播类

注册token

App发送 project number到GCM Server注册接收推送信息。 

GCM Server 向App返回token(token是GCM服务器自行生产的,能够保证某一终端设备上的某一个应用,很重要)。 

在设备上应该先判断是否安装了Google play 服务
int resultCode = GooglePlayServicesUtil.isGooglePlayServicesAvailable(this);  
if (resultCode != ConnectionResult.SUCCESS) {
if (GooglePlayServicesUtil.isUserRecoverableError(resultCode)) {
GooglePlayServicesUtil.getErrorDialog(resultCode, this, PLAY_SERVICES_RESOLUTION_REQUEST).show();//会提示错误对话框,其他错误信息可参考官网API
} else {
Log.i(TAG, "This device is not supported.”);
}

 
 
Google play服务存在,那接下来就需要去注册token了,在上边创建项目得到的project number在这里就需要用它来注册token


10.jpg




9.jpg


 
InstanceID instanceID = InstanceID.getInstance(this);  
String token = instanceID.getToken(PROJECT_NUMBER,
GoogleCloudMessaging.INSTANCE_ID_SCOPE, null);

 
对于旧版本还是要使用以下方式来注册token(用于Google play 服务7.5版本之前)
GoogleCloudMessaging.getInstance(Context).register(PROJECT_NUMBER);  

 
在自己的服务器端注册api key

App向我们自己服务器发送token(推送消息的时候要使用token,GCM服务器是使用token来确定某一终端设备上的某一个应用接收消息的,所以第三方服务器需要保存它,需要注意的是token很长,存数据库时需要注意字段长度) ,将我们在上边create new key 得到的apikey添加到自己的服务器端保存,我们的服务器向GCM Server发送消息,传递apikey和token ,GCM Server把消息推送给App 
最后注意一下,关于app卸载出现的问题,看一下官方的介绍


11.jpg



app会发生卸载过程并不能很快完成,卸载app需要花时间从GCM移除当前关联的token,这个过程就会出现发送消息是成功的,但是不能到达app客户端,在最后,token被移除了,服务器端发送的消息失败,得到一个NotRegistered错误,这个时候就应该在服务器端删除相对应的token

0 个评论

要回复文章请先登录注册