You can do it only with IntentService
.
When a new Notification
is received in your BroadcastReceiver
, you will send it to the IntentService
so, there before displaying Notification with NotificationBuilder
have a Listener
set to the Activity
. If Listener
exists, the user is inside the Activity, then just ignore the notifications.
For Example, define a Listener like this,
public interface PushNotificationListenerService {
public void showNewMessage();
}
And in your IntentService
before displaying Notification,
public void setListener(PushNotificationListenerService listener) {
onPushReceivedCallback = listener;
}
Handler mHandler = new Handler(getMainLooper());
mHandler.post(new Runnable() {
@Override
public void run() {
if (onPushReceivedCallback != null) {
onPushReceivedCallback.showNewMessage();
// then ignore the notification.
}
else{
// show notification
}
});
In you Activity,
onCreate
Method,
NotificationIntentService.getInstance(this).setListener(this);