Reputation: 903
I have implemented GCM in my android application by using the google provided tutorial. Everything works fine but sometimes the "from" parameter of onMessageReceived method is null. Following is my GcmListenerService
enter code herepublic class MsgLstnrSvc extends GcmListenerService {
@Override
public void onMessageReceived(String from, Bundle data) {
String message = data.getString("message");
if (from.startsWith("/topics/")) {
// message received from some topic.
} else {
// normal downstream message.
}
}
private String LOG_AREA = "MsgLstnrSvc";
}
May I know what could be the reason for this? I never did GCM integration before this. Thanks in advance
Upvotes: 2
Views: 468
Reputation: 6791
Double check if you are using the key
that is sent from the server and in the app are the same.
for example:
Server
{
"to": "/topics/foo-bar",
"data": {
"message": "This is a GCM Topic Message!",
}
}
Client
@Override
public void onMessageReceived(String from, Bundle data) {
String message = data.getString("message");
Log.d(TAG, "From: " + from);
Log.d(TAG, "Message: " + message);
if (from.startsWith("/topics/")) {
// message received from some topic.
} else {
// normal downstream message.
}
// ...
}
for more information look at this SO question on how to implement the GCM message for server and client app.
Upvotes: 1