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:
- Why
MyGcmListenerService
doesn't receive any message? - Shouldn't the
MyBroadcastReceiver
be automatically linked to the app, without adding all that in theMainActivity
? - Is it better to use the
GcmReceiver
or to use the custom one? Why? - Shouldn't the
MyGcmListenerService
handle all the receive messages? If not, what should be handled by it and what byMyBroadcastReceiver
?