Pasha Shkaran
Pasha Shkaran

Reputation: 1481

Android notification with GCM

I have chat app with GCM. When app is in foreground on particular chat activity i don't want to receive or show this notification. How i can do that?

Here is my notification:

private void sendNotification(String message, String userId, String senderName) {
    Intent intent = new Intent(this, ChatActivity.class);
    intent.addFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP);
    intent.putExtra("userId2", userId);
    PendingIntent pendingIntent = PendingIntent.getActivity(this, 0, intent,
            PendingIntent.FLAG_ONE_SHOT);

    Uri defaultSoundUri = RingtoneManager.getDefaultUri(RingtoneManager.TYPE_NOTIFICATION);
    NotificationCompat.Builder notificationBuilder = new NotificationCompat.Builder(this)
            .setSmallIcon(R.mipmap.ic_launcher)
            .setContentTitle(senderName+" send message")
            .setContentText(message)
            .setAutoCancel(true)
            .setSound(defaultSoundUri)
            .setContentIntent(pendingIntent);

    NotificationManager notificationManager =
            (NotificationManager) getSystemService(Context.NOTIFICATION_SERVICE);

    notificationManager.notify(0, notificationBuilder.build());
}

Upvotes: 0

Views: 84

Answers (3)

Mobile Apps Expert
Mobile Apps Expert

Reputation: 1048

You can check wheather is your app is in forground or not by this

;

import java.util.List;

import android.app.ActivityManager;
import android.app.ActivityManager.RunningAppProcessInfo;
import android.content.Context;
import android.os.AsyncTask;

public class ForegroundCheckTask extends AsyncTask<Context, Void, Boolean> {

    @Override
    protected Boolean doInBackground(Context... params) {
        final Context context = params[0].getApplicationContext();
        return isAppOnForeground(context);
    }

    private boolean isAppOnForeground(Context context) {
        ActivityManager activityManager = (ActivityManager) context
                .getSystemService(Context.ACTIVITY_SERVICE);
        List<RunningAppProcessInfo> appProcesses = activityManager
                .getRunningAppProcesses();
        if (appProcesses == null) {
            return false;
        }
        final String packageName = context.getPackageName();
        for (RunningAppProcessInfo appProcess : appProcesses) {
            if (appProcess.importance == RunningAppProcessInfo.IMPORTANCE_FOREGROUND
                    && appProcess.processName.equals(packageName)) {
                return true;
            }
        }
        return false;
    }
}

And add this permission in you manifest

<uses-permission android:name="android.permission.GET_TASKS"/>

Upvotes: 0

Arth Tilva
Arth Tilva

Reputation: 2496

To check whether ChatActivity is in foreground or not keep one variable. boolean isActive; make this variable false in onPause and onDestroy and make it true in onResume. For accessing this variable in other class make it public and static to access it with class name.

public static boolean isActive;

@Override
public void onResume() {
	super.onResume();
	isActive=true;
}
@Override
public void onPause() {
	super.onPause();
	isActive=false;
}

@Override
protected void onDestroy() {
	super.onDestroy();
	isActive=false;
}

Now while sending notificaton check value of this variable.

if(!ChatActivity.isActive){
  sendNotification();
}

Upvotes: 2

Brian Hoang
Brian Hoang

Reputation: 1111

You can unregister GCM when your app in in foreground or save app state on SharedPreference.

Upvotes: 0

Related Questions