I would like to, on receive of a GCM, display a Popup on my current Activity if my app is active.
I wanted to access my current activity in GcmIntentService but I dont think it is possible or a good way to proceed...
Can anyone help me?
Solution
In my GcmIntentService.java :
@Override
protected void onHandleIntent(Intent intent) {
...
Intent broadCastIntent = new Intent("client_notifications_broadcast");
broadCastIntent.putExtra("data", extras.getString("other"));
LocalBroadcastManager.getInstance(this).sendBroadcast(broadCastIntent);
...
}
In my MainActivity extended by all activities where I want the popup I add a Dialog with a custom layout :
@Override
protected void onCreate(Bundle savedInstanceState) {
...
mMessageReceiver = new BroadcastReceiver() {
@Override
public void onReceive(Context context, Intent intent) {
String dataString = intent.getStringExtra("data");
final Dialog dialog = new Dialog(ClientMainActivity.this);
dialog.setContentView(R.layout.custom_dialog_popup);
dialog.setTitle("Title...");
// set the custom dialog components - text, image and button
TextView text = (TextView) dialog.findViewById(R.id.text);
text.setText("Android custom dialog example!");
ImageView image = (ImageView) dialog.findViewById(R.id.image);
image.setImageResource(R.drawable.ic_launcher);
TextView dialogButton = (TextView) dialog.findViewById(R.id.dialogButtonOK);
// if button is clicked, close the custom dialog
dialogButton.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
dialog.dismiss();
}
});
dialog.show();
Log.d("receiver", "Got message: " + dataString);
}
};
LocalBroadcastManager.getInstance(this).registerReceiver(mMessageReceiver, new IntentFilter("client_notifications_broadcast"));
}