Reputation: 1272
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"));
}
Upvotes: 2
Views: 4345
Reputation: 23990
I have something like this:
In your GCM broadcast receiver
Intent intent = new Intent(NOTIFICATION_ACTION);
intent.putExtra(EXTRA_PARAMETER, "something you want to pass as an extra");
context.sendBroadcast(intent);
In your BaseActivity (an Activity that is extended by all the activities that you want to show the pop up)
private BroadcastReceiver notificationBroadcastReceiver = new NotificationBroadcastReceiver();
@Override
protected void onStart() {
super.onStart();
IntentFilter intentFilter = new IntentFilter(NOTIFICATION_ACTION);
registerReceiver(notificationBroadcastReceiver, intentFilter);
}
@Override
protected void onStop() {
super.onStop();
unregisterReceiver(notificationBroadcastReceiver);
}
private class NotificationBroadcastReceiver extends BroadcastReceiver {
@Override
public void onReceive(Context context, Intent intent) {
//Show popup
}
}
NOTIFICATION_ACTION is a constant you should define somewhere
Upvotes: 1
Reputation: 26017
Well, you can use LocalBroadcast
. See LocalBroadcast Manager.
For how to implement is, there is a great example on how to use LocalBroadcastManager?.
LocalBroadcast Manager is a helper to register for and send broadcasts of Intents to local objects within your process. The data you are broadcasting won't leave your app, so don't need to worry about leaking private data.`
Your activity registers for this local broadcast. From the service you send a LocalBroadcast
from within the onMessage
(saying that hey, I received a message. Show it activity). Then inside your Activity
you can listen to the broadcast. This way if the activity is in the forefront/is active, it will receive the broadcast otherwise it won't. So, whenever you receive that local broadcast, you may do the desired action if activity is open.
If you want to do for the whole app, then you can make all your activities extend an abstract activity. And inside this abstract activity class you can register it for this 'LocalBroadcast'. Other way is to register for LocalBroadcast inside all your activities (but then you'll have to manage how you'll show the message only once).
Hope it helps you.
Upvotes: 1