2013-04-08 159 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