Reputation: 270
i wants to show only dialog when notification comes and app is already opened. i do not wants to show notification on notificationbar if app is opened like divyabhaskar Android app
Upvotes: 2
Views: 1144
Reputation: 3195
When notification comes write following code
ActivityManager am = (ActivityManager) this.getSystemService(Context.ACTIVITY_SERVICE);
List<ActivityManager.RunningTaskInfo> taskInfo = am
.getRunningTasks(1);
Log.d("current task :", "CURRENT Activity ::"
+ taskInfo.get(0).topActivity.getClass().getSimpleName());
ComponentName componentInfo = taskInfo.get(0).topActivity;
className = componentInfo.getClassName();
Log.v("", "class Name-" + className);
if (extras != null) {
if (componentInfo.getPackageName().equalsIgnoreCase(
getPackageName())) {
Log.v("", "inside app-");
Intent broadcast = new Intent(ApplicationConstants.MY_BROADCAST);
broadcast.putExtra(ApplicationConstants.BOOKING_ID, bookingId);
broadcast.putExtra(ApplicationConstants.NOTIFICATION_TYPE, type);
broadcast.putExtra(ApplicationConstants.MESSAGE, message);
broadcast.putExtra(ApplicationConstants.CLASS_NAME, className);
LocalBroadcastManager.getInstance(this).sendBroadcast(
broadcast);
Log.v("", "Sending broadcast....");
} else {
sendNotification(extras);
}
And register broadcast receiver in the activity where you want dialog.
LocalBroadcastManager.getInstance(this).registerReceiver(receiver,
new IntentFilter(ApplicationConstants.MY_BROADCAST));
Receive Broadcast
private BroadcastReceiver receiver = new BroadcastReceiver() {
@Override
public void onReceive(Context context, Intent intent) {
if (intent.getAction().equals(ApplicationConstants.MY_BROADCAST)) {
//Log.v("", "inside on receiver-");
showDialog(intent.getStringExtra(ApplicationConstants.MESSAGE));
}
}
};
In onStop Method unregister the broadcast
@Override
protected void onStop() {
super.onStop();
if (receiver != null) {
LocalBroadcastManager.getInstance(this).unregisterReceiver(receiver);
}
}
Upvotes: 1