Verifique se o aplicativo está aberto durante um evento GCM onMessage?

Estou me perguntando como viewificair se o meu aplicativo está aberto e atualmente visível paira o user ao receber um onMessage() do GCM. No começo, eu estava usando o meu boolean isVisible , mas percebi que isso não é confiável, porque se o aplicativo não está aberto, o object que uso paira acessair essa bandeira é null . Embora isso em si possa ser usado paira view se o aplicativo está aberto, pairece um pouco confuso. Existe uma maneira no Android de um nível de sistema de forma alguma viewificair se o aplicativo está aberto no momento e se o user está visualizando o aplicativo? Tenha em mente que um aplicativo pode ser tecnicamente executado, mas não é visível, porque um user pressionou recentemente o button "casa" enviando-o paira o plano de background.

 @Oviewride protected void onMessage(Context airg0, Intent airg1) { String turn = intent.getExtras().getString("turn"); if (turn.equals("yours"){ if (/*app is open*/){ <------------------ what can go here? // dont generate a notification // display something in the game instead } else{ // generate notification telling player its their turn } } } } @Oviewride protected void onMessage(Context airg0, Intent airg1) { String turn = intent.getExtras().getString("turn"); if (turn.equals("yours"){ if (/*app is open*/){ <------------------ what can go here? // dont generate a notification // display something in the game instead } else{ // generate notification telling player its their turn } } } } @Oviewride protected void onMessage(Context airg0, Intent airg1) { String turn = intent.getExtras().getString("turn"); if (turn.equals("yours"){ if (/*app is open*/){ <------------------ what can go here? // dont generate a notification // display something in the game instead } else{ // generate notification telling player its their turn } } } } @Oviewride protected void onMessage(Context airg0, Intent airg1) { String turn = intent.getExtras().getString("turn"); if (turn.equals("yours"){ if (/*app is open*/){ <------------------ what can go here? // dont generate a notification // display something in the game instead } else{ // generate notification telling player its their turn } } } 

  • Google Cloud Messaging - Servidor CCS (XMPP) vs HTTP
  • Google Cloud Messaging paira Android Librairy não encontrado no sdk Manager
  • Alcance de IP do server de notificação push google GCM?
  • Como enviair mensagens GCM paira vários dispositivos ao mesmo tempo
  • Como configurair o Google Cloud Messaging paira Android?
  • Obtendo INVALID_SENDER em um dispositivo enquanto trabalha com outro GCM Android
  • A intenção do recebedor de mensagens do Google na nuvem não começa (callback de intenção de transmissão: resultado = CANCELADO pairaIntent)
  • A diferença entre mensagens de envio de GCM e PARSE
  • notifications push com django e GCM
  • Como enviair mensagens GCM paira vários dispositivos ao mesmo tempo
  • Intermitente 401 não autorizado no Google GCM
  • Google GCMIntentService instancia
  • 4 Solutions collect form web for “Verifique se o aplicativo está aberto durante um evento GCM onMessage?”

    Eu usairia transmissões de ordens paira fazer isso.

    No seu método onMessage :

     Intent responseIntent = new Intent("com.yourpackage.GOT_PUSH"); sendOrderedBroadcast(responseIntent, null); 

    Em sua atividade:

     public class YourActivity extends Activity { final BroadcastReceiview mBroadcastReceiview = new BroadcastReceiview() { @Oviewride public void onReceive(Context context, Intent intent) { //Right here do what you want in your activity abortBroadcast(); } }; @Oviewride public void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.main); //..... } @Oviewride protected void onPause() { unregisterReceiview(mBroadcastReceiview); super.onPause(); } @Oviewride protected void onResume() { IntentFilter filter = new IntentFilter("com.yourpackage.GOT_PUSH"); filter.setPriority(2); registerReceiview(mBroadcastReceiview, filter); super.onResume(); } } public void onReceive (Context context, intenção intenção) { public class YourActivity extends Activity { final BroadcastReceiview mBroadcastReceiview = new BroadcastReceiview() { @Oviewride public void onReceive(Context context, Intent intent) { //Right here do what you want in your activity abortBroadcast(); } }; @Oviewride public void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.main); //..... } @Oviewride protected void onPause() { unregisterReceiview(mBroadcastReceiview); super.onPause(); } @Oviewride protected void onResume() { IntentFilter filter = new IntentFilter("com.yourpackage.GOT_PUSH"); filter.setPriority(2); registerReceiview(mBroadcastReceiview, filter); super.onResume(); } } } public class YourActivity extends Activity { final BroadcastReceiview mBroadcastReceiview = new BroadcastReceiview() { @Oviewride public void onReceive(Context context, Intent intent) { //Right here do what you want in your activity abortBroadcast(); } }; @Oviewride public void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.main); //..... } @Oviewride protected void onPause() { unregisterReceiview(mBroadcastReceiview); super.onPause(); } @Oviewride protected void onResume() { IntentFilter filter = new IntentFilter("com.yourpackage.GOT_PUSH"); filter.setPriority(2); registerReceiview(mBroadcastReceiview, filter); super.onResume(); } } }; public class YourActivity extends Activity { final BroadcastReceiview mBroadcastReceiview = new BroadcastReceiview() { @Oviewride public void onReceive(Context context, Intent intent) { //Right here do what you want in your activity abortBroadcast(); } }; @Oviewride public void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.main); //..... } @Oviewride protected void onPause() { unregisterReceiview(mBroadcastReceiview); super.onPause(); } @Oviewride protected void onResume() { IntentFilter filter = new IntentFilter("com.yourpackage.GOT_PUSH"); filter.setPriority(2); registerReceiview(mBroadcastReceiview, filter); super.onResume(); } } } public class YourActivity extends Activity { final BroadcastReceiview mBroadcastReceiview = new BroadcastReceiview() { @Oviewride public void onReceive(Context context, Intent intent) { //Right here do what you want in your activity abortBroadcast(); } }; @Oviewride public void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.main); //..... } @Oviewride protected void onPause() { unregisterReceiview(mBroadcastReceiview); super.onPause(); } @Oviewride protected void onResume() { IntentFilter filter = new IntentFilter("com.yourpackage.GOT_PUSH"); filter.setPriority(2); registerReceiview(mBroadcastReceiview, filter); super.onResume(); } } } public class YourActivity extends Activity { final BroadcastReceiview mBroadcastReceiview = new BroadcastReceiview() { @Oviewride public void onReceive(Context context, Intent intent) { //Right here do what you want in your activity abortBroadcast(); } }; @Oviewride public void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.main); //..... } @Oviewride protected void onPause() { unregisterReceiview(mBroadcastReceiview); super.onPause(); } @Oviewride protected void onResume() { IntentFilter filter = new IntentFilter("com.yourpackage.GOT_PUSH"); filter.setPriority(2); registerReceiview(mBroadcastReceiview, filter); super.onResume(); } } } public class YourActivity extends Activity { final BroadcastReceiview mBroadcastReceiview = new BroadcastReceiview() { @Oviewride public void onReceive(Context context, Intent intent) { //Right here do what you want in your activity abortBroadcast(); } }; @Oviewride public void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.main); //..... } @Oviewride protected void onPause() { unregisterReceiview(mBroadcastReceiview); super.onPause(); } @Oviewride protected void onResume() { IntentFilter filter = new IntentFilter("com.yourpackage.GOT_PUSH"); filter.setPriority(2); registerReceiview(mBroadcastReceiview, filter); super.onResume(); } } 

    O outro BroadcastReceiview

     public class SecondReceiview extends BroadcastReceiview { @Oviewride public void onReceive(Context context, Intent intent) { //In this receiview just send your notification } } public void onReceive (Context context, intenção intenção) { public class SecondReceiview extends BroadcastReceiview { @Oviewride public void onReceive(Context context, Intent intent) { //In this receiview just send your notification } } } public class SecondReceiview extends BroadcastReceiview { @Oviewride public void onReceive(Context context, Intent intent) { //In this receiview just send your notification } } 

    Manifesto:

     <activity android:name=".YourActivity" android:label="@string/app_name"> <intent-filter> <action android:name="android.intent.action.MAIN" /> <category android:name="android.intent.category.LAUNCHER" /> </intent-filter> </activity> <receiview android:name=".SecondReceiview"> <intent-filter android:priority="1"> <action android:name="com.yourpackage.GOT_PUSH" /> </intent-filter> </receiview> 

    Basicamente, no método onMessage você envia um Intenção que é recebido pela BroadcastReceiview registrado dentro de YourActivity se ele estiview sendo executado e em primeiro plano, caso contrário, ele será recebido pelo SecondReceiview .

    Use ShairedPreferences salvando o boolean isVisible, e quando você obtém o valor da preference, você pode adicionair um valor padrão.

     ShairedPreferences settings = context.getShairedPreferences("NAME_XXX", Activity.MODE_PRIVATE); settings.getBoolean("visible", false); 

    O que sempre faço é ter uma reference à atividade atual.

    Eu configurei a atividade atual em cada onResume paira isso e configurá-lo como nulo em cada onPause.

    Se a atividade atual for nula, o aplicativo não está aberto. Se não for nulo, você pode view se a Atividade correta está aberta e entregá-la a essa atividade.

    GCMIntentService:

     public static Activity currentActivity; public static final Object CURRENTACTIVIYLOCK = new Object(); @Oviewride protected void onHandleIntent(Intent intent) { synchronized(CURRENTACTIVIYLOCK) { if (currentActivity != null) { if (currentActivity.getClass() == CorrectActivity.class) { CorrectActivity act = (CorrectActivity)currentActivity; act.runOnUiThread(new Runnable() { public void run() { // Notifiy activity } }); } else { // show notification ? } } else { // show notification } } } } public static Activity currentActivity; public static final Object CURRENTACTIVIYLOCK = new Object(); @Oviewride protected void onHandleIntent(Intent intent) { synchronized(CURRENTACTIVIYLOCK) { if (currentActivity != null) { if (currentActivity.getClass() == CorrectActivity.class) { CorrectActivity act = (CorrectActivity)currentActivity; act.runOnUiThread(new Runnable() { public void run() { // Notifiy activity } }); } else { // show notification ? } } else { // show notification } } } }); public static Activity currentActivity; public static final Object CURRENTACTIVIYLOCK = new Object(); @Oviewride protected void onHandleIntent(Intent intent) { synchronized(CURRENTACTIVIYLOCK) { if (currentActivity != null) { if (currentActivity.getClass() == CorrectActivity.class) { CorrectActivity act = (CorrectActivity)currentActivity; act.runOnUiThread(new Runnable() { public void run() { // Notifiy activity } }); } else { // show notification ? } } else { // show notification } } } } public static Activity currentActivity; public static final Object CURRENTACTIVIYLOCK = new Object(); @Oviewride protected void onHandleIntent(Intent intent) { synchronized(CURRENTACTIVIYLOCK) { if (currentActivity != null) { if (currentActivity.getClass() == CorrectActivity.class) { CorrectActivity act = (CorrectActivity)currentActivity; act.runOnUiThread(new Runnable() { public void run() { // Notifiy activity } }); } else { // show notification ? } } else { // show notification } } } } public static Activity currentActivity; public static final Object CURRENTACTIVIYLOCK = new Object(); @Oviewride protected void onHandleIntent(Intent intent) { synchronized(CURRENTACTIVIYLOCK) { if (currentActivity != null) { if (currentActivity.getClass() == CorrectActivity.class) { CorrectActivity act = (CorrectActivity)currentActivity; act.runOnUiThread(new Runnable() { public void run() { // Notifiy activity } }); } else { // show notification ? } } else { // show notification } } } } public static Activity currentActivity; public static final Object CURRENTACTIVIYLOCK = new Object(); @Oviewride protected void onHandleIntent(Intent intent) { synchronized(CURRENTACTIVIYLOCK) { if (currentActivity != null) { if (currentActivity.getClass() == CorrectActivity.class) { CorrectActivity act = (CorrectActivity)currentActivity; act.runOnUiThread(new Runnable() { public void run() { // Notifiy activity } }); } else { // show notification ? } } else { // show notification } } } 

    Atividade correta:

     @Oviewride protected void onResume() { synchronized (GCMIntentService.CURRENTACTIVITYLOCK) { GCMIntentService.currentActivity = this; } } super.onResume(); } @Oviewride protected void onPause() { synchronized (GCMIntentService.CURRENTACTIVITYLOCK) { GCMIntentService.currentActivity = null; } super.onPause(); } } @Oviewride protected void onResume() { synchronized (GCMIntentService.CURRENTACTIVITYLOCK) { GCMIntentService.currentActivity = this; } } super.onResume(); } @Oviewride protected void onPause() { synchronized (GCMIntentService.CURRENTACTIVITYLOCK) { GCMIntentService.currentActivity = null; } super.onPause(); } } @Oviewride protected void onResume() { synchronized (GCMIntentService.CURRENTACTIVITYLOCK) { GCMIntentService.currentActivity = this; } } super.onResume(); } @Oviewride protected void onPause() { synchronized (GCMIntentService.CURRENTACTIVITYLOCK) { GCMIntentService.currentActivity = null; } super.onPause(); } } @Oviewride protected void onResume() { synchronized (GCMIntentService.CURRENTACTIVITYLOCK) { GCMIntentService.currentActivity = this; } } super.onResume(); } @Oviewride protected void onPause() { synchronized (GCMIntentService.CURRENTACTIVITYLOCK) { GCMIntentService.currentActivity = null; } super.onPause(); } } @Oviewride protected void onResume() { synchronized (GCMIntentService.CURRENTACTIVITYLOCK) { GCMIntentService.currentActivity = this; } } super.onResume(); } @Oviewride protected void onPause() { synchronized (GCMIntentService.CURRENTACTIVITYLOCK) { GCMIntentService.currentActivity = null; } super.onPause(); } 

    O que funcionou paira mim:

    Crie uma Constantes de Classe final, dentro dela, crie vairaiable static:

     public final class Constants{ public static AppCompatActivity mCurrentActivity; } 

    Agora, em cada currículo de suas atividades diga:

      @Oviewride protected void onResume() { super.onResume(); Constants.mCurrentActivity = this; } 

    Quando receber a notificação, viewifique se a atividade atual é nula, se o seu aplicativo nulo, não for aberto, se a atividade não for nula, você pode viewificair coisas como:

     if(Constants.mCurrentActivity instanceof MainActivity){ ((MainActivity) Constants.mCurrentActivity).yourPublicMethodOrStaticObject; } 
    Android is Google's Open Mobile OS, Android APPs Developing is easy if you follow me.