如何使用工作管理器在以后显示通知

时间:2018-08-22 17:51:22

标签: java android scheduled-tasks android-workmanager

我正在尝试了解如何在特定时间显示通知,我了解可以为此目的使用workmanager,并且它将选择正确的方法来完成工作(在这种情况下,显示通知)如果应用已关闭,则设备会重置等。

现在我有一个安排通知时间的测试方法,看来如果应用程序正在运行,而作业应该运行,那么一切正常,但是如果我的应用程序在此之前被杀死,那么直到我再次打开应用程序时,通知才会显示

我想了解为什么在关闭应用程序时不显示通知,以及即使关闭应用程序也如何使工作经理运行该工作。

工作人员:

public class WorkerNext extends Worker {

public Result doWork() {
        Result workResult=Result.FAILURE;
        int workType=getInputData().getInt(DATA_KEY_WORK_TYPE,WORK_TYPE1);
        switch (workType)
        {
            case WORK_TYPE1:
                workResult=doWorkUpdate();
                break;
            case WORK_TYPE_SHOW_NOTIFICATION:
                workResult=doWorkShowNotification();
                break;
            default:
                workResult=doWorkUpdate();
                break;
        }
        return workResult;
    }

private Result doWorkShowNotification() {
        showHagralaDetailsNotification();
        scheduleNotifications();
        return Result.SUCCESS;
    }

    public void showHagralaDetailsNotification()
    {
        Context context=MyApp.getApplication().getApplicationContext();
        Notification.Builder builder = new Notification.Builder(context)
                .setContentTitle(context.getString(R.string.Notification_NextHagralaTitle))
                .setContentText(NextHagralaDetails.toNotificationText())
                .setAutoCancel(true)
                .setSmallIcon(R.mipmap.ic_launcher_foreground)
                .setLargeIcon(((BitmapDrawable) context.getResources().getDrawable(R.mipmap.ic_launcher)).getBitmap())
                .setSound(RingtoneManager.getDefaultUri(RingtoneManager.TYPE_NOTIFICATION));

        Notification notification = builder.getNotification();
        NotificationManager manager=(NotificationManager) context.getSystemService(Context.NOTIFICATION_SERVICE);
        manager.notify(NOTIFICATION_ID_HAGRALA_DETAILS,notification);
    }

private void scheduleNotifications()
    {
        long delay=calculateNextNotificationsDelay();
        if (delay!=MyConfig.ALERT_NONE)
        {
            ExistingWorkPolicy workPolicy=ExistingWorkPolicy.REPLACE;

            Data data=new Data.Builder().putInt(WorkerNextHagrala.DATA_KEY_WORK_TYPE,
                    WorkerNextHagrala.WORK_TYPE_SHOW_NOTIFICATION).build();
            Log.d("GABI","scheduleUpdateNextNotifications - delay:"+delay);
            OneTimeWorkRequest request=new OneTimeWorkRequest.Builder(WorkerNextHagrala.class).
                    setInitialDelay(delay, TimeUnit.MILLISECONDS).setInputData(data).build();
            WorkManager.getInstance().beginUniqueWork(WORK_NAME_SHOW_NOTIFICATION,workPolicy ,request).enqueue();
        }
    }
}

测试方法-(从现在开始1分钟的时间表通知):

public void showNotification(View view) {
        long delay=60000;
        if (delay!=MyConfig.ALERT_NONE)
        {
            ExistingWorkPolicy workPolicy=ExistingWorkPolicy.REPLACE;

            Data data=new Data.Builder().putInt(WorkerNextHagrala.DATA_KEY_WORK_TYPE,
                    WorkerNextHagrala.WORK_TYPE_SHOW_NOTIFICATION).build();
            Log.d("GABI","scheduleUpdateNextNotifications - delay:"+delay);
            OneTimeWorkRequest request=new OneTimeWorkRequest.Builder(WorkerNextHagrala.class).
                    setInitialDelay(delay, TimeUnit.MILLISECONDS).setInputData(data).build();
            WorkManager.getInstance().beginUniqueWork(WorkerNextHagrala.WORK_NAME_SHOW_NOTIFICATION,workPolicy ,request).enqueue();
        }
    }

0 个答案:

没有答案