Flutter真棒通知点击打开特定页面



我正在使用Flutter很棒的通知。当应用程序关闭时单击通知时,我希望将其引导到应用程序中的一个特殊页面。对我来说,最简单的方法是什么?

要做到这一点,首先需要在runApp之前设置initializeAwesomeNotifications,然后只需放置listner即可收听通知点击:

初始化:

AwesomeNotifications().initialize(
'resource://drawable/logo_circle_notification',
[
NotificationChannel(
channelGroupKey: 'normal_channel_group',
channelKey: 'normal_channel',
channelName: 'Normal Notifications',
channelDescription: 'Notification channel for normal notifications',
defaultColor: const Color(0xFF9D50DD),
ledColor: Colors.white
),
],
channelGroups: [
NotificationChannelGroup(
channelGroupkey: 'basic_channel_group',
channelGroupName: 'Basic group'),
],
debug: true
);

听:

listenActionStream(){
AwesomeNotifications().actionStream.listen((receivedAction) {
var payload = receivedAction.payload;

if(receivedAction.channelKey == 'normal_channel'){
//do something here
}
});
}

在导航到应用程序的主屏幕之前,您可以将该列表器放在启动屏幕的initState中或其他位置。

因为actionStream在AwesomeNotification((中不再可用我想提出一个不同的解决方案

通知控制器

class NotificationController {
static ReceivedAction? initialAction;
static Future<void> initializeLocalNotifications() async {
await AwesomeNotifications().initialize(
"resource://drawable/ic_bg_service_small",
[
NotificationChannel(
channelKey: "notificationChannelId",
channelName: "channelName",
channelDescription: "channelName",
playSound: true,
onlyAlertOnce: true,
importance: NotificationImportance.High,
defaultPrivacy: NotificationPrivacy.Private,
defaultColor: Colors.blue,
ledColor: Colors.blue,
icon: "resource://drawable/ic_bg_service_small",
)
],
debug: true,
);
// Get initial notification action is optional
initialAction = await AwesomeNotifications()
.getInitialNotificationAction(removeFromActionEvents: false);
}
static Future<void> startListeningNotificationEvents() async {
AwesomeNotifications().setListeners(
onActionReceivedMethod: onActionReceivedMethod,
);
}
@pragma('vm:entry-point')
static Future<void> onActionReceivedMethod(
ReceivedAction receivedAction) async {
if (receivedAction.actionType == ActionType.SilentAction) {
if (receivedAction.payload != null) {
//////////////////////////////
print(receivedAction.payload!["payload"]!);
////////////////////////////
}
} else {
if (receivedAction.payload != null) {
////////////////////////
print(receivedAction.payload!["payload"]!);
}
}
}
}

Future<void> main() async {
await NotificationController.initializeLocalNotifications();
runApp(const MyApp());
}
class MyApp extends StatefulWidget {
const MyApp({super.key});
@override
State<MyApp> createState() => _MyAppState();
}
class _MyAppState extends State<MyApp> {
late ReceivedAction? receivedAction;
@override
void initState() {
NotificationController.startListeningNotificationEvents();
receivedAction = NotificationController.initialAction;
super.initState();
}
@override
Widget build(BuildContext context) {
return const MaterialApp(
debugShowCheckedModeBanner: false,
home: receivedAction!.payload == null
? NotificationPage(
receivedAction: receivedAction,
)
: HomePage(),
);
}
}
class HomePage extends StatelessWidget {
const HomePage({super.key});
@override
Widget build(BuildContext context) {
return Scaffold(
appBar: AppBar(
title: const Text("Home page"),
),
body: const Column(
children: [
Text("Home page"),
],
),
);
}
}
class NotificationPage extends StatelessWidget {
const NotificationPage({super.key, this.receivedAction});
final ReceivedAction receivedAction;
@override
Widget build(BuildContext context) {
return Scaffold(
appBar: AppBar(
title: const Text("Notification page"),
),
body: Column(
children: [
Text("Notification page: ${receivedAction!.payload}"),
],
),
);
}
}

我建议将有效载荷保存在SharedPreference中,并在NotificationPage 之后清除它

相关内容

  • 没有找到相关文章

最新更新