EliKo
EliKo

Reputation: 101

Parsing received GCM message into push notification

I'm sending GCM messages from my server to my application.

the notification works with sample data, but when I'm trying to use the received message information from my server, I get empty values.

The is exmplae for a message I get from my server: (received as msg at showNotification())

Received: {
"subtitle": "text",
"sound": "1",
"message": "bla bla",
etc..

This is how I tried to handle it (look for showNotification()):

public class GcmService extends GcmListenerService {
    String title;

    @Override
    public void onMessageReceived(String from, Bundle data) {
        JSONObject jsonObject = new JSONObject();
        Set<String> keys = data.keySet();
        for (String key : keys) {
            try {
                jsonObject.put(key, data.get(key));
            } catch (JSONException e) {
                e.printStackTrace();
            }
        }
        try {
            sendNotification("Received: " + jsonObject.toString(5));
        } catch (JSONException e) {
            e.printStackTrace();
        }
    }

    @Override
    public void onDeletedMessages() {
        sendNotification("Deleted messages on server");
    }

    @Override
    public void onMessageSent(String msgId) {
        sendNotification("Upstream message sent. Id=" + msgId);
    }

    @Override
    public void onSendError(String msgId, String error) {
        sendNotification("Upstream message send error. Id=" + msgId + ", error" + error);
    }

    private void sendNotification(final String msg) {
        Handler handler = new Handler(Looper.getMainLooper());
        handler.post(new Runnable() {
            @Override
            public void run() {
                MainActivity.mTextView.setText(msg);

                //JSON Parsing
                try {
                    JSONObject thePush = new JSONObject(msg);
                    JSONArray pushData;
                    pushData = thePush.optJSONArray("Received");
                    thePush = pushData.optJSONObject(0);
                    if (thePush != null) {
                        //Initalize data from my JSON
                        title = thePush.optString("title");
                    }

                } catch (JSONException e) {
                    e.printStackTrace();
                }

                NotificationCompat.Builder mBuilder =
                        new NotificationCompat.Builder(getApplicationContext())
                                .setSmallIcon(R.drawable.beer)
                                .setContentTitle(title)
                                .setContentText("Hello World!");
                // Creates an explicit intent for an Activity in your app
                Intent resultIntent = new Intent(getApplicationContext(), MainActivity.class);

// The stack builder object will contain an artificial back stack for the
// started Activity.
// This ensures that navigating backward from the Activity leads out of
// your application to the Home screen.
                TaskStackBuilder stackBuilder = TaskStackBuilder.create(getApplicationContext());
// Adds the back stack for the Intent (but not the Intent itself)
                stackBuilder.addParentStack(MainActivity.class);
// Adds the Intent that starts the Activity to the top of the stack
                stackBuilder.addNextIntent(resultIntent);
                PendingIntent resultPendingIntent =
                        stackBuilder.getPendingIntent(
                                0,
                                PendingIntent.FLAG_UPDATE_CURRENT
                        );
                mBuilder.setContentIntent(resultPendingIntent);
                NotificationManager mNotificationManager =
                        (NotificationManager) getSystemService(Context.NOTIFICATION_SERVICE);
// mId allows you to update the notification later on.
                mNotificationManager.notify(1, mBuilder.build());
            }
        });
    }


}

When I Receive GCM message from the below code, I get a message with no title. the body works since the value is not from my json, for testing.

What's the problem with the way I received the json?

Upvotes: 1

Views: 1084

Answers (1)

Satyen Udeshi
Satyen Udeshi

Reputation: 3243

Data you are receiving is already in json format so you can do something like below to get value from the corresponding key

@Override
public void onMessageReceived(String from, Bundle data) {
    String subtitle = data.getString("subtitle","defValue");
    String sound = data.getString("sound","defValue");
    String message = data.getString("message","defValue");
    //..... fetch other values similarly 

    Log.d("Data ->",subtitle+"-"+sound+"-"+message);
}

Upvotes: 1

Related Questions