读取gcm消息

时间:2013-04-08 19:44:55

标签: android google-cloud-messaging

我有一个gcm类来使用gcm服务:

public class GCMIntentService extends GCMBaseIntentService {

private static final String TAG = "GCMIntentService";

public GCMIntentService() {
    super("726233487705");
}

@Override
protected void onRegistered(Context context, String registrationId) {
    Log.i(TAG, "Device registered: regId = " + registrationId);
}

@Override
protected void onUnregistered(Context context, String registrationId) {
    Log.i(TAG, "Device unregistered");
}

@Override
protected void onDeletedMessages(Context context, int total) {
    Log.i(TAG, "Received deleted messages notification");
}

@Override
public void onError(Context context, String errorId) {
    Log.i(TAG, "Received error: " + errorId);
}

@Override
protected boolean onRecoverableError(Context context, String errorId) {
    // log message
    Log.i(TAG, "Received recoverable error: " + errorId);
    return super.onRecoverableError(context, errorId);
}

/**
 * Issues a notification to inform the user that server has sent a message.
 */
private static void generateNotification(Context context, String message) {
    System.out.println(message);
}

@Override
protected void onMessage(Context context, Intent intent) {
    String msg = intent.getExtras().getString("id");
    System.out.println(msg);
}

}

但我不明白如何运行我的课程来获取任何活动的通知

因为在我的情况下,当我用我的服务器OnMessage发送消息时什么都不做

谢谢

1 个答案:

答案 0 :(得分:1)

您是否在主要活动或Application onCreate中编写了以下代码?:

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");
}

来源:http://developer.android.com/google/gcm/gs.html

不要忘记检查权限。