2013-04-08 121 views
0

我有一个GCM类使用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

你写下面的代码在您的主要活动或在应用程序的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

不要忘记检查的权限了。

+0

是的我在主要活动和活动上使用它我试图让我的gcm – Ajouve 2013-04-08 20:26:14

+0

感谢您的帮助,现在它的作品:) – Ajouve 2013-04-08 20:51:02