0

I have GCM with php AND my code work without any problem its idea get notification from php and show it on notification bar and when click on notification its show me the message on TextView i just want to update that TextView on Activity without click on notification

now let me show the code for understand me this the home ACtivity thats contain the TextView

   import android.app.Activity;
   import android.os.Bundle;
   import android.widget.TextView;


   public class Home_Activity extends Activity {
   TextView view_msg;
 // i try make it public static TextView view_msg;

   @Override
    protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.home);

    view_msg = (TextView) findViewById(R.id.message);
   }
   public void onResume()
   {
    super.onResume();
    String str = getIntent().getStringExtra("msg");  
    if (str != null) {
        view_msg.setText(str);  
    }
   } 
   }

we can see its

onResume

check if there are any extra data but i want to update that TextView just when new gcm message recive

now the code that recive message

import android.app.IntentService;
import android.app.Notification;
import android.app.NotificationManager;
import android.app.PendingIntent;
import android.content.Context;
import android.content.Intent;
import android.os.Bundle;
import android.support.v4.app.NotificationCompat;

import com.medo.alex.Home_Activity;
import com.medo.alex.R;
import com.google.android.gms.gcm.GoogleCloudMessaging;


 public class Gcm_Notification_Intent_Service extends IntentService {
// Sets an ID for the notification, so it can be updated
public static final int notifyID = 9001;

public Gcm_Notification_Intent_Service() {
    super("GcmIntentService");
}


@Override
protected void onHandleIntent(Intent intent) {
    Bundle extras = intent.getExtras();
    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());
        } else if (GoogleCloudMessaging.MESSAGE_TYPE_MESSAGE
                .equals(messageType)) {

            sendNotification(""    +    extras.get(Gcm_Application_Constants.MSG_KEY)); //When Message is received normally from GCM Cloud Server
        }
    }
    Gcm_Broadcast_Receiver.completeWakefulIntent(intent);
}

private void sendNotification(String msg) {
    Intent resultIntent = new Intent(this, Home_Activity.class);
    resultIntent.putExtra("msg", msg);
    PendingIntent resultPendingIntent = PendingIntent.getActivity(this, 0,
            resultIntent, PendingIntent.FLAG_ONE_SHOT);  

    NotificationCompat.Builder mNotifyBuilder;
    NotificationManager mNotificationManager;

    mNotificationManager = (NotificationManager)      getSystemService(Context.NOTIFICATION_SERVICE);

    mNotifyBuilder = new NotificationCompat.Builder(this)
            .setContentTitle("NEW MESSAGE")
            .setSmallIcon(R.mipmap.ic_launcher);
    // Set pending intent
    mNotifyBuilder.setContentIntent(resultPendingIntent);

    // Set Vibrate, Sound and Light
    int defaults = 0;
    defaults = defaults | Notification.DEFAULT_LIGHTS;
    defaults = defaults | Notification.DEFAULT_VIBRATE;
    defaults = defaults | Notification.DEFAULT_SOUND;

    mNotifyBuilder.setDefaults(defaults);
    // Set the content for Notification
    mNotifyBuilder.setContentText("YOU HAVE NEW MESSAGE");
    // Set autocancel
    mNotifyBuilder.setAutoCancel(true);
    // Post a notification
    mNotificationManager.notify(notifyID, mNotifyBuilder.build());


  // MainActivity.view_msg.setText(msg); // iam try use this and make my view_msg public and static when i add this line and run my project and say progect name has stopped just when add this line 
  }
 }

now my question its how to update the view_msg and its on Home_activity when recive messgae from Gcm i write in code what i try to do but its say my project was stopped now forget my trying and just give me idea about how to update the view_msg TextView from another class

1 Answers1

1

It seems like you need Service bounding

some awesome example you can find HERE

just try to bind your Activity and when it can be done just send Message with content. when Activity is missing e.g. keep Notification popping

Community
  • 1
  • 1
snachmsm
  • 17,866
  • 3
  • 32
  • 74
  • Thankyou. I did not knew even remotely that such a thing exists. Another option to update would be to listen to notifications via NotificationListenerService and perform necessary actions in its onNotificationPosted() – Rusheel Jain Mar 28 '16 at 11:20
  • true, and yet another way is to create `singleTop` `Activity` if it can be in this launch mode, and passing new `Intent` to [onNewIntent](http://developer.android.com/reference/android/app/Activity.html#onNewIntent%28android.content.Intent%29) . also `NotificationListenerService` is introduced in API 18, a bit to high for me at this moment, still supporting lower ;) – snachmsm Mar 28 '16 at 11:22
  • i am beginner can u make simple solution to me :/ – Максим Зубков Mar 28 '16 at 11:31
  • probably simplest for you will be broadcasting without all binding action... just check pretty well written sample [HERE](http://www.mysamplecode.com/2011/10/android-intentservice-example-using.html) . note `broadcastIntent` and `sendBroadcast` in `IntentService` and `registerReceiver` with class which `extends BroadcastReceiver` in `Activity`. noboy said that this is simple, just figure your way out, all tools on the table ;) good luck! – snachmsm Mar 28 '16 at 11:44