Alterar comportamento do Push Notification de acordo com status da Activity

0 respostas
B

Boa tarde a todos,

Preciso de alterar o comportamento do Push Notification do meu app da seguinte forma:

- Se o app estiver em primeiro plano (Foreground), irá ocorrer a atualização de uma lista e não irá aparecer a notificação na barra de status.
- Se o app estiver em segundo plano (Background) ou fechado, a notificação irá aparecer.

Semelhante ao app do Gmail.

Procurei e achei essas formas:

1º:
ActivityManager activityManager = (ActivityManager) this.getSystemService(Context.ACTIVITY_SERVICE);
            List<RunningTaskInfo> services = activityManager.getRunningTasks(Integer.MAX_VALUE);
            boolean isActivityFound = false;

            if (services.get(0).topActivity.getPackageName().toString().equalsIgnoreCase(getPackageName().toString()))
            {
                isActivityFound = true;
            }
            if (!isActivityFound)
            {
                NotificationCompat.Builder builder = new NotificationCompat.Builder(this)
            .setSmallIcon(R.drawable.ic_launcher)
            .setContentTitle("xxxx")
            .setStyle(new NotificationCompat.BigTextStyle()
                    .bigText("Update List"))
            .setContentText("Update List").setAutoCancel(true).setDefaults(Notification.DEFAULT_ALL).setWhen(System.currentTimeMillis());
            builder.setContentIntent(listPendingIntent);
            NotificationManager mNotificationManager = (NotificationManager) getSystemService(Context.NOTIFICATION_SERVICE);
            mNotificationManager.notify(NOTIFICATION_ID, builder.build());
            }

2º: http://markhudnall.com/2013/11/13/gcm-foreground-and-background/

Existe alguma outra forma de fazer isso? Ou alguma dessas soluções é a que normalmente se utiliza?

Criado 4 de janeiro de 2015
Respostas 0
Participantes 1