0

我有一个 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 什么都不做

谢谢

4

1 回答 1

1

您是在主要活动中还是在应用程序 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

不要忘记检查权限。

于 2013-04-08T19:58:05.850 回答