Bolandian Eran
Bolandian Eran

Reputation: 221

GCM - why im getting NULL in onMessageReceived?

im using google cloud messaging and i successfully send push notification. the only problem i have is that the method onMessageReceived always gets null. i can tell by the log, it shows me null. im sending from the server to the user and thats what i get


for example 04-16 12:29:17.231 19637-19705/com.world.bolandian.watchme
D/MyGcmListenerService: Message: null

This is my server code

 package jdbc;

 import java.io.IOException;
 import java.io.UnsupportedEncodingException;
 import java.net.URLEncoder;

 import javax.sound.midi.SysexMessage;

 import com.google.android.gcm.server.Message;
 import com.google.android.gcm.server.Result;
 import com.google.android.gcm.server.Sender;

 public class GcmSender {
    private static final int GCM_RETRIES = 2;

  public void sendGooglePushNotification(String token, String type, String data, String serverApiKey) {

    // Create Message
    Message.Builder builder = new Message.Builder().delayWhileIdle(true);

    try {
        builder.addData(Params.TYPE_MESSAGE, URLEncoder.encode(type, "UTF-8"));
        builder.addData(Params.DATA_MESSAGE, URLEncoder.encode(data, "UTF-8"));
    } catch (UnsupportedEncodingException e1) {
        // TODO Auto-generated catch block
        e1.printStackTrace();
    }

    Message message = builder.build();

    // Create Sender
    Sender sender = new Sender(serverApiKey);

    // Sending Message with Sender to the list of users.
    try {

        Result rs=sender.send(message, token, GCM_RETRIES);
        String ans=rs.toString();
        System.out.println(ans);

        //System.out.println("notification sent");
    } catch (IOException e) {
        // TODO Auto-generated catch block
        e.printStackTrace();
        // Failed to Send Message.

        }

    }
 }

And this is the Android code

 package com.world.bolandian.watchme;

 import android.app.NotificationManager;
 import android.app.PendingIntent;
 import android.content.Context;
 import android.content.Intent;
 import android.media.RingtoneManager;
 import android.net.Uri;
 import android.os.Bundle;
 import android.support.v4.app.NotificationCompat;
 import android.util.Log;

 import com.google.android.gms.gcm.GcmListenerService;

 public class MyGcmListenerService extends GcmListenerService {

  private static final String TAG = "MyGcmListenerService";

   /**
   * Called when message is received.
   *
   * @param from SenderID of the sender.
   * @param data Data bundle containing message data as key/value pairs.
   *             For Set of keys use data.keySet().
   */
  // [START receive_message]
@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.
    }

    // [START_EXCLUDE]
    /**
     * Production applications would usually process the message here.
     * Eg: - Syncing with server.
     *     - Store message in local database.
     *     - Update UI.
     */

    /**
     * In some cases it may be useful to show a notification indicating to the user
     * that a message was received.
     */
    sendNotification(message);
    // [END_EXCLUDE]
}
// [END receive_message]

/**
 * Create and show a simple notification containing the received GCM message.
 *
 * @param message GCM message received.
 */
private void sendNotification(String message) {
    Intent intent = new Intent(this, MainActivity.class);
    intent.addFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP);
    PendingIntent pendingIntent = PendingIntent.getActivity(this, 0 /* Request code */, intent,
            PendingIntent.FLAG_ONE_SHOT);

    Uri defaultSoundUri = RingtoneManager.getDefaultUri(RingtoneManager.TYPE_NOTIFICATION);
    NotificationCompat.Builder notificationBuilder = new NotificationCompat.Builder(this)
            .setSmallIcon(R.drawable.common_google_signin_btn_icon_dark)
            .setContentTitle("ALERT")
            .setContentText(message)
            .setAutoCancel(true)
            .setSound(defaultSoundUri)
            .setContentIntent(pendingIntent);

    NotificationManager notificationManager =

            (NotificationManager) getSystemService(Context.NOTIFICATION_SERVICE);

    notificationManager.notify(0 /* ID of notification */, notificationBuilder.build());
   }
 }

I do get the GCM Message but without the message (data) that i send from the server. and on the log it shows me null. do you guys have any idea how to fix this?

Upvotes: 1

Views: 1067

Answers (1)

user4510427
user4510427

Reputation:

Message.Builder builder = new Message.Builder().addData("message", "Your string here").delayWhileIdle(true);

OR

Message message = new Message.Builder().addData("message", "Your string here").delayWhileIdle(true).build();

In server side you need to add data to the message you should add data with key and value then you will get it in android like that , in your code you wait data from key "message" in android while no send it from the server

Upvotes: 1

Related Questions