我想使用 GCM。我尝试在客户端进行以下操作:
显现:
<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android" package="com.example.test" android:installLocation="preferExternal" android:versionCode="1" android:versionName="1.0">
<uses-sdk android:minSdkVersion="8" android:targetSdkVersion="16" />
<uses-permission android:name="android.permission.INTERNET" />
<uses-permission android:name="android.permission.ACCESS_NETWORK_STATE" />
<uses-permission android:name="android.permission.ACCESS_WIFI_STATE" />
<uses-permission android:name="android.permission.VIBRATE" />
<uses-permission android:name="android.permission.GET_ACCOUNTS" />
<uses-permission android:name="android.permission.WAKE_LOCK" />
<uses-permission android:name="com.google.android.c2dm.permission.RECEIVE" />
<permission android:name="com.example.test.permission.C2D_MESSAGE" android:protectionLevel="signature" />
<uses-permission android:name="com.example.test.permission.C2D_MESSAGE" />
<application android:icon="@drawable/ic_launcher" android:label="@string/app_name" >
<activity android:screenOrientation="portrait"
android:name=".MainActivity"
android:label="@string/app_name"
android:theme="@android:style/Theme.NoTitleBar.Fullscreen" >
<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.test" />
</intent-filter>
</receiver>
<service android:name=".GCMIntentService" android:enabled="true" />
</application>
</manifest>
活动:
package com.example.test;
import com.google.android.gcm.GCMRegistrar;
import android.os.Bundle;
import android.app.Activity;
import android.app.PendingIntent;
import android.content.Intent;
import android.util.Log;
import android.view.Menu;
import android.view.WindowManager;
import android.widget.Toast;
public class MainActivity extends Activity {
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
gcmRelated();
}
private void gcmRelated() {
GCMRegistrar.checkDevice(this);
GCMRegistrar.checkManifest(this);
final String regId = GCMRegistrar.getRegistrationId(this);
Toast.makeText(this, regId, 10000).show();
if (regId.equals("")) {
GCMRegistrar.register(this, "609612932495");
} else {
Log.v("Register", "Already registered");
}
}
}
GCMIntentService.java:
package com.example.test;
import com.google.android.gcm.GCMBaseIntentService;
import com.google.android.gcm.GCMRegistrar;
import android.content.Context;
import android.content.Intent;
import android.os.PowerManager;
import java.util.logging.Logger;
public class GCMIntentService extends GCMBaseIntentService
{
private static PowerManager.WakeLock sWakeLock;
private static final Object LOCK = GCMIntentService.class;
private static final String GCM_SENDER_ID = "609612932495";
private static final String GCM_INTENT_FILTER = "com.example.test.GCM_MESSAGE";
private static final String MESSAGE_TYPE = "Type";
private static final String MESSAGE_CONTENT = "Body";
private static Logger log = Logger.getLogger(GCMIntentService.class.getName());
public GCMIntentService()
{
super(GCM_SENDER_ID);
}
static void runIntentInService(Context context,Intent intent)
{
synchronized(LOCK)
{
if (sWakeLock == null)
{
PowerManager pm = (PowerManager) context.getSystemService(Context.POWER_SERVICE);
sWakeLock = pm.newWakeLock(PowerManager.PARTIAL_WAKE_LOCK,"sc_wakelock");
}
}
sWakeLock.acquire();
intent.setClassName(context,GCMIntentService.class.getName());
context.startService(intent);
}
@Override protected void onRegistered(Context context,String registrationId)
{
log.warning("From GCMIntentService: Device successfully registered as "+registrationId);
}
@Override protected void onUnregistered(Context context,String registrationId)
{
log.warning("From GCMIntentService: Device successfully unregistered");
}
@Override protected void onMessage(Context context,Intent messageIntent)
{
log.warning("From GCMIntentService: Game update notice received");
}
@Override protected void onDeletedMessages(Context context,int total)
{
log.warning("From GCMIntentService: Server deleted "+Integer.toString(total)+" pending messages");
}
@Override public void onError(Context context,String errorId)
{
log.warning("From GCMIntentService: Error "+errorId);
}
@Override protected boolean onRecoverableError(Context context,String errorId)
{
log.warning("From GCMIntentService: Recoverable error "+errorId);
return(super.onRecoverableError(context,errorId));
}
}
它显示没有错误。GCMRegistrar.getRegistrationId(this)总是返回" "(empty string)。所以,它总是试图注册,我从来没有得到 registerId。
有什么要做的吗?
另外,在实现服务器端代码时,我是否需要发送此注册 ID 并将所有 ID 保存在数据库中并发送它们?