当前位置: 首页 > 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转换、重复项或视频格式。 使用强大的…...

Cilium动手实验室: 精通之旅---20.Isovalent Enterprise for Cilium: Zero Trust Visibility

Cilium动手实验室: 精通之旅---20.Isovalent Enterprise for Cilium: Zero Trust Visibility 1. 实验室环境1.1 实验室环境1.2 小测试 2. The Endor System2.1 部署应用2.2 检查现有策略 3. Cilium 策略实体3.1 创建 allow-all 网络策略3.2 在 Hubble CLI 中验证网络策略源3.3 …...

鸿蒙中用HarmonyOS SDK应用服务 HarmonyOS5开发一个医院查看报告小程序

一、开发环境准备 ​​工具安装​​&#xff1a; 下载安装DevEco Studio 4.0&#xff08;支持HarmonyOS 5&#xff09;配置HarmonyOS SDK 5.0确保Node.js版本≥14 ​​项目初始化​​&#xff1a; ohpm init harmony/hospital-report-app 二、核心功能模块实现 1. 报告列表…...

WEB3全栈开发——面试专业技能点P2智能合约开发(Solidity)

一、Solidity合约开发 下面是 Solidity 合约开发 的概念、代码示例及讲解&#xff0c;适合用作学习或写简历项目背景说明。 &#x1f9e0; 一、概念简介&#xff1a;Solidity 合约开发 Solidity 是一种专门为 以太坊&#xff08;Ethereum&#xff09;平台编写智能合约的高级编…...

Java入门学习详细版(一)

大家好&#xff0c;Java 学习是一个系统学习的过程&#xff0c;核心原则就是“理论 实践 坚持”&#xff0c;并且需循序渐进&#xff0c;不可过于着急&#xff0c;本篇文章推出的这份详细入门学习资料将带大家从零基础开始&#xff0c;逐步掌握 Java 的核心概念和编程技能。 …...

USB Over IP专用硬件的5个特点

USB over IP技术通过将USB协议数据封装在标准TCP/IP网络数据包中&#xff0c;从根本上改变了USB连接。这允许客户端通过局域网或广域网远程访问和控制物理连接到服务器的USB设备&#xff08;如专用硬件设备&#xff09;&#xff0c;从而消除了直接物理连接的需要。USB over IP的…...

JavaScript基础-API 和 Web API

在学习JavaScript的过程中&#xff0c;理解API&#xff08;应用程序接口&#xff09;和Web API的概念及其应用是非常重要的。这些工具极大地扩展了JavaScript的功能&#xff0c;使得开发者能够创建出功能丰富、交互性强的Web应用程序。本文将深入探讨JavaScript中的API与Web AP…...

JS手写代码篇----使用Promise封装AJAX请求

15、使用Promise封装AJAX请求 promise就有reject和resolve了&#xff0c;就不必写成功和失败的回调函数了 const BASEURL ./手写ajax/test.jsonfunction promiseAjax() {return new Promise((resolve, reject) > {const xhr new XMLHttpRequest();xhr.open("get&quo…...

Git 3天2K星标:Datawhale 的 Happy-LLM 项目介绍(附教程)

引言 在人工智能飞速发展的今天&#xff0c;大语言模型&#xff08;Large Language Models, LLMs&#xff09;已成为技术领域的焦点。从智能写作到代码生成&#xff0c;LLM 的应用场景不断扩展&#xff0c;深刻改变了我们的工作和生活方式。然而&#xff0c;理解这些模型的内部…...

32单片机——基本定时器

STM32F103有众多的定时器&#xff0c;其中包括2个基本定时器&#xff08;TIM6和TIM7&#xff09;、4个通用定时器&#xff08;TIM2~TIM5&#xff09;、2个高级控制定时器&#xff08;TIM1和TIM8&#xff09;&#xff0c;这些定时器彼此完全独立&#xff0c;不共享任何资源 1、定…...

yaml读取写入常见错误 (‘cannot represent an object‘, 117)

错误一&#xff1a;yaml.representer.RepresenterError: (‘cannot represent an object’, 117) 出现这个问题一直没找到原因&#xff0c;后面把yaml.safe_dump直接替换成yaml.dump&#xff0c;确实能保存&#xff0c;但出现乱码&#xff1a; 放弃yaml.dump&#xff0c;又切…...