【问题标题】:I'm having Issues with flutter_local_notifications. How do i fix this problem?我遇到了 flutter_local_notifications 的问题。我该如何解决这个问题?
【发布时间】:2025-12-24 14:55:11
【问题描述】:

我已经尝试了下面的代码,看来我还需要做一些事情。我正在使用flutter_local_notification。我需要包括firebase吗?这是我的代码

import 'package:flutter/material.dart';  
import 'package:flutter_local_notifications/flutter_local_notifications.dart';

class NotificationApi {
static final _notifications = FlutterLocalNotificationsPlugin();
static Future _notificationDetails() async {
return NotificationDetails(
  android: AndroidNotificationDetails(
    'com.example.gym',
    'goal_channel',
    importance: Importance.max,
  ),
  iOS: IOSNotificationDetails(),
);
}

static Future showNotification({
  int id = 0,
  String? title,
  body,
  payload,
 }) async =>
  _notifications.show(
    id,
    title,
    body,
    await _notificationDetails(),
    payload: payload,
  );
}

结果如下

【问题讨论】:

  • 在 AndroidNotificationDetails 中添加图标:'@mipmap/ic_launcher' 并重试
  • 你是个天才!令人惊讶的是,Icon 只是让我耽误了几个小时。谢谢大哥
  • 任何时候,兄弟,我之前也发生过同样的错误:)

标签: flutter notifications flutter-local-notification


【解决方案1】:

感谢@Amroun

这是一个有效的代码

import 'package:flutter/material.dart';  
import'package:flutter_local_notifications/flutter_local_notifications.dart';

class NotificationApi {
 static final _notifications = FlutterLocalNotificationsPlugin();
 static Future _notificationDetails() async {
 return NotificationDetails(
  android: AndroidNotificationDetails(
  'com.example.gym',
  'goal_channel',
  icon: '@mipmap/ic_launcher',
  importance: Importance.max,
),
iOS: IOSNotificationDetails(),
);
}

 static Future showNotification({
  int id = 0,
  String? title,
  body,
  payload,
 }) async =>
_notifications.show(
id,
title,
body,
await _notificationDetails(),
payload: payload,
);
}

【讨论】:

    最近更新 更多