本地通知 Flutter

5

有人能给我展示一下用代码如何使用本地通知插件在Flutter中安排通知吗?我试过Git存储库中的示例,但对我来说并不起作用。虽然普通通知可以工作,但是如何为特定时间安排它,例如提醒?


1
看看我的答案,自己尝试一下。如果您仍然无法安排通知,请在您的项目中更新帖子,并说明您所使用的代码,以及您是否无法在Android和iOS上或仅在一个平台上安排通知。重要的是发布您尝试过的最小代码,以便人们可以根据您的代码来帮助您。这也使您的问题符合SO最佳实践。 - shadowsheep
2个回答

5

如果您想要安排通知,可以使用类似以下代码的方式:

插件示例代码中:

/// Schedules a notification that specifies a different icon, sound and vibration pattern
  Future _scheduleNotification() async {
    var scheduledNotificationDateTime =
        new DateTime.now().add(new Duration(seconds: 5));
    var vibrationPattern = new Int64List(4);
    vibrationPattern[0] = 0;
    vibrationPattern[1] = 1000;
    vibrationPattern[2] = 5000;
    vibrationPattern[3] = 2000;

    var androidPlatformChannelSpecifics = new AndroidNotificationDetails(
        'your other channel id',
        'your other channel name',
        'your other channel description',
        icon: 'secondary_icon',
        sound: 'slow_spring_board',
        largeIcon: 'sample_large_icon',
        largeIconBitmapSource: BitmapSource.Drawable,
        vibrationPattern: vibrationPattern,
        color: const Color.fromARGB(255, 255, 0, 0));
    var iOSPlatformChannelSpecifics =
        new IOSNotificationDetails(sound: "slow_spring_board.aiff");
    var platformChannelSpecifics = new NotificationDetails(
        androidPlatformChannelSpecifics, iOSPlatformChannelSpecifics);
    await flutterLocalNotificationsPlugin.schedule(
        0,
        'scheduled title',
        'scheduled body',
        scheduledNotificationDateTime,
        platformChannelSpecifics);
  }

你必须关注的部分是:
// Schedule a notification in 5 secs from now
var scheduledNotificationDateTime =
        new DateTime.now().add(new Duration(seconds: 5));

如果您不确定原生项目设置该如何进行以显示通知,请克隆插件存储库并尝试其示例。

/// 重要提示:单独运行以下代码将无法正常工作, 因为每个平台的头项目都需要设置。

/// 请从GitHub存储库下载完整的示例应用程序,其中所有设置都已完成


当我们将时间设置为120秒这样的短时间时,它是有效的,但当我们将通知设置为2000秒(30分钟)时,它就不起作用了。在这种方法中安排长时间是否存在问题? - Abdullah Khan
flutterlocalnotificationsplugin声音在iOS上无法工作,请帮帮我。 - Hitesh sapra

-1
const AndroidNotificationDetails androidPlatformChannelSpecifics = AndroidNotificationDetails('repeating channel id','repeating channel name', 'repeating description');
const NotificationDetails platformChannelSpecifics = NotificationDetails(android: androidPlatformChannelSpecifics);
await flutterLocalNotificationsPlugin.periodicallyShow(0, 'repeating title', 'repeating body', RepeatInterval.everyMinute, platformChannelSpecifics, androidAllowWhileIdle: true);

你可以使用 periodicallyShowflutter_local_notifications


网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接