应用关闭后,Firebase网络调度程序停止工作。但GCM网络管理器完美地适用于上述场景

时间:2018-05-07 12:36:42

标签: android firebase gcm-network-manager

我有一个场景,即使应用程序关闭,我也必须听取XMPP连接状态。现在我正在使用Firebase网络调度程序,因为GCM网络管理器已被弃用,它仅在应用程序处于前台/后台时才有效。只要应用程序关闭,它就会停止。有人可以提出建议吗?以下是我的代码段。

    Driver driver = new GooglePlayDriver(getApplicationContext());
    FirebaseJobDispatcher firebaseJobDispatcher = new FirebaseJobDispatcher(driver);

    Job constraintReminderJob = firebaseJobDispatcher.newJobBuilder()
            .setService(FirebaseService.class)
            .setTag("Notify Tag")
            .setLifetime(Lifetime.UNTIL_NEXT_BOOT)
            .build();
    firebaseJobDispatcher.schedule(constraintReminderJob);

1 个答案:

答案 0 :(得分:0)

Driver driver = new GooglePlayDriver(context);
        FirebaseJobDispatcher firebaseJobDispatcher = new FirebaseJobDispatcher(driver);

        Job constraintReminderJob = firebaseJobDispatcher.newJobBuilder()
                .setService(ReminderService.class)
                .setTag(REMINDER_JOB_TAG)
                .setConstraints(Constraint.DEVICE_CHARGING)
                .setLifetime(Lifetime.FOREVER)
                .setRecurring(true)
                .setTrigger(Trigger.executionWindow(
                        REMINDER_INTERVAL_SECONDS,
                        REMINDER_INTERVAL_SECONDS + SYNC_FLEXTIME_SECONDS
                ))
                .setReplaceCurrent(true)
                .build();

        firebaseJobDispatcher.schedule(constraintReminderJob);