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