0

When my app receive "message" through google cloud messaging a notification is generate after clicking on that notification i am displaying that message in the TextView by applying text.setText(mycity);. Now what i want is the moment when my app receive gcm message, so instead of generating notification if my MainActivity is in the Foreground state i want to set gcm message directly in the TextView. How can i do this.

This is how at the moment my app working

Edited

GcmBroadcastReceiver

public class GcmBroadcastReceiver extends WakefulBroadcastReceiver{

    @Override
    public void onReceive(Context context, Intent intent) {
        // TODO Auto-generated method stub

        // 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);


    }

}

GcmIntentService

public class GcmIntentService extends IntentService{
Context context;
public static final int NOTIFICATION_ID = 1;
private NotificationManager mNotificationManager;
NotificationCompat.Builder builder;
public static final String TAG = "GCM Demo";

public GcmIntentService() {
    super("GcmIntentService");
    // TODO Auto-generated constructor stub
}

@Override
protected void onHandleIntent(Intent intent) {
    // TODO Auto-generated method stub
    Bundle extras = intent.getExtras();
    String msg = intent.getStringExtra("message");
    GoogleCloudMessaging gcm = GoogleCloudMessaging.getInstance(this);
    String messageType = gcm.getMessageType(intent);

     if (!extras.isEmpty()) {

         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(TAG, "Working... " + (i+1)
                            + "/5 @ " + SystemClock.elapsedRealtime());
                    try {
                        Thread.sleep(500);
                    } catch (InterruptedException e) {
                    }
                }
                Log.i(TAG, "Completed work @ " + SystemClock.elapsedRealtime());
                // Post notification of received message.
                //sendNotification("Received: " + extras.toString());
                sendNotification(msg);
                Log.i(TAG, "Received: " + extras.toString());

                          }
        }
     GcmBroadcastReceiver.completeWakefulIntent(intent);
}
private void sendNotification(String msg) {
    mNotificationManager = (NotificationManager)
            this.getSystemService(Context.NOTIFICATION_SERVICE);

    Log.i(TAG, msg);

    Intent myintent = new Intent(this, MainActivity.class);
    myintent.putExtra("message", msg);
    PendingIntent contentIntent = PendingIntent.getActivity(this, 0,
            myintent, PendingIntent.FLAG_UPDATE_CURRENT);

    NotificationCompat.Builder mBuilder =
            new NotificationCompat.Builder(this)
    .setSmallIcon(R.drawable.ic_stat_gcm)
    .setContentTitle("Notification")
    .setStyle(new NotificationCompat.BigTextStyle()
    .bigText(msg))
    .setContentText(msg);

    mBuilder.setAutoCancel(true);
    mBuilder.setContentIntent(contentIntent);
    mNotificationManager.notify(NOTIFICATION_ID, mBuilder.build());
}   

And is is how in MainActivity i am setting that message in TextView

        json = new JSONObject(message);
        String mycity = json.getString("city");
        text = (EditText) findViewById(R.id.this_is_the_id_of_textview);
        text.setText(mycity);
user3211342
  • 23
  • 1
  • 5
  • When you receive the GCM message, send broadcast with the city name to the activity with the text view, and in the broadcastReciver change the textView text – Itay Dec 16 '14 at 15:43
  • You didn't change the Google code for all... In the "else if (GoogleCloudMessaging. MESSAGE_TYPE_MESSAGE.equals(messageType)) {" you should change your code for your needs. For example: save the message you received in a Sqlite DB, then send broadcast to your main activity, and then in the BroadcastReciver change the text of your text view – Itay Dec 17 '14 at 07:11
  • ohh yes i understand now and one more thing i want to display that message only in TextView if my MainActivity where my TextView is in Foreground state else if MainActivity is in Background state garbage the message. How can i achieve this. – user3211342 Dec 17 '14 at 07:52
  • See http://stackoverflow.com/questions/18038399/how-to-check-if-activity-is-in-foreground-or-in-visible-background for answer – Itay Dec 17 '14 at 09:47

0 Answers0