GCM How do I detect if app is open and if so pop up an alert box, instead of normal notification flow?

后端 未结 4 979
猫巷女王i
猫巷女王i 2021-02-03 13:47

I have an app where I want to build 2 different flow\'s in:

  • 1.a App is open on any activity
  • 1.b App show\'s an alertbox where user can choose to go to

4条回答
  •  夕颜
    夕颜 (楼主)
    2021-02-03 14:27

    I'd just write another BroadcastReceiver like this:

    public class MainActivityBroadcastReceiver extends BroadcastReceiver {
    
        public MainActivityBroadcastReceiver(Activity mainActivity) {
    
        }
    
        @Override
        public void onReceive(Context context, Intent intent) {
            Toast toast = Toast.makeText(context, "hola", Toast.LENGTH_LONG);
            toast.show();   
            abortBroadcast();
        }
    
    }
    

    and then in the MainActivity I'd override the methods and then I don't receive the notification so I intercept the `Intent~ and abort the other broadcast:

    @Override
    protected void onPause() {
        unregisterReceiver(mainActivityBroadcastReceiver);
        super.onPause();
    }
    
    @Override
    public void onResume() {
        IntentFilter filter = new IntentFilter("com.google.android.c2dm.intent.RECEIVE");
        filter.setPriority(1);
        registerReceiver(mainActivityBroadcastReceiver, filter);
    
        super.onResume();
    }
    

提交回复
热议问题