编辑:此问题已在最新的颤振和Firebase版本上修复。
我正在收听 Firebase FCMdata
类型的消息,在收到 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会引发错误
Unhandled Exception: MissingPluginException(No implementation found for method initialize on channel dexterous.com/flutter/local_notifications)
我在这里错过了什么?
在添加任何代码之前,您应该尝试运行flutter clean
.
当我添加新包并在没有完全重建的情况下执行热重载/重新启动时,此错误发生在我身上。
如果它与此无关,我认为您只是错过了Cannel Creation部分。我已经使这段代码对我来说工作正常,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);
}
}