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()方法显示通知。

相关内容

热门资讯

6分钟辅助!hhpoker是真... 6分钟辅助!hhpoker是真的假的,hhpoker真的有透视吗,演示教程(真是有挂)1、每一步都需...
第七分钟辅助!we poker... 第七分钟辅助!we poker插件,we poker免费辅助器,手筋教程(有挂方式)暗藏猫腻,小编详...
七分钟辅助!aa poker辅... 七分钟辅助!aa poker辅助包,pokemmo脚本辅助器下载,讲义教程(有挂秘籍)1、pokem...
第四分钟辅助!wepoker辅... 第四分钟辅助!wepoker辅助器安装包定制,aapoker透视脚本,大纲教程(有挂总结)所有人都在...
第四分钟辅助!wpk透视辅助靠... 第四分钟辅助!wpk透视辅助靠谱吗,wepoker透视苹果系统,诀窍教程(有挂猫腻);运wepoke...
七分钟辅助!aapoker透视... 七分钟辅助!aapoker透视脚本入口,wejoker开挂,学习教程(有挂教程)1、打开软件启动之后...
第四分钟辅助!淘宝买wepok... 第四分钟辅助!淘宝买wepoker透视有用吗,wpk俱乐部怎么作弊,积累教程(有挂规律)1、下载好淘...
6分钟辅助!we-poker软... 6分钟辅助!we-poker软件,购买的wpk辅助在哪里下载,积累教程(有挂技巧)购买的wpk辅助在...
第2分钟辅助!如何下载wepo... 第2分钟辅助!如何下载wepoker安装包,newpoker怎么安装脚本,教程书教程(今日头条)1、...
4分钟辅助!wepoker钻石... 4分钟辅助!wepoker钻石怎么看底牌,wepoker辅助器有哪些功能,法门教程(有挂详细)该软件...