且构网

分享程序员开发的那些事...
且构网 - 分享程序员编程开发的那些事

使用GCM,城市飞艇发送推送通知

更新时间:2023-02-16 22:24:09

据了说明:的 http://developer.android.com/guide/google/gcm/gs.html
 编写Android应用程序
您必须下载并添加GCM库。
然后,使舱单更改:

It is explained here: http://developer.android.com/guide/google/gcm/gs.html Writing the Android Application You have to download and add gcm libraries. Then make changes in manifest:

<uses-sdk android:minSdkVersion="8" android:targetSdkVersion="xx"/>

<permission android:name="my_app_package.permission.C2D_MESSAGE" android:protectionLevel="signature" />
<uses-permission android:name="my_app_package.permission.C2D_MESSAGE" /> 

在my_app_package插入你的包名。
添加其他权限。

inserting Your package name in "my_app_package". Add other permissions

<!-- App receives GCM messages. -->
<uses-permission android:name="com.google.android.c2dm.permission.RECEIVE" />
<!-- GCM connects to Google Services. -->
<uses-permission android:name="android.permission.INTERNET" /> 
<!-- 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" />

接收

<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="my_app_package" />
</intent-filter>
</receiver>

写一遍你的包名my_app_package。
然后,声明你的服务

Again write Your package name in "my_app_package". Then declare your service

<service android:name=".GCMIntentService" />

您必须创建一个名为GCMIntentService类。这个类将要处理的推送通知。
它应该是这样的:

You have to create a class named GCMIntentService. This class is going to handle the push notifications. It should look like this:

class GCMIntentService extends  com.google.android.gcm.GCMBaseIntentService{
     public void onRegistered(Context context, String regId){}
     public void onUnregistered(Context context, String regId){}
     public void onError(Context context, String errorId){}
     public void onMessage(Context context, Intent intent){}
}

在的onMessage(),你将不得不处理推送消息。您可以使用通知。

In onMessage() you will have to handle the push message. You can use notifications.

然后在你出发的活动,你将不得不把这个

Then in Your starting Activity you will have to put this

GCMRegistrar.checkDevice(this);
GCMRegistrar.checkManifest(this);
final String regId = GCMRegistrar.getRegistrationId(this);
if (regId.equals("")) {
  GCMRegistrar.register(this, SENDER_ID);
} else {
  Log.v(TAG, "Already registered");
}

在的onCreate()。
SENDER_ID是包含您从谷歌得到了控制台所有的数字字符串。

in onCreate(). SENDER_ID is the String containing all numbers that you got from the google console.