如何在flutter和web api中推送通知

如何解决如何在flutter和web api中推送通知

我希望程序能够每分钟向服务器发送一个请求并返回一个列表: 我的清单: [{"NoteId": "Id","Title": "Note Title","Description": "Note Description","AtDateTime": "1/7/2021 10:10:15"}]

如果 AtDateTime 字段的值等于当前时间,则显示带有列表记录值的警报

我使用一个节点js来连接数据。 请帮帮我

解决方法

我找到了一个定期在后台执行任务的解决方案。
但这只会每 15 分钟发生一次

对于计划任务需要添加包:workmanager

对于本地通知添加包:flutter_local_notifications

 dependencies:
  flutter:
    sdk: flutter
....
  # Use with the Workmanger for background jobs headless execution.
  workmanager: ^0.2.3
  # Use with FlutterLocalNotificationsPlugin for local push notifications.
  flutter_local_notifications: ^1.4.4+2

将此代码添加到:-> android -> app -> src -> main -> AndroidManifest.xml
<!-- Add below permission inside 'manifest' tag -->
<uses-permission android:name="android.permission.RECEIVE_BOOT_COMPLETED"/>
<!-- Add below permission inside 'application' tag -->
<receiver android:name="com.dexterous.flutterlocalnotifications.ScheduledNotificationBootReceiver">
    <intent-filter>
        <action android:name="android.intent.action.BOOT_COMPLETED"/>
        <action android:name="android.intent.action.MY_PACKAGE_REPLACED"/>
    </intent-filter>
</receiver>

lib -> main.dart
import 'package:flutter/material.dart'; 
import 'package:flutter_local_notifications/flutter_local_notifications.dart'; 
import 'package:workmanager/workmanager.dart'; 

void main() { 
    
// needed if you intend to initialize in the `main` function 
WidgetsFlutterBinding.ensureInitialized(); 
Workmanager.initialize(         
    // The top level function,aka callbackDispatcher 
    callbackDispatcher,// If enabled it will post a notification whenever 
    // the task is running. Handy for debugging tasks 
    isInDebugMode: true
); 
// Periodic task registration 
Workmanager.registerPeriodicTask( 
    "2",//This is the value that will be 
    // returned in the callbackDispatcher 
    "simplePeriodicTask",// When no frequency is provided 
    // the default 15 minutes is set. 
    // Minimum frequency is 15 min. 
    // Android will automatically change 
    // your frequency to 15 min 
    // if you have configured a lower frequency. 
    frequency: Duration(minutes: 15),); 
runApp(MyApp()); 
} 

void callbackDispatcher() { 
Workmanager.executeTask((task,inputData) { 
    
    // initialise the plugin of flutterlocalnotifications. 
    FlutterLocalNotificationsPlugin flip = new FlutterLocalNotificationsPlugin(); 
    
    // app_icon needs to be a added as a drawable 
    // resource to the Android head project. 
    var android = new AndroidInitializationSettings('@mipmap/ic_launcher'); 
    var IOS = new IOSInitializationSettings(); 
    
    // initialise settings for both Android and iOS device. 
    var settings = new InitializationSettings(android,IOS); 
    flip.initialize(settings); 
    _showNotificationWithDefaultSound(flip); 
    return Future.value(true); 
}); 
} 

Future _showNotificationWithDefaultSound(flip) async { 
    
// Show a notification after every 15 minute with the first 
// appearance happening a minute after invoking the method 
var androidPlatformChannelSpecifics = new AndroidNotificationDetails( 
    'your channel id','your channel name','your channel description',importance: Importance.Max,priority: Priority.High 
); 
var iOSPlatformChannelSpecifics = new IOSNotificationDetails(); 
    
// initialise channel platform for both Android and iOS device. 
var platformChannelSpecifics = new NotificationDetails( 
    androidPlatformChannelSpecifics,iOSPlatformChannelSpecifics 
); 
await flip.show(0,'GeeksforGeeks','Your are one step away to connect with GeeksforGeeks',platformChannelSpecifics,payload: 'Default_Sound'
); 
} 

class MyApp extends StatelessWidget { 
// This widget is the root of your application. 
@override 
Widget build(BuildContext context) { 
    return MaterialApp( 
    title: 'Geeks Demo',theme: ThemeData( 
        
        // This is the theme 
        // of your application. 
        primarySwatch: Colors.green,),home: HomePage(title: "GeeksforGeeks"),); 
} 
} 

class HomePage extends StatefulWidget { 
HomePage({Key key,this.title}) : super(key: key); 

final String title; 

@override 
_HomePageState createState() => _HomePageState(); 
} 

class _HomePageState extends State<HomePage> { 
@override 
Widget build(BuildContext context) { 
    return Scaffold( 
    appBar: AppBar( 
        title: Text(widget.title),body: new Container(),); 
} 
} 

版权声明:本文内容由互联网用户自发贡献,该文观点与技术仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 dio@foxmail.com 举报,一经查实,本站将立刻删除。

相关推荐


Selenium Web驱动程序和Java。元素在(x,y)点处不可单击。其他元素将获得点击?
Python-如何使用点“。” 访问字典成员?
Java 字符串是不可变的。到底是什么意思?
Java中的“ final”关键字如何工作?(我仍然可以修改对象。)
“loop:”在Java代码中。这是什么,为什么要编译?
java.lang.ClassNotFoundException:sun.jdbc.odbc.JdbcOdbcDriver发生异常。为什么?
这是用Java进行XML解析的最佳库。
Java的PriorityQueue的内置迭代器不会以任何特定顺序遍历数据结构。为什么?
如何在Java中聆听按键时移动图像。
Java“Program to an interface”。这是什么意思?
Java在半透明框架/面板/组件上重新绘画。
Java“ Class.forName()”和“ Class.forName()。newInstance()”之间有什么区别?
在此环境中不提供编译器。也许是在JRE而不是JDK上运行?
Java用相同的方法在一个类中实现两个接口。哪种接口方法被覆盖?
Java 什么是Runtime.getRuntime()。totalMemory()和freeMemory()?
java.library.path中的java.lang.UnsatisfiedLinkError否*****。dll
JavaFX“位置是必需的。” 即使在同一包装中
Java 导入两个具有相同名称的类。怎么处理?
Java 是否应该在HttpServletResponse.getOutputStream()/。getWriter()上调用.close()?
Java RegEx元字符(。)和普通点?