You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

已授权通知权限,Flutter本地定时通知仍无法显示的问题

问题排查与解决方案

核心问题1:插件实例不统一

你的showTestNotification方法使用了独立的FlutterLocalNotificationsPlugin实例,这个实例未经过NotificationService中的初始化流程(时区绑定、平台配置初始化);而正常工作的普通通知用的是NotificationService内部的notificationsPlugin实例,两者不是同一个对象,导致定时通知的配置不完整。

修复方案:
将定时通知方法整合到NotificationService类中,统一使用同一个插件实例:

class NotificationService {
  final FlutterLocalNotificationsPlugin notificationsPlugin = FlutterLocalNotificationsPlugin();

  // 保留原initNotification、notificationDetails、showNotification方法

  Future<void> scheduleTestNotification() async {
    // 检查权限
    final bool? isGranted = await notificationsPlugin.resolvePlatformSpecificImplementation<AndroidFlutterLocalNotificationsPlugin>()?.areNotificationsEnabled();
    if (!isGranted!) {
      print("Notifications are not granted");
      AppSettings.openAppSettings();
      return;
    }
    print("Notifications are granted");

    // 统一渠道配置
    const AndroidNotificationDetails androidDetails = AndroidNotificationDetails(
      'channelId', 
      'channelName', 
      importance: Importance.max, 
      priority: Priority.high, 
      showWhen: false
    );
    const DarwinNotificationDetails iosDetails = DarwinNotificationDetails();
    const NotificationDetails platformDetails = NotificationDetails(android: androidDetails, iOS: iosDetails);

    await notificationsPlugin.zonedSchedule(
      0,
      'Test Notification',
      'This is a test notification after 5 seconds',
      tz.TZDateTime.now(tz.local).add(const Duration(seconds: 5)),
      platformDetails,
      androidAllowWhileIdle: true,
      uiLocalNotificationDateInterpretation: UILocalNotificationDateInterpretation.absoluteTime,
    );
  }
}

修改触发按钮代码:

OutlinedButton(
  onPressed: () => _notificationService.scheduleTestNotification(),
  child: Text("Schedule 5-sec Notification"),
),

核心问题2:iOS权限缺失

iOS平台的定时通知需要额外的调度通知权限,你的DarwinInitializationSettings仅请求了基础的弹窗、角标、声音权限,未请求调度权限,导致iOS无法触发定时通知。

修复方案:
在iOS初始化设置中添加调度权限请求:

var initializationSettingsIOS = DarwinInitializationSettings(
  requestAlertPermission: true,
  requestBadgePermission: true,
  requestSoundPermission: true,
  requestScheduleNotificationPermission: true, // 新增该行
  onDidReceiveLocalNotification: (int id, String? title, String? body, String? payload) async {}
);

次要优化:时区初始化顺序

调整main函数初始化顺序,先初始化时区再初始化通知服务,避免潜在的时区绑定问题:

void main() async {
  WidgetsFlutterBinding.ensureInitialized();
  tz.initializeTimeZones(); // 移至通知初始化前
  NotificationService().initNotification();
}

额外验证点

  • 测试时确保应用处于后台或前台状态(部分设备前台需开启通知显示权限)
  • Android设备需将应用加入电池优化白名单,避免系统杀死定时任务
  • 检查通知渠道channelId未被系统禁用

内容的提问来源于stack exchange,提问作者A_K

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.07.07 14:25:29