Check if app is open during a GCM onMessage event?

后端 未结 4 1201
长发绾君心
长发绾君心 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条回答
  • What I always do is have a reference to the current Activity.

    I set the current Activity in every onResume to this and set it to null in every onPause.

    If the current Activity is null then the app is not open. If it's not null you can see if the correct Activity is open and deliver it to that Activity.

    GCMIntentService:

    public static Activity currentActivity;
    public static final Object CURRENTACTIVIYLOCK = new Object();
    
        @Override
    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
            }
        }
    }
    

    CorrectActivity:

    @Override
        protected void onResume() {
            synchronized (GCMIntentService.CURRENTACTIVITYLOCK) {
                    GCMIntentService.currentActivity = this;
                }
            }
            super.onResume();
        }
    
    @Override
        protected void onPause() {
            synchronized (GCMIntentService.CURRENTACTIVITYLOCK) {
                GCMIntentService.currentActivity = null;
            }
            super.onPause();
        }
    
    0 讨论(0)
  • 2020-12-16 02:55

    Use SharedPreferences saving the boolean isVisible, and when you get the value from the preference you can add a default value.

    SharedPreferences settings = context.getSharedPreferences("NAME_XXX", Activity.MODE_PRIVATE);
    settings.getBoolean("visible", false);
    
    0 讨论(0)
  • 2020-12-16 03:08

    I would use order broadcasts to do that.

    In your onMessage method:

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

    In your Activity:

    public class YourActivity extends Activity {
    
        final BroadcastReceiver mBroadcastReceiver = new BroadcastReceiver() {
    
            @Override
            public void onReceive(Context context, Intent intent) {
                //Right here do what you want in your activity
                abortBroadcast();
            }
        };
    
        @Override
        public void onCreate(Bundle savedInstanceState) {
            super.onCreate(savedInstanceState);
            setContentView(R.layout.main);
    
            //.....
        }
    
        @Override
        protected void onPause() {
            unregisterReceiver(mBroadcastReceiver);
            super.onPause();
        }
    
        @Override
        protected void onResume() {
            IntentFilter filter = new IntentFilter("com.yourpackage.GOT_PUSH");
            filter.setPriority(2);
            registerReceiver(mBroadcastReceiver, filter);
            super.onResume();
        }
    }
    

    The other BroadcastReceiver

    public class SecondReceiver extends BroadcastReceiver {
    
        @Override
        public void onReceive(Context context, Intent intent) {
            //In this receiver just send your notification
        }
    
    }
    

    Manifest:

    <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>
    
    <receiver
        android:name=".SecondReceiver">
        <intent-filter
            android:priority="1">
            <action
                android:name="com.yourpackage.GOT_PUSH" />
        </intent-filter>
    </receiver>
    

    Basically in the onMessage method you send an Intent which is first received by the BroadcastReceiver registered inside YourActivity if it is running and in foreground, otherwise it is received by the SecondReceiver.

    0 讨论(0)
  • 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;
    }
    
    0 讨论(0)
提交回复
热议问题