当前位置: 首页 > news >正文

Flutter开发之flutter_local_notifications

flutter_local_notifications 消息通知

flutter_local_notifications地址

flutter_local_notifications: ^18.0.1
class NotificationHelper {//工厂模式调用该类时,默认调用此方法,将实例对象返回出去static NotificationHelper? _instance = null;static NotificationHelper getInstance() {_instance ??= NotificationHelper._initial();return _instance!;}factory NotificationHelper() => _instance ??= NotificationHelper._initial();//创建命名构造函数NotificationHelper._initial() {initialize();}// FlutterLocalNotificationsPlugin实例final FlutterLocalNotificationsPlugin _notificationsPlugin =FlutterLocalNotificationsPlugin();// 常量定义static const String _channelId = 'your.channel.id';static const String _channelName = 'your channel name';static const String _channelDescription = 'your channel description';static const String _ticker = 'ticker';static const String _darwinNotificationCategoryPlain = 'plainCategory';// 初始化通知插件Future<void> initialize() async {try {final AndroidInitializationSettings initializationSettingsAndroid =AndroidInitializationSettings('@mipmap/ic_launcher');final DarwinInitializationSettings initializationSettingsIOS =DarwinInitializationSettings();final InitializationSettings initializationSettings =InitializationSettings(android: initializationSettingsAndroid,iOS: initializationSettingsIOS);await _notificationsPlugin.initialize(initializationSettings);} catch (e) {print('初始化通知插件失败: $e');}}Future<void> requestNotificationPermissions() async {if (await Permission.notification.isDenied) {final status = await Permission.notification.request();final status1 = await Permission.scheduleExactAlarm.request();LogUtils.d("requestNotificationPermissions :通知权限status1 $status1");if (status.isGranted) {LogUtils.d("requestNotificationPermissions :通知权限已授予");print('通知权限已授予');} else {LogUtils.d("requestNotificationPermissions :通知权限被拒绝");print('通知权限被拒绝');}} else {LogUtils.d("requestNotificationPermissions :通知权限已授予");print('通知权限已授予');}}// 显示通知Future<void> showNotification({required String title, required String body}) async {try {final AndroidNotificationDetails androidNotificationDetails =AndroidNotificationDetails(_channelId, _channelName,channelDescription: _channelDescription,importance: Importance.max,priority: Priority.high,ticker: _ticker);final DarwinNotificationDetails iosNotificationDetails =DarwinNotificationDetails(categoryIdentifier: _darwinNotificationCategoryPlain);final NotificationDetails platformChannelSpecifics =NotificationDetails(android: androidNotificationDetails, iOS: iosNotificationDetails);await _notificationsPlugin.show(1,title,body,platformChannelSpecifics,);} catch (e) {print('显示通知失败: $e');}}// 周期性通知Future<void> scheduleNotification({required int id,required String title,required String body,}) async {const AndroidNotificationDetails androidNotificationDetails =AndroidNotificationDetails('your.channel.id', 'your channel name',channelDescription: 'your channel description',importance: Importance.max,priority: Priority.high,ticker: 'ticker');// ios的通知const String darwinNotificationCategoryPlain = 'plainCategory';const DarwinNotificationDetails iosNotificationDetails =DarwinNotificationDetails(categoryIdentifier: darwinNotificationCategoryPlain, // 通知分类);// 创建跨平台通知const NotificationDetails platformChannelSpecifics = NotificationDetails(android: androidNotificationDetails, iOS: iosNotificationDetails);
// 发起通知await _notificationsPlugin.periodicallyShow(id, title, body, RepeatInterval.everyMinute, platformChannelSpecifics);}// 定时通知Future<void> zonedScheduleNotification({required int id,required String title,required String body,required DateTime scheduledDateTime}) async {const AndroidNotificationDetails androidNotificationDetails =AndroidNotificationDetails('10001', '唤醒',channelDescription: 'your channel description',importance: Importance.max,priority: Priority.high,ticker: 'ticker');// ios的通知const String darwinNotificationCategoryPlain = 'plainCategory';const DarwinNotificationDetails iosNotificationDetails =DarwinNotificationDetails(categoryIdentifier: darwinNotificationCategoryPlain, // 通知分类);// 创建跨平台通知const NotificationDetails platformChannelSpecifics = NotificationDetails(android: androidNotificationDetails, iOS: iosNotificationDetails);// 获取本地时区final location = tz.getLocation(tz.local.name);// 发起通知_notificationsPlugin.zonedSchedule(id, title, body,TZDateTime.from(scheduledDateTime, location), // 使用本地时区的时间platformChannelSpecifics,uiLocalNotificationDateInterpretation:UILocalNotificationDateInterpretation.wallClockTime, // 设置通知的触发时间是觉得时间);}/// 取消全部通知cancelAll(){_notificationsPlugin.cancelAll();}/// 取消对应ID的通知cancelId(int id){_notificationsPlugin.cancel(id);}
}

使用步骤:

0.请求通知权限
android
manifest.xml

<uses-permission android:name="com.android.alarm.permission.SET_ALARM"/>
<uses-permission android:name="android.permission.SCHEDULE_EXACT_ALARM" />

ios
Info.plist

<key>NSUserNotificationAlertIdentifier</key>
<string>我们需要您的许可来发送通知</string>
<key>NSUserNotificationAlertTitle</key>
<string>请求通知权限</string>
<key>NSUserNotificationAlertBody</key>
<string>我们希望能够在您允许的情况下发送通知。</string>

使用之前一定要代码里边获取通知权限

NotificationHelper.getInstance().requestNotificationPermissions();

1.初始化

NotificationHelper.getInstance().initialize();

2.使用

NotificationHelper.getInstance().zonedScheduleNotification(id: 10001, title: "科学研究", body: "研究开始了", scheduledDateTime: DateUtilss.getNowDateMs15or25DateTime(15));

相关文章:

Flutter开发之flutter_local_notifications

flutter_local_notifications 消息通知 flutter_local_notifications地址 flutter_local_notifications: ^18.0.1class NotificationHelper {//工厂模式调用该类时&#xff0c;默认调用此方法&#xff0c;将实例对象返回出去static NotificationHelper? _instance null;sta…...

Gradle和maven

大家好&#xff0c;我是风筝 作为Java 开发者&#xff0c;你平时用 Maven 还是 Gradle&#xff1f; 我一直用的都是 Maven&#xff0c;但是前几天做了一个小项目&#xff0c;用的是 Gradle&#xff0c;因为项目创建出来默认就是用的 Gradle&#xff0c;而且功能足够简单&#x…...

RabbitMQ教程:发布/订阅模式(Publish/Subscribe)(三)

文章目录 RabbitMQ教程&#xff1a;发布/订阅模式&#xff08;Publish/Subscribe&#xff09;&#xff08;三&#xff09;一、引言二、简介三、准备工作3.1 说明3.2 生成项目 四、实战4.1 交换机&#xff08;Exchanges&#xff09;4.2 临时队列&#xff08;Temporary Queues&am…...

服务器被挂马怎么办?——解决服务器被挂马的方法和步骤

服务器被挂马&#xff08;即被植入恶意软件&#xff09;是一个常见的网络安全问题&#xff0c;可能导致数据泄露、服务中断和经济损失。本文将详细介绍如何检测和清除服务器上的恶意软件&#xff0c;并提供实用的代码示例&#xff0c;帮助读者解决服务器被挂马的问题。 一、什…...

Qt 项目架构设计

在开发一个 Qt 项目时&#xff0c;合理的文件夹结构和清晰的构建流程是非常重要的。Qt 项目通常需要管理源代码、UI 文件、资源文件、构建脚本等。下面我会给出一个详细的文件夹结构示例&#xff0c;并解释每个部分的作用及如何设计 Makefile 或使用 Qt 的 qmake 来自动化构建过…...

Elasticsearch:管理和排除 Elasticsearch 内存故障

作者&#xff1a;来自 Elastic Stef Nestor 随着 Elastic Cloud 提供可观察性、安全性和搜索等解决方案&#xff0c;我们将使用 Elastic Cloud 的用户范围从完整的运营团队扩大到包括数据工程师、安全团队和顾问。作为 Elastic 支持代表&#xff0c;我很乐意与各种各样的用户和…...

高级java每日一道面试题-2024年11月07日-Redis篇-Redis有哪些功能?

如果有遗漏,评论区告诉我进行补充 面试官: Redis有哪些功能? 我回答: Redis 是一个开源的、基于键值对的 NoSQL 数据库&#xff0c;以其高性能、丰富的数据结构和多种功能而闻名。在高级 Java 面试中&#xff0c;了解 Redis 的核心功能和高级特性是非常重要的。以下是 Redi…...

实用且免费的 IP 地域查询 API 接口推荐

实用且免费的 IP 地域查询 API 接口推荐 在日常开发中&#xff0c;IP 地域查询是一个常见需求。最近无意间发现一个实用的 IP 地域查询 API&#xff0c;目前是免费的&#xff0c;未来是否收费尚不可知&#xff0c;但在当前情况下非常值得推荐。 API 地址示例&#xff1a; ht…...

STM32学习笔记----SPI协议

STM32的SPI&#xff08;串行外设接口&#xff0c;Serial Peripheral Interface&#xff09;是一种常见的同步串行通信协议&#xff0c;广泛应用于与传感器、显示屏、存储设备等外设的通信。SPI通过主从模式&#xff08;Master/Slave&#xff09;来实现数据交换&#xff0c;其中…...

Ceph的pool有两种类型

Replicated Pool&#xff08;拷贝型Pool&#xff0c;默认&#xff09; 概述&#xff1a; 这是Ceph的默认存储池类型。它通过生成对象的多份拷贝来确保数据的冗余和高可用性。 工作原理&#xff1a; 每个存入的对象&#xff08;Object&#xff09;都会被存储为多个副本&#xf…...

推荐一款流程图和图表绘制工具:WizFlow Flowcharter Pro

WizFlow Flowcharter是一款易于使用、功能丰富的Windows流程图和图表绘制工具。它允许用户使用超过一百种预定义的形状和箭头定义形状“样式”。您可以将自己的样式保存在图表模板中&#xff0c;以建立自己的绘图方法。WizFlow附带了完整的流程图模板&#xff0c;以帮助您入门。…...

设计模式之插件模式

插件模式是一种设计模式,可以让您在不修改现有系统代码的情况下扩展功能,非常适合实现监控软件和交换机配置的解耦。在嵌入式Linux系统中,您可以使用C++实现插件机制,使监控软件能够动态加载交换机型号的配置模块。这种方式允许您通过插件形式快速适配新型号的交换机。 插…...

深度学习基础—Beam search集束搜索

引言 深度学习基础—Seq2Seq模型https://blog.csdn.net/sniper_fandc/article/details/143781223?fromshareblogdetail&sharetypeblogdetail&sharerId143781223&sharereferPC&sharesourcesniper_fandc&sharefromfrom_link 上篇博客讲到&#xff0c;贪心算…...

STM32 串口输出调试信息

软硬件信息 CubeMX version 6.12.1Keil uVision V5.41.0.0 注意 串口有多种&#xff1a; TTL232485 串口的相关知识&#xff1a; 01-【HAL库】STM32实现串口打印&#xff08;printf方式) &#xff0c; 内含 TTL 和 232 区别。 我把 232 串口连进 STM32 串口助手收到的信息…...

任务调度中心-XXL-JOB使用详解

目录 详解 调度中心 执行器 原理 快速入门 源码仓库地址 1.初始化数据库 2.配置调度中心 1.解压源码 2.需改配置文件 3.启动调度中心 3.配置执行器 1.引入pom依赖 2.修改配置文件 3.执行器组件配置 4.部署执行器项目 4.开发第一个任务 BEAN模式&#xff08;类…...

git本地分支推送到远程和远程pull到本地

文章目录 本地分支推送到远程仓库git拉取远程分支到本地 本地分支推送到远程仓库 要将本地分支推送到远程仓库的某个分支&#xff08;可以是同名的分支&#xff0c;也可以是不同名的分支&#xff09;&#xff0c;你可以使用 git push 命令。这里有几种不同的情况&#xff1a; …...

Python_爬虫1_Requests库入门

目录 Requests库 7个主要方法 Requests库的get()方法 Response对象的属性 爬取网页的通用代码框架 理解requests库的异常 HTTP协议及Requests库方法 HTTP协议 HTTP协议采用URL作为定位网络资源的标识。 HTTP协议对资源的操作 理解PATCH和PUT的区别 HTTP协议与Requse…...

安全见闻1-5

涵盖了编程语言、软件程序类型、操作系统、网络通讯、硬件设备、web前后端、脚本语言、病毒种类、服务器程序、人工智能等基本知识&#xff0c;有助于全面了解计算机科学和网络技术的各个方面。 安全见闻1 1.编程语言简要概述 C语言&#xff1a;面向过程&#xff0c;适用于系统…...

STM32 学习笔记-----STM32 的启动过程

STM32 的启动过程是一个精细而系统的流程&#xff0c;它涉及从芯片复位开始&#xff0c;到初始化系统、设置时钟、运行主程序等一系列步骤。下面详细介绍 STM32 启动过程的主要步骤。 1. Boot引脚设定 STM32 系列芯片有多个启动模式&#xff0c;这些模式是通过引脚&#xff0…...

35.3K+ Star!PhotoPrism:一款基于AI的开源照片管理工具

PhotoPrism 简介 PhotoPrism[1] 是一个为去中心化网络设计的AI照片应用,它利用最新技术自动标记和查找图片,实现自动图像分类与本地化部署,你可以在家中、私有服务器或云端运行它。 项目特点 主要特点 浏览所有照片和视频,无需担心RAW转换、重复项或视频格式。 使用强大的…...

Anthropic泄露新一代Claude Mythos 模型,具备网络安全漏洞检测优势

配置错误曝光新模型Anthropic PBC 内容管理系统的一处配置错误意外泄露了其正在测试的新型大语言模型 Claude Mythos。该公司周四向《财富》杂志证实&#xff0c;工程师已完成该模型的训练工作&#xff0c;目前正与早期客户进行试点测试。Anthropic 强调这是其"迄今为止构…...

Celery 入门与原理剖析:从使用到理解

在现代 Web 应用和后台系统中&#xff0c;异步任务处理是提升系统响应速度、解耦业务逻辑的关键技术。Celery 作为 Python 生态中最流行的分布式任务队列框架&#xff0c;因其简洁的 API 和强大的功能被广泛采用。本文将分为两部分&#xff1a;首先演示如何基于 Redis 快速上手…...

Midscene.js视觉驱动自动化:从认知到实践的AI跨平台控制指南

Midscene.js视觉驱动自动化&#xff1a;从认知到实践的AI跨平台控制指南 【免费下载链接】midscene Let AI be your browser operator. 项目地址: https://gitcode.com/GitHub_Trending/mid/midscene 一、认知篇&#xff1a;理解Midscene.js的技术革新 1.1 破解传统自动…...

QGC地面站视频流配置避坑指南:从Windows到Android,手把手解决‘无画面’问题

QGC地面站视频流配置避坑指南&#xff1a;从Windows到Android全平台实战解析 当无人机图传画面在QGroundControl中显示为黑屏时&#xff0c;那种挫败感每个飞手都深有体会。上周帮朋友调试一台Inspire 2时&#xff0c;我们花了整整三小时才找到问题根源——一个被忽略的GStream…...

跨平台QGIS二次开发环境实战:从源码编译到工程配置(QGIS 3.28 + Qt 5.15)

1. 跨平台QGIS开发环境全景概览 第一次接触QGIS二次开发的朋友可能会被复杂的依赖关系吓到&#xff0c;特别是当需要在不同操作系统上搭建环境时。我花了整整两周时间踩遍了Ubuntu和Windows平台的所有坑&#xff0c;最终总结出这套可复现的配置方案。QGIS作为开源GIS软件的标杆…...

【单片机实战】中断服务程序编写精要:从现场保护到中断返回

1. 中断服务程序的核心作用与基本结构 第一次接触单片机中断时&#xff0c;我盯着开发板上的按键发愣——明明没有循环检测IO口状态&#xff0c;按下按键却能立即触发LED亮灭。这种"随叫随到"的响应机制&#xff0c;就是中断服务程序&#xff08;ISR&#xff09;的魔…...

TripoSR:0.5秒单图像3D重建技术指南与实战应用

TripoSR&#xff1a;0.5秒单图像3D重建技术指南与实战应用 【免费下载链接】TripoSR 项目地址: https://gitcode.com/GitHub_Trending/tr/TripoSR 在3D内容创作领域&#xff0c;传统建模流程耗时耗力&#xff0c;而TripoSR作为开源3D重建模型&#xff0c;通过单张2D图像…...

从休眠到唤醒:深入解读AUTOSAR CanNm的Bus Load Reduction与Immediate Restart机制

从休眠到唤醒&#xff1a;深入解读AUTOSAR CanNm的Bus Load Reduction与Immediate Restart机制 在新能源汽车和智能座舱快速发展的今天&#xff0c;车载电子系统的功耗优化与实时响应能力成为工程师面临的核心挑战。AUTOSAR CanNm模块作为车载网络管理的关键组件&#xff0c;其…...

为什么92%的Spring Cloud Function项目仍在忍受秒级冷启动?这4个被忽视的Classloader陷阱必须立即修复

第一章&#xff1a;冷启动问题的云原生本质与量化归因冷启动并非单纯的应用延迟现象&#xff0c;而是云原生架构中资源按需供给、隔离边界强化与运行时环境动态构建三者耦合引发的系统性效应。其本质在于容器编排层&#xff08;如 Kubernetes&#xff09;与函数计算平台&#x…...

Alt App Installer:打破微软商店限制的Windows应用自由安装方案

Alt App Installer&#xff1a;打破微软商店限制的Windows应用自由安装方案 【免费下载链接】alt-app-installer A Program To Download And Install Microsoft Store Apps Without Store 项目地址: https://gitcode.com/gh_mirrors/alt/alt-app-installer 你是否曾经因…...