一小时后颤动通知

问题描述 投票:0回答:2

您好,每当按下按钮时,我需要在移动设备上发出本地通知。我不想立即发出通知,但一小时后,当用户点击该按钮时,有人知道吗?

我尝试了这个插件

flutter_local_notifications: ^8.2.0

但是有人可以帮助我如何在一小时后触发

谢谢

flutter notifications
2个回答
1
投票

使用此功能每小时调用通知

 shownotifications() async {

const AndroidNotificationDetails androidPlatformChannelSpecifics =
    AndroidNotificationDetails('repeating channel id',
        'repeating channel name', 'repeating description');
const NotificationDetails platformChannelSpecifics =
    NotificationDetails(android: androidPlatformChannelSpecifics);
await FlutterLocalNotificationsPlugin().periodicallyShow(0, 'Test title',
    'test body', RepeatInterval.hourly, platformChannelSpecifics,
    androidAllowWhileIdle: true);
 }

并按照此文章逐步设置您正在使用的软件包


0
投票
One of the ways to activate the notification in the flutter application 
is to use the flutter_local_notifications package.
This package is designed to display notifications in daily, hourly, and 
every minute intervals, etc., to use this You must follow the following steps:

First of all, you must install the packages from the links below

https://pub.dev/packages/timezone

https://pub.dev/packages/flutter_local_notifications

Then add the following permissions in the AndroidManifest.xml file of 
your project


<uses-permission 
android:name="android.permission.RECEIVE_BOOT_COMPLETED"/>
<uses-permission 
android:name="android.permission.SCHEDULE_EXACT_ALARM"/>
<uses-permission android:name="android.permission.USE_EXACT_ALARM"/>
<uses-permission android:name="android.permission.POST_NOTIFICATIONS"/>
<uses-permission android:name="android.permission.VIBRATE"/>


Then add the following code in the application tag of this page

<receiver
  android:name=".MyWidgetProvider"
  android:exported="false">
<intent-filter>
  <action android:name="android.appwidget.action.APPWIDGET_UPDATE"/>
  <action android:name="android.intent.action.BOOT_COMPLETED"/>
  <action android:name="android.intent.action.MY_PACKAGE_REPLACED"/>
  <action android:name="android.intent.action.QUICKBOOT_POWERON"/>
  <action android:name="com.htc.intent.action.QUICKBOOT_POWERON"/>
</intent-filter>

</receiver>

<!-- Notification scheduling receiver -->
<receiver android:exported="false" android:name="com.dexterous.flutterlocalnotifications.ScheduledNotificationReceiver"/>
<receiver android:exported="false" android:name="com.dexterous.flutterlocalnotifications.ScheduledNotificationBootReceiver"/>

完成这些设置后,您可以使用以下代码来显示 通知

在项目的主文件中,定义以下变量 之前的变量旁边的状态部分

 final FlutterLocalNotificationsPlugin flutterLocalNotificationsPlugin = FlutterLocalNotificationsPlugin();

然后编写以下函数

Future<void> _initializeNotifications() async {
   const AndroidInitializationSettings initializationSettingsAndroid =
   AndroidInitializationSettings('@mipmap/ic_launcher');

   const InitializationSettings initializationSettings =
   InitializationSettings(android: initializationSettingsAndroid);

   await flutterLocalNotificationsPlugin.initialize(initializationSettings);
   const AndroidNotificationChannel channel = 
   AndroidNotificationChannel(
   'test_channel',
   'cafeZaban Notifications',
   description: 'This channel is for cafe Zaban notifications',
   importance: Importance.max,
   playSound: true,
   showBadge: true,
  );

   await flutterLocalNotificationsPlugin.resolvePlatformSpecificImplementation<AndroidFlutterLocalNotificationsPlugin>()?.createNotificationChannel(channel);
  }


showNotifications() async {
    const AndroidNotificationDetails androidPlatformChannelSpecifics =
    AndroidNotificationDetails(
      'test_channel',
      'cafeZaban Notifications',
    );
    const NotificationDetails platformChannelSpecifics =
    NotificationDetails(android: androidPlatformChannelSpecifics);
    await FlutterLocalNotificationsPlugin().periodicallyShow(
    0,
    "Test title",
    "Test body 😉",
    RepeatInterval.everyMinute,  // everyMinute, hourly, daily, weekly
    platformChannelSpecifics,
    androidAllowWhileIdle: true
  );
}

And finally, call the functions in initState as follows

 @override
 void initState() {
 super.initState();
 tz.initializeTimeZones(); // This will start the time zone database
 _initializeNotifications();
 showNotifications();
 }

To use the tz variable, you need to add the following line in the mports 
section of your main page like this

 import 'package:timezone/data/latest.dart' as tz;

With these things you have done, now you send a notification every 
minute. 
I hope your project is successful ;)
© www.soinside.com 2019 - 2024. All rights reserved.