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

Flutter本地通知仅模拟器生效,Android真机无响应求解决

安卓真机定时通知不弹出问题排查与修复方案

问题背景

实现了三个对应5/10/15分钟的定时通知按钮,使用flutter_local_notifications(v14.0.0)开发,模拟器运行正常,但安卓真机完全无法弹出通知。核心需求是无论应用状态如何,指定时长后都能触发通知。

用户提供的核心代码如下:

import 'dart:io';
import 'package:flutter/material.dart';
import 'package:flutter_local_notifications/flutter_local_notifications.dart';
import 'package:flutter_timezone/flutter_timezone.dart';
import 'package:rxdart/rxdart.dart';
import 'package:timezone/data/latest_all.dart';
import 'package:timezone/timezone.dart';

class NotificationApi {
static final _notifications = FlutterLocalNotificationsPlugin();
static final onNotifications = BehaviorSubject<String?>();

static bool notificationPermission = true;

static Future _notificationDetails() async {
    return const NotificationDetails(
        android: AndroidNotificationDetails(
            'channelID',
            'channelName',
            channelDescription: 'channelDescription',
            importance: Importance.high, // Set the importance to high
            priority: Priority.high, // Set the priority to high
            playSound: true,
        ),
    );
}

static Future init({bool initScheduled = false}) async {
    const AndroidInitializationSettings android =
        AndroidInitializationSettings('@mipmap/ic_launcher');
    const InitializationSettings settings =
        InitializationSettings(android: android);

    final details = await _notifications.getNotificationAppLaunchDetails();
    if (details != null && details.didNotificationLaunchApp) {
        onNotifications.add(details.notificationResponse?.payload);
    }

    await _notifications.initialize(settings,
        onDidReceiveNotificationResponse: ((payload) async {
            onNotifications.add(payload.payload);
        }));

    if (initScheduled) {
        initializeTimeZones();
        final timeZoneName = await FlutterTimezone.getLocalTimezone();
        setLocalLocation(getLocation(timeZoneName));
    }
}

static Future<bool?> checkNotificationPermissions() async {
    if (Platform.isAndroid) {
        await _notifications
            .resolvePlatformSpecificImplementation<
                AndroidFlutterLocalNotificationsPlugin>()
            ?.requestPermission();
        return await _notifications
            .resolvePlatformSpecificImplementation<
                AndroidFlutterLocalNotificationsPlugin>()
            ?.areNotificationsEnabled();
    }
    return null;
}

static void showScheduledNotification({
    required int id,
    String? title,
    String? body,
    String? payload,
    required DateTime scheduledDate,
}) async =>
    _notifications.zonedSchedule(id, title, body,
        TZDateTime.from(scheduledDate, local), await _notificationDetails(),
        androidScheduleMode: AndroidScheduleMode.exactAllowWhileIdle,
        uiLocalNotificationDateInterpretation:
            UILocalNotificationDateInterpretation.absoluteTime,
        payload: payload);

static void cancel(int id) => _notifications.cancel(id);
static void cancelAll() => _notifications.cancelAll();
}

class MyApp extends StatelessWidget {
const MyApp({Key? key}) : super(key: key);

@override
Widget build(BuildContext context) {
    return MaterialApp(
        home: HomePage(),
    );
}
}

class HomePage extends StatefulWidget {
@override
_HomePageState createState() => _HomePageState();
}

class _HomePageState extends State<HomePage> {
bool _notificationPermission = true;

@override
void initState() {
    super.initState();

    WidgetsBinding.instance!.addPostFrameCallback((_) async {
        await NotificationApi.init(initScheduled: true);
        _listenNotifications();
        NotificationApi.checkNotificationPermissions().then((value) {
            setState(() {
                _notificationPermission = value ?? true;
            });
        });
    });
    }

@override
void dispose() {
    NotificationApi.onNotifications.close();
    super.dispose();
}  

Future<void> _requestNotificationPermission() async {
    final bool? result = await NotificationApi.checkNotificationPermissions();
    if (result != null && !result) {
        setState(() {
            _notificationPermission = false;
        });
        // Handle the case where the user has not granted notification permissions
    }
    }

void _scheduleNotification(int minutes) {
    final scheduledDate = DateTime.now().add(Duration(minutes: minutes));
    NotificationApi.showScheduledNotification(
        id: minutes,
        scheduledDate: scheduledDate,
        title: 'Time is up',
        body: 'This is the notificaiton message that needs to be displayed.',
    );

    ScaffoldMessenger.of(context).showSnackBar(
        SnackBar(
            content: Text('Notification scheduled for $minutes minutes.'),
        ),
    );
    }

void _listenNotifications() =>
    NotificationApi.onNotifications.stream.listen((payload) {
        // Handle notification payload, if needed
        print('Notification payload: $payload');
    });

@override
Widget build(BuildContext context) {
    return Scaffold(
        appBar: AppBar(
            title: const Text('Local Notification Page'),
        ),
        body: Center(
            child: Column(
                mainAxisAlignment: MainAxisAlignment.center,
                children: [
                    ElevatedButton(
                        onPressed: () async {
                            await _requestNotificationPermission();
                            _scheduleNotification(5);
                        },
                        child: const Text('Schedule notification message in 5 Minutes'),
                    ),
                    SizedBox(height: 16),
                    ElevatedButton(
                        onPressed: () async {
                            await _requestNotificationPermission();
                            _scheduleNotification(10);
                        },
                        child: const Text('Schedule notification message in 10 Minutes'),
                    ),
                    SizedBox(height: 16),
                    ElevatedButton(
                        onPressed: () async {
                            await _requestNotificationPermission();
                            _scheduleNotification(15);
                        },
                        child: const Text('Schedule notification message in 15 Minutes'),
                    ),
                ],
            ),
        ),
    );
}
}

排查与修复方案

1. 主动创建通知渠道(Android 8.0+必做)

Android 8.0+要求必须显式创建通知渠道,自动创建可能存在延迟或失败的情况。修改NotificationApi的init方法,主动创建渠道:

static Future init({bool initScheduled = false}) async {
    const AndroidInitializationSettings android =
        AndroidInitializationSettings('@mipmap/ic_launcher');
    const InitializationSettings settings =
        InitializationSettings(android: android);

    final details = await _notifications.getNotificationAppLaunchDetails();
    if (details != null && details.didNotificationLaunchApp) {
        onNotifications.add(details.notificationResponse?.payload);
    }

    // 新增:主动创建通知渠道
    if (Platform.isAndroid) {
      const AndroidNotificationChannel channel = AndroidNotificationChannel(
        'channelID',
        '定时通知', // 更友好的渠道名称
        description: '用于触发定时提醒的通知渠道', // 更清晰的描述
        importance: Importance.high,
        enableVibration: true,
        playSound: true,
      );
      final AndroidFlutterLocalNotificationsPlugin? androidPlugin =
          _notifications.resolvePlatformSpecificImplementation<AndroidFlutterLocalNotificationsPlugin>();
      await androidPlugin?.createNotificationChannel(channel);
    }

    await _notifications.initialize(settings,
        onDidReceiveNotificationResponse: ((payload) async {
            onNotifications.add(payload.payload);
        }));

    if (initScheduled) {
        initializeTimeZones();
        final timeZoneName = await FlutterTimezone.getLocalTimezone();
        setLocalLocation(getLocation(timeZoneName));
    }
}

2. 优化定时调度的时间转换与参数配置

TZDateTime.from可能存在时区转换误差,且需要显式开启androidAllowWhileIdle确保低功耗模式下触发:

static void showScheduledNotification({
    required int id,
    String? title,
    String? body,
    String? payload,
    required DateTime scheduledDate,
}) async {
    // 直接用本地时区构建TZDateTime,避免转换误差
    final tzScheduledDate = TZDateTime(
        local,
        scheduledDate.year,
        scheduledDate.month,
        scheduledDate.day,
        scheduledDate.hour,
        scheduledDate.minute,
        scheduledDate.second,
    );
    print('调度通知ID:$id,触发时间:$tzScheduledDate'); // 添加日志排查

    await _notifications.zonedSchedule(
        id,
        title,
        body,
        tzScheduledDate,
        await _notificationDetails(),
        androidScheduleMode: AndroidScheduleMode.exactAllowWhileIdle,
        uiLocalNotificationDateInterpretation: UILocalNotificationDateInterpretation.absoluteTime,
        payload: payload,
        androidAllowWhileIdle: true, // 关键参数:允许低功耗模式下触发
    );
}

3. 优化权限请求逻辑

原权限请求流程先请求再检查,改为先检查再请求,且在调度前确保权限已开启:

// 修改NotificationApi的权限方法
static Future<bool> checkAndRequestPermissions() async {
    if (Platform.isAndroid) {
      final AndroidFlutterLocalNotificationsPlugin? androidPlugin =
          _notifications.resolvePlatformSpecificImplementation<AndroidFlutterLocalNotificationsPlugin>();
      bool isEnabled = await androidPlugin?.areNotificationsEnabled() ?? false;
      if (!isEnabled) {
        // 请求权限
        await androidPlugin?.requestPermission();
        // 再次检查权限状态
        isEnabled = await androidPlugin?.areNotificationsEnabled() ?? false;
      }
      return isEnabled;
    }
    return true;
}

// 修改HomePage的_scheduleNotification方法
void _scheduleNotification(int minutes) async {
    bool hasPermission = await NotificationApi.checkAndRequestPermissions();
    if (!hasPermission) {
      ScaffoldMessenger.of(context).showSnackBar(
        const SnackBar(content: Text('请先开启通知权限后重试')),
      );
      return;
    }
    final scheduledDate = DateTime.now().add(Duration(minutes: minutes));
    NotificationApi.showScheduledNotification(
        id: minutes,
        scheduledDate: scheduledDate,
        title: '时间到了',
        body: '这是您设置的定时提醒',
    );

    ScaffoldMessenger.of(context).showSnackBar(
        SnackBar(content: Text('已设置$minutes分钟后提醒')),
    );
}

4. 检查安卓设备的电池优化设置

国内安卓厂商(小米、华为、OPPO等)的电池优化会阻止后台任务:

  • 引导用户进入设置 > 电池 > 电池优化,找到应用并设置为不允许优化
  • 部分机型需开启自启动权限,确保应用被杀死后仍能触发定时任务

5. 先验证即时通知功能

添加即时通知测试方法,排除基础配置问题:

// 在NotificationApi中添加
static Future showInstantNotification({
    required int id,
    String? title,
    String? body,
    String? payload,
}) async =>
    _notifications.show(id, title, body, await _notificationDetails(), payload: payload);

在HomePage添加测试按钮,点击触发即时通知。如果即时通知也不弹出,说明是权限或渠道配置问题;如果能弹出,再聚焦定时调度的问题。

6. 日志排查

通过打印日志确认调度是否成功,以及是否有错误信息:

  • 查看showScheduledNotification中打印的触发时间是否正确
  • 查看控制台是否有权限相关的错误日志

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

相关产品推荐
方舟 Agent Plan

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

最近更新时间:2026.07.01 03:35:57