I am doing push notification in my android app that is triggered by GCM. I want to display the notification if the app is running in a alert box,if the app is not running means not in the foreground just display the status bar notification, How do you determine if the app is running and is in the foreground?is this is possible to show notification like this.now am using this code for show status bar push notification
NotificationManager notificationManager = (NotificationManager)
context.getSystemService(Context.NOTIFICATION_SERVICE);
Notification notification = new Notification(icon, message, when);
String title = context.getString(R.string.app_name);
Intent notificationIntent = new Intent(context, BottomActivity.class);
// set intent so it does not start a new activity
notificationIntent.setFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP | Intent.FLAG_ACTIVITY_SINGLE_TOP);
PendingIntent intent = PendingIntent.getActivity(context, 0, notificationIntent, 0);
notification.setLatestEventInfo(context, title, message, intent);
notification.flags |= Notification.FLAG_AUTO_CANCEL;
// Play default notification sound
notification.defaults |= Notification.DEFAULT_SOUND;
//notification.sound = Uri.parse("android.resource://" + context.getPackageName() + "your_sound_file_name.mp3");
// Vibrate if vibrate is enabled
notification.defaults |= Notification.DEFAULT_VIBRATE;
notificationManager.notify(0, notification);
Firebase notifications behave differently depending on the foreground/background state of the receiving app. If you want foregrounded apps to receive notification messages or data messages, you'll need to write code to handle the onMessageReceived callback.
Let's start with Android. The Android OS is designed to listen for push messages and upon receiving one, wake up the appropriate Android app to handle the push message, regardless of whether the app is closed or not.
Foreground push provides the ability to send regular visible push notifications to the foreground of a user's device. Background push is available regardless of whether a particular device has opted-in to receive push notifications from that brand.
Turn on notifications for Android devicesTap More on the bottom navigation bar and select Settings. Tap Turn on notifications. Tap Notifications. Tap Show notifications.
You can create a new Activity and theme as it as Dialog and check for foreground activity. Check for below code.
ActivityManager activityManager = (ActivityManager) context.getSystemService(Context.ACTIVITY_SERVICE);
List<RunningTaskInfo> services = activityManager
.getRunningTasks(Integer.MAX_VALUE);
boolean isActivityFound = false;
if (services.get(0).topActivity.getPackageName().toString()
.equalsIgnoreCase(context.getPackageName().toString())) {
isActivityFound = true;
}
if (isActivityFound) {
resultIntent = new Intent(context, AlertDialogNotification.class);
resultIntent.putExtra(EXTRA_ALERT_MESSAGE_BEAUTIFUL, "anyMessage");
resultIntent.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
context.startActivity(resultIntent);
}
In the manifest make the theme as Dialog for the activity. Hope it helps. Let us know if you have issue.
How many activities do you have? If just one, it's quite easy to keep a singleton reference to it between onResume() and onPause() (AKA when it's active), and call the activity from the receiver if it's. if you have many activities, you can achieve a similar logic by having a common base class for them. Something like this:
class MyActivity: Activity
{
static private MyActivity _Current = null;
protected void onResume() //Activated
{
super.onResume();
_Current = this;
}
protected void onPause() //Deactivated
{
super.onPause();
_Current = null;
}
//This is for the receiver to call
static public PopAlert()
{
if(_Current != null)
{
new AlertDialog.Builder(_Current)
.setMessage("Hello world")
//More alert setup; use _Current as a context object
.create().show();
}
}
}
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With