AndroidAlarmManager调用flutterLocalNotificationsPlugin.show(),但没有显示通知。
创始人
2024-10-06 06:02:52
0

要使用Android的AlarmManager调用flutterLocalNotificationsPlugin.show()方法显示通知,你需要确保已经正确设置了AlarmManager和通知通道。

以下是一个示例代码,演示了如何在Android中使用AlarmManager调用flutterLocalNotificationsPlugin.show()方法显示通知:

首先,确保在Android的AndroidManifest.xml文件中添加以下权限:




接下来,创建一个名为MyNotificationReceiver的BroadcastReceiver类,继承自FlutterLocalNotificationsPlugin.NotificationReceiver类,并重写onReceive()方法:

import android.content.BroadcastReceiver;
import android.content.Context;
import android.content.Intent;

public class MyNotificationReceiver extends FlutterLocalNotificationsPlugin.NotificationReceiver {
    @Override
    public void onReceive(Context context, Intent intent) {
        super.onReceive(context, intent);

        // 在这里调用flutterLocalNotificationsPlugin.show()方法显示通知
        // 示例代码:
        // flutterLocalNotificationsPlugin.show(
        //     0,
        //     "标题",
        //     "内容",
        //     new NotificationDetails(
        //         androidPlatformChannelSpecifics,
        //         null
        //     )
        // );
    }
}

然后,在Android的MainActivity中注册MyNotificationReceiver类:

import io.flutter.embedding.android.FlutterActivity;
import io.flutter.embedding.engine.FlutterEngine;
import io.flutter.plugins.GeneratedPluginRegistrant;

public class MainActivity extends FlutterActivity {
    @Override
    public void configureFlutterEngine(FlutterEngine flutterEngine) {
        GeneratedPluginRegistrant.registerWith(flutterEngine);

        // 注册MyNotificationReceiver类
        new FlutterLocalNotificationsPlugin().setNotificationReceiver(new MyNotificationReceiver());
    }
}

最后,在你的Flutter代码中使用AlarmManager调用flutterLocalNotificationsPlugin.show()方法:

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

final FlutterLocalNotificationsPlugin flutterLocalNotificationsPlugin =
    FlutterLocalNotificationsPlugin();

void main() async {
  WidgetsFlutterBinding.ensureInitialized();

  // 初始化flutterLocalNotificationsPlugin
  const AndroidInitializationSettings initializationSettingsAndroid =
      AndroidInitializationSettings('app_icon');
  final InitializationSettings initializationSettings =
      InitializationSettings(android: initializationSettingsAndroid);
  await flutterLocalNotificationsPlugin.initialize(initializationSettings,
      onSelectNotification: null);

  // 注册AlarmManager
  if (Platform.isAndroid) {
    await AndroidAlarmManager.initialize();
  }

  runApp(MyApp());
}

class MyApp extends StatelessWidget {
  @override
  Widget build(BuildContext context) {
    return MaterialApp(
      title: 'Flutter Demo',
      theme: ThemeData(
        primarySwatch: Colors.blue,
        visualDensity: VisualDensity.adaptivePlatformDensity,
      ),
      home: MyHomePage(),
    );
  }
}

class MyHomePage extends StatelessWidget {
  @override
  Widget build(BuildContext context) {
    return Scaffold(
      appBar: AppBar(
        title: Text('Flutter Demo Home Page'),
      ),
      body: Center(
        child: RaisedButton(
          child: Text('调用AlarmManager显示通知'),
          onPressed: () {
            // 设置AlarmManager
            AndroidAlarmManager.oneShot(
              const Duration(seconds: 5),
              0,
              showNotification,
            );
          },
        ),
      ),
    );
  }

  void showNotification() async {
    const AndroidNotificationDetails androidPlatformChannelSpecifics =
        AndroidNotificationDetails(
      'your channel id',
      'your channel name',
      'your channel description',
      importance: Importance.max,
      priority: Priority.high,
      ticker: 'ticker',
    );
    const NotificationDetails platformChannelSpecifics =
        NotificationDetails(android: androidPlatformChannelSpecifics);

    await flutterLocalNotificationsPlugin.show(
      0,
      '标题',
      '内容',
      platformChannelSpecifics,
    );
  }
}

这样,当你点击按钮后,AlarmManager会在5秒后触发showNotification()方法,然后调用flutterLocalNotificationsPlugin.show()方法显示通知。

相关内容

热门资讯

2026版辅助挂!牵手游戏ap... 2026版辅助挂!牵手游戏app辅助器,心悦踢辅助软件-其实真的是有辅助神器(哔哩哔哩)1、完成牵手...
反观!金州水鱼脚本,永盛联盟辅... 反观!金州水鱼脚本,永盛联盟辅助脚本-其实真的是有辅助插件(哔哩哔哩)1、下载好永盛联盟辅助脚本脚本...
方法辅助挂!新漫游免费辅助器,... 方法辅助挂!新漫游免费辅助器,新海贝之城脚本-竟然存在有辅助脚本(哔哩哔哩)在进入新漫游免费辅助器软...
代打辅助挂!微信老铁13水辅助... 代打辅助挂!微信老铁13水辅助,仙神互娱辅助-果然确实有辅助器(哔哩哔哩)1、任何微信老铁13水辅助...
黑科技辅助挂!乐酷副厅外卖辅助... 黑科技辅助挂!乐酷副厅外卖辅助,掌中乐游戏辅助工具-好像真的是有辅助工具(哔哩哔哩)1、下载好掌中乐...
今日!桃乐甘肃麻将辅助器,天天... 今日!桃乐甘肃麻将辅助器,天天福建十三兵修改器-一贯存在有辅助挂(哔哩哔哩)1、任何天天福建十三兵修...
透视免费!新九天作必弊系统,赣... 透视免费!新九天作必弊系统,赣湘互娱辅助-切实是真的有辅助神器(哔哩哔哩)1、玩家可以在新九天作必弊...
截至目前!金虎爷辅助器,新51... 截至目前!金虎爷辅助器,新518互游插件下载-真是是真的有辅助神器(哔哩哔哩)1、下载好新518互游...
截至发稿!蜀山四川辅助脚本,福... 截至发稿!蜀山四川辅助脚本,福建天天开心辅助工具下载-真是真的是有辅助app(哔哩哔哩)1、上手简单...
随着!杭州都莱大菠萝买了挂有用... 随着!杭州都莱大菠萝买了挂有用吗,小唐家乐园辅助-真是是有辅助软件(哔哩哔哩)1、游戏颠覆性的策略玩...