Reputation: 7425
I have implemented the Google Cloud Messaging for Android client on my app. When sent from our 3rd party server, we received a success message:
EDITED
So turns out I am actually receiving the message from the Google Cloud Messaging server, but I am not getting a notification on the phone. I know I am receiving the message because it shows it my Logcat. The only problem is that my notification is not getting built/sent.
Why is this happening? Please note my GcmIntentService
is a separate class on its own. As well as the BroadcastReceiver
. I am guessing it has to do with my sendNotification()
method?
IntentService
public class GcmIntentService extends IntentService {
public static final int NOTIFICATION_ID = 1;
private NotificationManager mNotificationManager;
NotificationCompat.Builder builder;
public GcmIntentService() {
super("GcmIntentService");
}
@Override
protected void onHandleIntent(Intent intent) {
Bundle extras = intent.getExtras();
GoogleCloudMessaging gcm = GoogleCloudMessaging.getInstance(this);
// The getMessageType() intent parameter must be the intent you received in your BroadcastReceiver.
String messageType = gcm.getMessageType(intent);
if (!extras.isEmpty()) { // has effect of unparcelling Bundle
// Filter messages based on message type. Since it is likely that GCM will be extended in the future with
// new message types, just ignore any message types you're not interested in, or that you don't recognize.
if (GoogleCloudMessaging.MESSAGE_TYPE_SEND_ERROR.equals(messageType)) {
sendNotification("Send error: " + extras.toString());
}
else if (GoogleCloudMessaging.MESSAGE_TYPE_DELETED.equals(messageType)) {
sendNotification("Deleted messages on server: " + extras.toString());
}
// If it's a regular GCM message, do some work.
else if (GoogleCloudMessaging.MESSAGE_TYPE_MESSAGE.equals(messageType)) {
// This loop represents the service doing some work.
for (int i=0; i<5; i++) {
Log.i("GCM", "Working... " + (i+1) + "/5 @ " + SystemClock.elapsedRealtime());
try {
Thread.sleep(5000);
}
catch (InterruptedException e) { e.printStackTrace(); }
}
Log.i("GCM", "Completed work @ " + SystemClock.elapsedRealtime());
// Post notification of received message.
sendNotification("Received: " + extras.toString());
Log.i("GCM", "Received: " + extras.toString());
}
}
// Release the wake lock provided by the WakefulBroadcastReceiver.
GcmBroadcastReceiver.completeWakefulIntent(intent);
}
// Put the message into a notification and post it.
// This is just one simple example of what you might choose to do with a GCM message.
private void sendNotification(String msg) {
mNotificationManager = (NotificationManager) this.getSystemService(Context.NOTIFICATION_SERVICE);
PendingIntent contentIntent = PendingIntent.getActivity(this, 0, new Intent(this, LoginActivity.class), 0);
NotificationCompat.Builder mBuilder = new NotificationCompat.Builder(this)
.setContentTitle("GCM Notification")
.setStyle(new NotificationCompat.BigTextStyle()
.bigText(msg))
.setContentText(msg);
mBuilder.setContentIntent(contentIntent);
mNotificationManager.notify(NOTIFICATION_ID, mBuilder.build());
}
}
BroadcastReceiver
public class GcmBroadcastReceiver extends WakefulBroadcastReceiver {
@Override
public void onReceive(Context context, Intent intent) {
// Explicitly specify that GcmIntentService will handle the intent.
ComponentName comp = new ComponentName(context.getPackageName(), GcmIntentService.class.getName());
// Start the service, keeping the device awake while it is launching.
startWakefulService(context, (intent.setComponent(comp)));
setResultCode(Activity.RESULT_OK);
}
}
Upvotes: 2
Views: 1173
Reputation: 7425
Figured it out!!!
Turns out you need to have 3 things for every notification:
Doh!
Upvotes: 3
Reputation: 23873
From the look of your response on your server with the 'fake_message_I'd in, you must have set the dry_run parameter in your message. This just validates the message without actually sending it. Remove that parameter and retest.
Upvotes: 0