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

使用docker在3台服务器上搭建基于redis 6.x的一主两从三台均是哨兵模式

一、环境及版本说明 如果服务器已经安装了docker,则忽略此步骤,如果没有安装,则可以按照一下方式安装: 1. 在线安装(有互联网环境): 请看我这篇文章 传送阵>> 点我查看 2. 离线安装(内网环境):请看我这篇文章 传送阵>> 点我查看 说明&#xff1a;假设每台服务器已…...

Lombok 的 @Data 注解失效,未生成 getter/setter 方法引发的HTTP 406 错误

HTTP 状态码 406 (Not Acceptable) 和 500 (Internal Server Error) 是两类完全不同的错误&#xff0c;它们的含义、原因和解决方法都有显著区别。以下是详细对比&#xff1a; 1. HTTP 406 (Not Acceptable) 含义&#xff1a; 客户端请求的内容类型与服务器支持的内容类型不匹…...

在鸿蒙HarmonyOS 5中实现抖音风格的点赞功能

下面我将详细介绍如何使用HarmonyOS SDK在HarmonyOS 5中实现类似抖音的点赞功能&#xff0c;包括动画效果、数据同步和交互优化。 1. 基础点赞功能实现 1.1 创建数据模型 // VideoModel.ets export class VideoModel {id: string "";title: string ""…...

从零实现STL哈希容器:unordered_map/unordered_set封装详解

本篇文章是对C学习的STL哈希容器自主实现部分的学习分享 希望也能为你带来些帮助~ 那咱们废话不多说&#xff0c;直接开始吧&#xff01; 一、源码结构分析 1. SGISTL30实现剖析 // hash_set核心结构 template <class Value, class HashFcn, ...> class hash_set {ty…...

vue3+vite项目中使用.env文件环境变量方法

vue3vite项目中使用.env文件环境变量方法 .env文件作用命名规则常用的配置项示例使用方法注意事项在vite.config.js文件中读取环境变量方法 .env文件作用 .env 文件用于定义环境变量&#xff0c;这些变量可以在项目中通过 import.meta.env 进行访问。Vite 会自动加载这些环境变…...

在QWebEngineView上实现鼠标、触摸等事件捕获的解决方案

这个问题我看其他博主也写了&#xff0c;要么要会员、要么写的乱七八糟。这里我整理一下&#xff0c;把问题说清楚并且给出代码&#xff0c;拿去用就行&#xff0c;照着葫芦画瓢。 问题 在继承QWebEngineView后&#xff0c;重写mousePressEvent或event函数无法捕获鼠标按下事…...

Mysql中select查询语句的执行过程

目录 1、介绍 1.1、组件介绍 1.2、Sql执行顺序 2、执行流程 2.1. 连接与认证 2.2. 查询缓存 2.3. 语法解析&#xff08;Parser&#xff09; 2.4、执行sql 1. 预处理&#xff08;Preprocessor&#xff09; 2. 查询优化器&#xff08;Optimizer&#xff09; 3. 执行器…...

JVM 内存结构 详解

内存结构 运行时数据区&#xff1a; Java虚拟机在运行Java程序过程中管理的内存区域。 程序计数器&#xff1a; ​ 线程私有&#xff0c;程序控制流的指示器&#xff0c;分支、循环、跳转、异常处理、线程恢复等基础功能都依赖这个计数器完成。 ​ 每个线程都有一个程序计数…...

现有的 Redis 分布式锁库(如 Redisson)提供了哪些便利?

现有的 Redis 分布式锁库&#xff08;如 Redisson&#xff09;相比于开发者自己基于 Redis 命令&#xff08;如 SETNX, EXPIRE, DEL&#xff09;手动实现分布式锁&#xff0c;提供了巨大的便利性和健壮性。主要体现在以下几个方面&#xff1a; 原子性保证 (Atomicity)&#xff…...

Java数值运算常见陷阱与规避方法

整数除法中的舍入问题 问题现象 当开发者预期进行浮点除法却误用整数除法时,会出现小数部分被截断的情况。典型错误模式如下: void process(int value) {double half = value / 2; // 整数除法导致截断// 使用half变量 }此时...