当前位置:首页 > VUE

vue实现每日到点提醒

2026-02-24 01:08:57VUE

实现每日到点提醒的Vue方案

使用浏览器通知API

通过Notification API实现桌面通知功能,需先获取用户授权。在Vue组件中可封装如下逻辑:

export default {
  methods: {
    requestNotificationPermission() {
      if ('Notification' in window) {
        Notification.requestPermission().then(permission => {
          if (permission === 'granted') {
            this.scheduleDailyReminder();
          }
        });
      }
    },
    scheduleDailyReminder() {
      const targetHour = 9; // 设置提醒时间(24小时制)
      const now = new Date();
      const targetTime = new Date();

      targetTime.setHours(targetHour, 0, 0, 0);
      if (now > targetTime) {
        targetTime.setDate(targetTime.getDate() + 1);
      }

      const timeout = targetTime - now;
      setTimeout(() => {
        new Notification('每日提醒', { 
          body: '这是您的预定提醒内容',
          icon: '/notification-icon.png'
        });
        this.scheduleDailyReminder(); // 递归调用实现每日重复
      }, timeout);
    }
  },
  mounted() {
    this.requestNotificationPermission();
  }
}

结合本地存储记录状态

防止页面刷新后定时器失效,可使用localStorage保存状态:

// 在scheduleDailyReminder方法中添加
localStorage.setItem('nextReminderTime', targetTime.getTime());

// 组件初始化时检查已有定时
const storedTime = localStorage.getItem('nextReminderTime');
if (storedTime && new Date(parseInt(storedTime)) > new Date()) {
  const remainingTime = parseInt(storedTime) - Date.now();
  setTimeout(() => {
    this.triggerNotification();
    this.scheduleDailyReminder();
  }, remainingTime);
}

使用Web Worker保持后台运行

解决浏览器标签页休眠导致定时器不准确的问题:

// worker.js
self.onmessage = function(e) {
  if (e.data === 'start') {
    const targetHour = 9;
    setInterval(() => {
      const now = new Date();
      if (now.getHours() === targetHour && now.getMinutes() === 0) {
        self.postMessage('remind');
      }
    }, 60000); // 每分钟检查一次
  }
};

// Vue组件中
const worker = new Worker('worker.js');
worker.onmessage = (e) => {
  if (e.data === 'remind' && Notification.permission === 'granted') {
    new Notification('定时提醒触发');
  }
};
worker.postMessage('start');

完整组件示例

整合上述功能的单文件组件:

<template>
  <div>
    <button @click="requestPermission">启用每日提醒</button>
    <p>当前提醒时间:每天{{ reminderTime }}点</p>
  </div>
</template>

<script>
export default {
  data() {
    return {
      reminderTime: 9
    }
  },
  methods: {
    requestPermission() {
      if ('Notification' in window) {
        Notification.requestPermission().then(permission => {
          if (permission === 'granted') {
            this.initReminderSystem();
          }
        });
      } else {
        alert('该浏览器不支持通知功能');
      }
    },
    initReminderSystem() {
      if ('serviceWorker' in navigator) {
        navigator.serviceWorker.register('/sw.js').then(() => {
          this.scheduleViaServiceWorker();
        });
      } else {
        this.scheduleViaTimeout();
      }
    },
    scheduleViaTimeout() {
      // ...前述setTimeout实现
    },
    scheduleViaServiceWorker() {
      // 更可靠的后台提醒方案
    }
  }
}
</script>

注意事项

  • 通知API需要HTTPS环境或localhost才能正常工作
  • 移动端浏览器对后台运行的限制较多,建议配合Service Worker实现
  • 首次使用需要用户主动授权,建议在合适的用户交互场景触发权限请求
  • 考虑添加关闭提醒的功能开关,提升用户体验

扩展方案

对于更复杂的提醒需求,可考虑:

vue实现每日到点提醒

  • 集成IndexedDB存储提醒配置
  • 使用第三方推送服务如Firebase Cloud Messaging
  • 开发浏览器扩展程序获得更稳定的后台权限

标签: vue
分享给朋友:

相关文章

vue实现用户信息删除

vue实现用户信息删除

Vue 实现用户信息删除功能 在 Vue 中实现用户信息删除功能通常涉及前端界面交互和后端 API 调用。以下是实现步骤: 前端界面设计 创建删除按钮或操作项,通常使用 @click 事件触发删除操…

vue 实现遮罩

vue 实现遮罩

Vue 实现遮罩层的方法 使用固定定位和透明背景 在Vue中实现遮罩层可以通过CSS固定定位结合透明背景色完成。创建一个全屏遮罩组件,利用position: fixed覆盖整个视窗。 <tem…

vue实现tabs

vue实现tabs

Vue实现Tabs组件的方法 使用动态组件和v-for指令 在Vue中创建Tabs组件可以通过动态组件和v-for指令实现。定义一个包含tab标题和内容的数组,使用v-for渲染tab标题,并通过点击…

vue实现listview

vue实现listview

Vue 实现 ListView 的方法 基础列表渲染 使用 v-for 指令可以轻松实现基础列表渲染。在 Vue 中,v-for 用于遍历数组或对象,生成动态列表。 <template>…

vue 实现循环

vue 实现循环

Vue 实现循环的方法 在 Vue 中,可以使用 v-for 指令来实现循环渲染列表数据。以下是几种常见的循环实现方式: 基本数组循环 <template> <ul>…

实现简单vue

实现简单vue

实现一个简单的 Vue.js 框架 Vue.js 的核心功能包括数据绑定、响应式系统和虚拟 DOM。以下是一个简化版的 Vue 实现,涵盖基本功能。 数据响应式系统 使用 Object.define…