抖动的本地通知中,“ MissingPluginException”不是“字符串”类型错误的子类型

时间:2019-04-16 07:25:20

标签: flutter localnotification

我尝试了以下代码。它给出的'MissingPluginException'不是'String'类型的子类型 以下是我使用的代码(此处仅粘贴了通知部分)。扑医生也没问题。

import 'package:flutter_local_notifications/flutter_local_notifications.dart';

class _Home extends State<HomePage> with TickerProviderStateMixin {
     static FirebaseMessaging _firebaseMessaging = FirebaseMessaging();
     FlutterLocalNotificationsPlugin flutterLocalNotificationsPlugin = new FlutterLocalNotificationsPlugin();

@override
  void initState() {
    super.initState();
    firebaseCloudMessaging_Listeners();

  }
void firebaseCloudMessaging_Listeners() {
    var android = new AndroidInitializationSettings('mipmap/ic_launcher',);
    var ios = new IOSInitializationSettings();
    var platform = new InitializationSettings(android, ios);

    flutterLocalNotificationsPlugin.initialize(platform,onSelectNotification: onSelectNotification);

    if (Platform.isIOS) iOS_Permission();
    _firebaseMessaging.getToken().then((token){
      //FCM=token;
    }).catchError((onError){
      print("blb07 "+onError.toString());
    });

    _firebaseMessaging.configure(
      onMessage: (Map<String, dynamic> message) async {
        print('blb07 on message $message');
        handleNotification(message['data']['message']);
      },
      onResume: (Map<String, dynamic> message) async {
        print('blb07 on resume $message');
        handleNotification(message['data']['message']);
      },
      onLaunch: (Map<String, dynamic> message) async {
        print('blb07 on launch $message');
        handleNotification(message['data']['message']);
      },
    );
  }

  void iOS_Permission() {
    _firebaseMessaging.requestNotificationPermissions(
        IosNotificationSettings(sound: true, badge: true, alert: true)
    );
    _firebaseMessaging.onIosSettingsRegistered
        .listen((IosNotificationSettings settings)
    {
      print("Settings registered: $settings");
    });
  }
  Future onSelectNotification(String payload) async {
    await Navigator.push(
      context,
      new MaterialPageRoute(builder: (context) => new DisplayNotifications()),
    );
  }

  void handleNotification(String message) async{
      var androidPlatformChannelSpecifics = new AndroidNotificationDetails(
          'default1', 'Sureworks ', 'Sureworks app notifications',
          importance: Importance.Max, priority: Priority.High);
      var iOSPlatformChannelSpecifics = new IOSNotificationDetails();
      var platformChannelSpecifics = new NotificationDetails(
          androidPlatformChannelSpecifics, iOSPlatformChannelSpecifics);
      print("blb07 start");
      try{
        await flutterLocalNotificationsPlugin.show(0, 'Sureworks', message, platformChannelSpecifics,payload: 'item')
            .then((onValue){
          print("blb07 finished");
        }).catchError((onError)=>print("blb07 "+onError));
        print("blb07 hello end line");
      }catch(e){
        print("blb07 error "+e.toString());
      }
  }
} 

我的pubspec.yaml依赖项

dev_dependencies:
  flutter_test:
    sdk: flutter

  http: ^0.11.3+16
  shared_preferences: ^0.4.2
  firebase_messaging: ^4.0.0+1
  firebase_core: ^0.2.5
  flutter_launcher_icons: ^0.7.0
  paging: ^0.1.0
  flutter_local_notifications: ^0.6.1

可能是什么问题?传递到handleNotification函数的消息仅是字符串格式。从服务器接收通知数据没问题。

2 个答案:

答案 0 :(得分:0)

您运行pod install吗? 如果不是很可能在本机方面缺少一些东西。

只需打开一个终端,然后导航到flutter项目的根目录。
然后cd进入iOS文件夹并运行pod install

这应该可以解决您的错误。

答案 1 :(得分:0)

我找到了解决方案。将我的Android迁移到AndroidX。现在一切正常。花了4个多小时。 迁移到AndroidX并完成了“使缓存无效并重新启动”操作。