Search code examples
androidbroadcastreceivergoogle-cloud-messagingandroid-broadcast

GCM error on app start


This is my RegisterGCM class

 public class RegisterGCM {

private static final String PROJECT_ID = "589104089214";
// This tag is used in Log.x() calls
private static final String TAG = "otp_verification.";

// This string will hold the lengthy registration id that comes
// from GCMRegistrar.register()
private String regId = "";

// These strings are hopefully self-explanatory
private String registrationStatus = "Not yet registered";
private String broadcastMessage = "No broadcast message";

// This intent filter will be set to filter on the string
// "GCM_RECEIVED_ACTION"
IntentFilter gcmFilter;
Context ctx;

// textviews used to show the status of our app's registration, and the
// latest
// broadcast message.

// This broadcastreceiver instance will receive messages broadcast
// with the action "GCM_RECEIVED_ACTION" via the gcmFilter

public String getRegId() {
    return regId;
}

public void setRegId(String regId) {
    this.regId = regId;
}

public String getRegistrationStatus() {
    return registrationStatus;
}

public void setRegistrationStatus(String registrationStatus) {
    this.registrationStatus = registrationStatus;
}

public String getBroadcastMessage() {
    return broadcastMessage;
}

public void setBroadcastMessage(String broadcastMessage) {
    this.broadcastMessage = broadcastMessage;
}

public IntentFilter getGcmFilter() {
    return gcmFilter;
}

public void setGcmFilter(IntentFilter gcmFilter) {
    this.gcmFilter = gcmFilter;
}

public Context getCtx() {
    return ctx;
}

public void setCtx(Context ctx) {
    this.ctx = ctx;
}

public BroadcastReceiver getGcmReceiver() {
    return gcmReceiver;
}

public void setGcmReceiver(BroadcastReceiver gcmReceiver) {
    this.gcmReceiver = gcmReceiver;
}

public static String getProjectId() {
    return PROJECT_ID;
}

public static String getTag() {
    return TAG;
}

// A BroadcastReceiver must override the onReceive() event.
private BroadcastReceiver gcmReceiver = new BroadcastReceiver() {

    @Override
    public void onReceive(Context context, Intent intent) {
        if (broadcastMessage != null) {
            // display our received message

        }
    }
};

public RegisterGCM(Context ctx) {
    super();
    this.ctx = ctx;
}

public void registerClient() {
    // TODO Auto-generated method stub

    try {
        // Check that the device supports GCM (should be in a try / catch)
        GCMRegistrar.checkDevice(ctx);

        // Check the manifest to be sure this app has all the required
        // permissions.
        GCMRegistrar.checkManifest(ctx);

        // Get the existing registration id, if it exists.
        regId = GCMRegistrar.getRegistrationId(ctx);

        if (regId.equals("")) {

            registrationStatus = "Registering...";

            // register this device for this project
            GCMRegistrar.register(ctx, PROJECT_ID);
            regId = GCMRegistrar.getRegistrationId(ctx);

            registrationStatus = "Registration Acquired";

            // This is actually a dummy function. At this point, one
            // would send the registration id, and other identifying
            // information to your server, which should save the id
            // for use when broadcasting messages.
            sendRegistrationToServer();

        } else {

            registrationStatus = "Already registered";

        }

    } catch (Exception e) {

        e.printStackTrace();
        registrationStatus = e.getMessage();

    }

    Log.d(TAG, registrationStatus);
    // tvRegStatusResult.setText(registrationStatus);

    // This is part of our CHEAT. For this demo, you'll need to
    // capture this registration id so it can be used in our demo web
    // service.
    Log.d(TAG, regId);

}

private void sendRegistrationToServer() {
    // TODO Auto-generated method stub

}

// NOTE the call to GCMRegistrar.onDestroy()

public void onDestroy() {

    GCMRegistrar.onDestroy(ctx);

}

public void stop(Context ctx) {
    GCMRegistrar.unregister(ctx);
}

}

Here's what my LogCat shows when i launch the application

06-18 12:15:03.338: V/GCMBroadcastReceiver(24212): onReceive: com.google.android.c2dm.intent.RECEIVE
06-18 12:15:03.338: V/GCMBroadcastReceiver(24212): GCM IntentService class: com.datavsn.QuickTransfer.GCMIntentService
06-18 12:15:03.338: V/GCMBaseIntentService(24212): Acquiring wakelock
06-18 12:15:03.348: E/BroadcastReceiver(24212): BroadcastReceiver trying to return result during a non-ordered broadcast
06-18 12:15:03.348: E/BroadcastReceiver(24212): java.lang.RuntimeException: BroadcastReceiver trying to return result during a non-ordered broadcast
06-18 12:15:03.348: E/BroadcastReceiver(24212):     at android.content.BroadcastReceiver.checkSynchronousHint(BroadcastReceiver.java:783)
06-18 12:15:03.348: E/BroadcastReceiver(24212):     at android.content.BroadcastReceiver.setResult(BroadcastReceiver.java:658)
06-18 12:15:03.348: E/BroadcastReceiver(24212):     at com.google.android.gcm.GCMBroadcastReceiver.onReceive(GCMBroadcastReceiver.java:47)
06-18 12:15:03.348: E/BroadcastReceiver(24212):     at android.app.ActivityThread.handleReceiver(ActivityThread.java:2667)
06-18 12:15:03.348: E/BroadcastReceiver(24212):     at android.app.ActivityThread.access$1700(ActivityThread.java:166)
06-18 12:15:03.348: E/BroadcastReceiver(24212):     at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1359)
06-18 12:15:03.348: E/BroadcastReceiver(24212):     at android.os.Handler.dispatchMessage(Handler.java:102)
06-18 12:15:03.348: E/BroadcastReceiver(24212):     at android.os.Looper.loop(Looper.java:136)
06-18 12:15:03.348: E/BroadcastReceiver(24212):     at android.app.ActivityThread.main(ActivityThread.java:5584)
06-18 12:15:03.348: E/BroadcastReceiver(24212):     at java.lang.reflect.Method.invokeNative(Native Method)
06-18 12:15:03.348: E/BroadcastReceiver(24212):     at java.lang.reflect.Method.invoke(Method.java:515)
06-18 12:15:03.348: E/BroadcastReceiver(24212):     at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:1268)
06-18 12:15:03.348: E/BroadcastReceiver(24212):     at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:1084)
06-18 12:15:03.348: E/BroadcastReceiver(24212):     at dalvik.system.NativeStart.main(Native Method)
06-18 12:15:03.348: D/GCMIntentService(24212): GCMIntentService init
06-18 12:15:03.358: D/GCMIntentService(24212): Message Received
06-18 12:15:03.358: V/GCMBaseIntentService(24212): Releasing wakelock

I have just started my app it does not have a gcm id so how can it recieve a message as in my logcat the last 2 line suggest and also my code for getting id is when i move to my login page. The app crashes the first time i start it and then when i start it again it runs smoothly without the above exception.


Solution

  • This looks like a known problem with a very old version of the GCM library. I would recommend updating to the latest version, as detailed here: https://developers.google.com/cloud-messaging/android/client