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

Flutter 颤振:应用程序处于后台时无法显示本地通知,flutter,firebase-cloud-messaging,Flutter,Firebase Cloud Messaging,编辑:此问题已在最新的Flatter和firebase版本上修复 我正在收听Firebase FCMdatatype消息,在收到FCM消息时,我打算在应用程序内显示本地通知 _firebaseMessaging.configure( onMessage: (Map<String, dynamic> message) async { print('on message $message'); LocalNotificationService.

编辑:此问题已在最新的Flatter和firebase版本上修复

我正在收听Firebase FCM
data
type消息,在收到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");
}
\u firebaseMessaging.configure(
onMessage:(映射消息)异步{
打印('on message$message');
LocalNotificationService.ShowNotificationWithDefaultSound(“onmessage”);
},
onResume:(映射消息)异步{
LocalNotificationService.ShowNotificationWithDefaultSound(“onresume”);
打印('在简历上$message');
},
onLaunch:(映射消息)异步{
LocalNotificationService.ShowNotificationWithDefaultSound(“onlaunch”);
打印('on launch$message');
},
onBackgroundMessage:myBackgroundMessageHandler,
);
///对。这是一个全局函数
未来myBackgroundMessageHandler(映射消息){
var androidPlatformChannelSpecifics=新的AndroidNotificationDetails(
‘chanID_007’、‘chanName_kk107’、‘这是我的频道’;
var iosPlatformChannelSpecifics=新的IOSNotificationDetails();
var platformChannelSpecifics=新通知详细信息(
androidPlatformChannelSpecifics、iosPlatformChannelSpecifics);
flatterlocalnotificationsplugin flatterlocalnotificationsplugin;
var初始化设置和ROID=
新的AndroidInitializationSettings(“@mipmap/ic_launcher”);
var initializationSettingsIos=新IOSSinitializationSettings();
var initializationSettings=新的初始化设置(
初始化设置和ROID,初始化设置SIOS);
FlatterLocalNotificationsPlugin=新的FlatterLocalNotificationsPlugin();
FlatterLocalNotificationsPlugin.initialize(初始化设置,
onSelectNotification:onSelectNotification);
等待本地通知。显示(
0、'Tada!!'、'test',平台通道详细信息,
有效载荷:“有效载荷#7”);
打印(“背景mssg”);
}
但是显示带有flatter_local_通知插件的本地通知会抛出一个错误

未处理的异常:MissingPluginException(在channel dexterous.com/flatter/local\u notifications上找不到方法初始化的实现)


我在这里遗漏了什么?

在添加任何代码之前,您应该尝试运行
flatterclean

当我添加新包并在没有完全重建的情况下执行热重新加载/重新启动时,发生了此错误

如果这与此无关,那么我认为你只是错过了cannel创作部分。我已经使这段代码对我来说运行良好,
wait
部分就是您所需要的

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);
    }
  }
Future ensureInitialized()异步{
flatterLocalNotificationsPlugin.initialize(初始化设置(
android:AndroidInitializationSettings(“ic_通知”)、iOS:IOSSInitializationSettings(),
);
if(Platform.isAndroid){
最终AndroidNotificationChannel通道=AndroidNotificationChannel(
“高重要性通道”,//id
“高重要性通知”,//标题
“此通道用于重要通知。”,//说明
重要性:重要性.max,
);
等待通知
.resolvePlatformSpecificImplementation<
AndroidLutterLocalNotificationsPlugin>()
?createNotificationChannel(频道);
}
}

在这里找到了解决方案。需要在android上的应用程序类中注册插件。这回答了你的问题吗?