Ajouve
Ajouve

Reputation: 10089

read gcm message

I have a gcm class to use the gcm service:

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

}

But I don't understand how to run my class to get notification in any activity

Because in my case when I send a message with my server OnMessage does nothing

Thanks

Upvotes: 0

Views: 268

Answers (1)

Benoit
Benoit

Reputation: 4599

Did you write the following code in your main activity or in the 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");
}

Source: http://developer.android.com/google/gcm/gs.html

Don't forget to check the permission too.

Upvotes: 1

Related Questions