0

I'm trying to add GCM into my app. For now I just want to see the logs of receival of message.

My AndroidManifest.xml contains this:

    <!-- GCM Receiver -->
    <receiver
        android:name="com.x.android.gcm.MyBroadcastReceiver"
        android:permission="com.google.android.c2dm.permission.SEND" >
        <intent-filter>
            <!--<action android:name="com.google.android.c2dm.intent.REGISTRATION" />-->
            <action android:name="com.google.android.c2dm.intent.RECEIVE" />
            <category android:name="com.x.android" />
        </intent-filter>
    </receiver>
    <!-- GCM Listener -->
    <service
        android:name="com.x.android.gcm.MyGcmListenerService"
        android:exported="false" >
        <intent-filter>
            <action android:name="com.google.android.c2dm.intent.RECEIVE" />
        </intent-filter>
    </service>
    <!-- GCM InstanceID Listener -->
    <service
        android:name="com.x.android.gcm.MyInstanceIDListenerService"
        android:exported="false">
        <intent-filter>
            <action android:name="com.google.android.gms.iid.InstanceID"/>
        </intent-filter>
    </service>
    <!-- GCM Registration -->
    <service
        android:name="com.x.android.gcm.RegistrationIntentService"
        android:exported="false">
    </service>

In the MainActivity I have:

private BroadcastReceiver mReceiver;
private boolean isReceiverRegistered;
...
protected void onCreate(Bundle savedInstanceState) {
    ...
    mReceiver= new MyBroadcastReceiver();
    registerReceiver();

    if (checkPlayServices()) {
        Intent intent = new Intent(this, RegistrationIntentService.class);
        startService(intent);
    }
    ...
}

private void registerReceiver(){
    if(!isReceiverRegistered) {
        LocalBroadcastManager.getInstance(this).registerReceiver(mRegistrationBroadcastReceiver,
                new IntentFilter(Preferences.REGISTRATION_COMPLETE));
        isReceiverRegistered = true;
    }
}

The MyBroadcastReceiver:

public class MyBroadcastReceiver extends WakefulBroadcastReceiver {
    @Override
    public void onReceive(Context context, Intent intent) {
        ComponentName comp = new ComponentName(
                MyGcmListenerService.class.getPackage().getName(),
                MyGcmListenerService.class.getName());
        context.startService((intent.setComponent(comp)));
        setResultCode(Activity.RESULT_CANCELED);
    }
}

The RegistrationIntentService:

public class RegistrationIntentService extends IntentService {
    private static final String TAG = RegistrationIntentService.class.getSimpleName();
    public RegistrationIntentService() { super(TAG); }

    protected void onHandleIntent(Intent intent) {
        InstanceID instanceID = InstanceID.getInstance(this);
        String senderId = getString(R.string.gcm_defaultSenderId);
        try {
            String token = instanceID.getToken(senderId, GoogleCloudMessaging.INSTANCE_ID_SCOPE);
            sendRegistrationToServer(token);
            subscribeTopics(token); // /topics/global
        } catch(...) {...}
    }
    Intent registrationComplete = new Intent(Preferences.REGISTRATION_COMPLETE);
    LocalBroadcastManager.getInstance(this).sendBroadcast(registrationComplete);
}

And finally the MyGcmListenerService:

public class MyGcmListenerService extends GcmListenerService {
    @Override
    public void onMessageReceived(String from, Bundle data) {
        String message = data.getString("message");
        Log.d(TAG, "From: " + from);
        Log.d(TAG, "Message: " + message);
    }
}

The requests I'm trying are:

{"data": { "message": "direct message" }, "to": "TOKEN" }
{"data": { "message": "topic message" },  "to": "/topics/global"}

For both I'm receiving a successful response.

QUESTIONS:

  1. Why MyGcmListenerService doesn't receive any message?
  2. Shouldn't the MyBroadcastReceiver be automatically linked to the app, without adding all that in the MainActivity?
  3. Is it better to use the GcmReceiver or to use the custom one? Why?
  4. Shouldn't the MyGcmListenerService handle all the receive messages? If not, what should be handled by it and what by MyBroadcastReceiver?
tasegula
  • 889
  • 1
  • 12
  • 26

3 Answers3

0

First Update the Broadcast so see what you get in Log

public class MyBroadcastReceiver extends WakefulBroadcastReceiver {
    @Override
    public void onReceive(Context context, Intent intent) {

        Bundle extras = intent.getExtras();

        String string = "Bundle{";
        for (String key : extras.keySet()) {
            string += " " + key + " => " + extras.get(key) + ";";
        }
        string += " }Bundle";

        Log.e("string","string"+string);

        ComponentName comp = new ComponentName(
                MyGcmListenerService.class.getPackage().getName(),
                MyGcmListenerService.class.getName());
        context.startService((intent.setComponent(comp)));
        setResultCode(Activity.RESULT_CANCELED);
    }
}
Abhay Dhiman
  • 131
  • 1
  • 6
0

check this hope this will help you GCM-Example

Moorthy
  • 723
  • 1
  • 6
  • 20
0

There seems to a delay ... a BIG delay between the time that the message was sent from the server to GCM, and when the message was received in the app.

I restarted the phone and before it connecting to the my wifi network, I received all the messages. There seems to be no delay if I'm on a 4G (and probably with 3G is the same).

Googling information about this delay, it seems that it is more common than it should be:

  1. How to avoid delay in Android GCM messages
  2. Receive Android GCM messages so slow?
  3. Google Cloud Messaging - messages either received instantly or with long delay

I tried the workaround presented in the first link, but while I'm on wifi, I receive a few messages right away, but after a while it stops. When I switch to 4G, I start receiving them again.

I observed this also happening with WhatApp notifications, because while testing this push notifications over wifi or 4G, I observed that when I switched to 4G I started receiving notification from the app, from 2 hours ago.

Also, according to link#3, the message should be received with a maximum delay of a few minutes, so it may also be a problem with the device, in this case a Nexus 5.

I may update this answer as I discover more about this issue.

Community
  • 1
  • 1
tasegula
  • 889
  • 1
  • 12
  • 26