gpt4 book ai didi

Flutter:当应用程序处于后台时无法显示本地通知

转载 作者:行者123 更新时间:2023-12-04 04:19:02 26 4
gpt4 key购买 nike

EDIT: This issue is fixed on the latest flutter and firebase version.

我正在收听 Firebase FCM data 类型的消息,并且在收到 FCM 消息时,我打算在应用内显示本地通知。

_firebaseMessaging.configure(
onMessage: (Map<String, dynamic> message) async {
print('on message $message');
LocalNotificationService.showNotoficationWithDefaultSound("onmessage");
},
onResume: (Map<String, dynamic> message) async {
LocalNotificationService.showNotoficationWithDefaultSound("onresume");
print('on resume $message');
},
onLaunch: (Map<String, dynamic> message) async {
LocalNotificationService.showNotoficationWithDefaultSound("onlaunch");
print('on launch $message');
},
onBackgroundMessage: myBackgroundMessageHandler,
);


/// Yes. This is a global function
Future<dynamic> myBackgroundMessageHandler(Map<String, dynamic> message) {
var androidPlatformChannelSpecifics = new AndroidNotificationDetails(
'chanID_007', 'chanName_kk107', 'This is my channel');
var iosPlatformChannelSpecifics = new IOSNotificationDetails();
var platformChannelSpecifics = new NotificationDetails(
androidPlatformChannelSpecifics, iosPlatformChannelSpecifics);
FlutterLocalNotificationsPlugin flutterLocalNotificationsPlugin;
var initializationSettingsAndroid =
new AndroidInitializationSettings('@mipmap/ic_launcher');
var initializationSettingsIos = new IOSInitializationSettings();
var initializationSettings = new InitializationSettings(
initializationSettingsAndroid, initializationSettingsIos);
flutterLocalNotificationsPlugin = new FlutterLocalNotificationsPlugin();
flutterLocalNotificationsPlugin.initialize(initializationSettings,
onSelectNotification: onSelectNotification);


await flutterLocalNotificationsPlugin.show(
0, 'Tada!!', 'test', platformChannelSpecifics,
payload: 'payload#7');
print("background mssg");
}

但是用 flutter_local_notification 插件显示本地通知会报错

未处理的异常:MissingPluginException(在 channel dexterous.com/flutter/local_notifications 上未找到方法初始化的实现)

我在这里错过了什么?

最佳答案

在添加任何代码之前,您应该尝试运行 flutter clean
当我添加一个新包并在没有完全重建的情况下执行热重载/重启时,我发生了这个错误。

如果它与此无关,我认为您只是错过了 channel 创建部分。我已经使这段代码对我来说工作正常,await 部分就是您所需要的。

Future<void> ensureInitialized() async {

flutterLocalNotificationsPlugin.initialize(InitializationSettings(
android: AndroidInitializationSettings("ic_notification"), iOS: IOSInitializationSettings()),
);

if (Platform.isAndroid) {

final AndroidNotificationChannel channel = AndroidNotificationChannel(
'high_importance_channel', // id
'High Importance Notifications', // title
'This channel is used for important notifications.', // description
importance: Importance.max,
);

await flutterLocalNotificationsPlugin
.resolvePlatformSpecificImplementation<
AndroidFlutterLocalNotificationsPlugin>()
?.createNotificationChannel(channel);
}
}

关于Flutter:当应用程序处于后台时无法显示本地通知,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/59860348/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com