Check if app is open during a GCM onMessage event?

后端 未结 4 1207
长发绾君心
长发绾君心 2020-12-16 02:45

I am wondering how to check if my application is open and currently visible to the user when receiving an onMessage() from GCM. At first, I was just using my ow

4条回答
  •  陌清茗
    陌清茗 (楼主)
    2020-12-16 03:15

    The thing that worked for me:

    Create a final Class Constants, inside it, create static varaiable:

    public final class Constants{
    public static AppCompatActivity mCurrentActivity;
    }
    

    Now, on each on resume of your activties say:

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

    When receieving notification, check if current activity is null, if its null, application is not opened, if activity isn't null, you can check things like:

    if(Constants.mCurrentActivity instanceof MainActivity){
       ((MainActivity) Constants.mCurrentActivity).yourPublicMethodOrStaticObject;
    }
    

提交回复
热议问题