即使该应用已关闭,如何发送通知?

时间:2018-07-18 20:56:46

标签: android push-notification android-notifications

我想制作一个应用程序,您可以使用开关选择一周中的某一天,并在那一天用户收到通知。

我在此处阅读了一些相关信息,但频道更新后,我不确定它们是否有效。

我有些困惑,所以我没有很多代码:

 public class MainActivity extends AppCompatActivity {

    Switch segSwitch;
    Switch terSwitch;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);

        segSwitch = findViewById(R.id.switch1);
        terSwitch = findViewById(R.id.switch2);

        Calendar calendar = Calendar.getInstance();

        int day = calendar.get(Calendar.DAY_OF_WEEK);

        switch (day) {

            case Calendar.MONDAY:
                notificate(segSwitch);

            case Calendar.TUESDAY:
                notificate(terSwitch);
        }
    }

    public void notificate(View view) {

        Switch mySwitch = (Switch) view;

        if (mySwitch.isChecked())
        {
            Toast.makeText(this, "Time to put the garbage out!", Toast.LENGTH_LONG).show();
        }

    }
}

1 个答案:

答案 0 :(得分:0)

您可以使用正常的Android通知(https://developer.android.com/guide/topics/ui/notifiers/notificationshttps://developer.android.com/training/notify-user/build-notification)在APp关闭时通知信息,也可以通过创建自定义视图(通过夸大自定义的“ R.layout” .xxx“),并在屏幕上的某个位置显示此自定义视图,而无需创建全屏活动(请注意,这种视图/窗口具有活动没有的一些限制:没有OnStart()/ OnPause() / OnBackPressed()/ etc ...事件):

final WindowManager cWindowManager = ((WindowManager)context.getSystemService(Context.WINDOW_SERVICE));
    mPopupView = mInflater.inflate(cLayoutResID, null);
    mPopupView.setLayerType(View.LAYER_TYPE_HARDWARE, null);
    final WindowManager.LayoutParams params = new WindowManager.LayoutParams(
            width,
            height,
            WindowManager.LayoutParams.TYPE_PHONE,
            WindowManager.LayoutParams.FLAG_HARDWARE_ACCELERATED | WindowManager.LayoutParams.FLAG_LAYOUT_IN_SCREEN | WindowManager.LayoutParams.FLAG_FULLSCREEN,
            PixelFormat.TRANSLUCENT);
    params.gravity = Gravity.START | Gravity.TOP;
    params.flags |= WindowManager.LayoutParams.FLAG_WATCH_OUTSIDE_TOUCH;
    params.x = 0;
    params.y = 0;
cWindowManager.addView(mPopupView, params);