微信公众号搜"智元新知"关注
微信扫一扫可直接关注哦!

面临 flutter_local_notifications-6.0.0 的问题

如何解决面临 flutter_local_notifications-6.0.0 的问题

我刚刚将 Flutter_local_notifications-6.0.0 添加到我的 pubspec.yaml 中,并且在我在 main.dart 中编写了通知代码之后,它才开始显示错误

这是我的控制台:

    Launching lib\main.dart on RMX1851 in debug mode...
Running Gradle task 'assembleDebug'...
/C:/src/Flutter/.pub-cache/hosted/pub.dartlang.org/Flutter_local_notifications-6.0.0/lib/src/Flutter_local_notifications_plugin.dart:12:1: Error: 'SelectNotificationCallback' is imported from both 'package:Flutter_local_notifications_platform_interface/src/typedefs.dart' and 'package:Flutter_local_notifications/src/typedefs.dart'.
import 'typedefs.dart';
^^^^^^^^^^^^^^^^^^^^^^^^...
/C:/src/Flutter/.pub-cache/hosted/pub.dartlang.org/Flutter_local_notifications-6.0.0/lib/src/platform_Flutter_local_notifications.dart:25:1: Error: 'SelectNotificationCallback' is imported from both 'package:Flutter_local_notifications_platform_interface/src/typedefs.dart' and 'package:Flutter_local_notifications/src/typedefs.dart'.
import 'typedefs.dart';
^^^^^^^^^^^^^^^^^^^^^^^^...
/C:/src/Flutter/.pub-cache/hosted/pub.dartlang.org/Flutter_local_notifications-6.0.0/lib/src/platform_Flutter_local_notifications.dart:37:5: Error: 'validateId' is imported from both 'package:Flutter_local_notifications_platform_interface/src/helpers.dart' and 'package:Flutter_local_notifications/src/helpers.dart'.
    validateId(id);
    ^^^^^^^^^^
/C:/src/Flutter/.pub-cache/hosted/pub.dartlang.org/Flutter_local_notifications-6.0.0/lib/src/platform_Flutter_local_notifications.dart:105:5: Error: 'validateId' is imported from both 'package:Flutter_local_notifications_platform_interface/src/helpers.dart' and 'package:Flutter_local_notifications/src/helpers.dart'.
    validateId(id);
    ^^^^^^^^^^
/C:/src/Flutter/.pub-cache/hosted/pub.dartlang.org/Flutter_local_notifications-6.0.0/lib/src/platform_Flutter_local_notifications.dart:131:5: Error: 'validateId' is imported from both 'package:Flutter_local_notifications_platform_interface/src/helpers.dart' and 'package:Flutter_local_notifications/src/helpers.dart'.
    validateId(id);
    ^^^^^^^^^^
/C:/src/Flutter/.pub-cache/hosted/pub.dartlang.org/Flutter_local_notifications-6.0.0/lib/src/platform_Flutter_local_notifications.dart:165:5: Error: 'validateId' is imported from both 'package:Flutter_local_notifications_platform_interface/src/helpers.dart' and 'package:Flutter_local_notifications/src/helpers.dart'.
    validateId(id);
    ^^^^^^^^^^
/C:/src/Flutter/.pub-cache/hosted/pub.dartlang.org/Flutter_local_notifications-6.0.0/lib/src/platform_Flutter_local_notifications.dart:190:5: Error: 'validateId' is imported from both 'package:Flutter_local_notifications_platform_interface/src/helpers.dart' and 'package:Flutter_local_notifications/src/helpers.dart'.
    validateId(id);
    ^^^^^^^^^^
/C:/src/Flutter/.pub-cache/hosted/pub.dartlang.org/Flutter_local_notifications-6.0.0/lib/src/platform_Flutter_local_notifications.dart:213:5: Error: 'validateId' is imported from both 'package:Flutter_local_notifications_platform_interface/src/helpers.dart' and 'package:Flutter_local_notifications/src/helpers.dart'.
    validateId(id);
    ^^^^^^^^^^
/C:/src/Flutter/.pub-cache/hosted/pub.dartlang.org/Flutter_local_notifications-6.0.0/lib/src/platform_Flutter_local_notifications.dart:236:5: Error: 'validateId' is imported from both 'package:Flutter_local_notifications_platform_interface/src/helpers.dart' and 'package:Flutter_local_notifications/src/helpers.dart'.
    validateId(id);
    ^^^^^^^^^^
/C:/src/Flutter/.pub-cache/hosted/pub.dartlang.org/Flutter_local_notifications-6.0.0/lib/src/platform_Flutter_local_notifications.dart:261:5: Error: 'validateId' is imported from both 'package:Flutter_local_notifications_platform_interface/src/helpers.dart' and 'package:Flutter_local_notifications/src/helpers.dart'.
    validateId(id);
    ^^^^^^^^^^
/C:/src/Flutter/.pub-cache/hosted/pub.dartlang.org/Flutter_local_notifications-6.0.0/lib/src/platform_Flutter_local_notifications.dart:430:5: Error: 'validateId' is imported from both 'package:Flutter_local_notifications_platform_interface/src/helpers.dart' and 'package:Flutter_local_notifications/src/helpers.dart'.
    validateId(id);
    ^^^^^^^^^^
/C:/src/Flutter/.pub-cache/hosted/pub.dartlang.org/Flutter_local_notifications-6.0.0/lib/src/platform_Flutter_local_notifications.dart:465:5: Error: 'validateId' is imported from both 'package:Flutter_local_notifications_platform_interface/src/helpers.dart' and 'package:Flutter_local_notifications/src/helpers.dart'.
    validateId(id);
    ^^^^^^^^^^
/C:/src/Flutter/.pub-cache/hosted/pub.dartlang.org/Flutter_local_notifications-6.0.0/lib/src/platform_Flutter_local_notifications.dart:501:5: Error: 'validateId' is imported from both 'package:Flutter_local_notifications_platform_interface/src/helpers.dart' and 'package:Flutter_local_notifications/src/helpers.dart'.
    validateId(id);
    ^^^^^^^^^^
/C:/src/Flutter/.pub-cache/hosted/pub.dartlang.org/Flutter_local_notifications-6.0.0/lib/src/platform_Flutter_local_notifications.dart:526:5: Error: 'validateId' is imported from both 'package:Flutter_local_notifications_platform_interface/src/helpers.dart' and 'package:Flutter_local_notifications/src/helpers.dart'.
    validateId(id);
    ^^^^^^^^^^
/C:/src/Flutter/.pub-cache/hosted/pub.dartlang.org/Flutter_local_notifications-6.0.0/lib/src/platform_Flutter_local_notifications.dart:548:5: Error: 'validateId' is imported from both 'package:Flutter_local_notifications_platform_interface/src/helpers.dart' and 'package:Flutter_local_notifications/src/helpers.dart'.
    validateId(id);
    ^^^^^^^^^^
/C:/src/Flutter/.pub-cache/hosted/pub.dartlang.org/Flutter_local_notifications-6.0.0/lib/src/platform_Flutter_local_notifications.dart:570:5: Error: 'validateId' is imported from both 'package:Flutter_local_notifications_platform_interface/src/helpers.dart' and 'package:Flutter_local_notifications/src/helpers.dart'.
    validateId(id);
    ^^^^^^^^^^
/C:/src/Flutter/.pub-cache/hosted/pub.dartlang.org/Flutter_local_notifications-6.0.0/lib/src/platform_Flutter_local_notifications.dart:654:5: Error: 'validateId' is imported from both 'package:Flutter_local_notifications_platform_interface/src/helpers.dart' and 'package:Flutter_local_notifications/src/helpers.dart'.
    validateId(id);
    ^^^^^^^^^^
/C:/src/Flutter/.pub-cache/hosted/pub.dartlang.org/Flutter_local_notifications-6.0.0/lib/src/platform_Flutter_local_notifications.dart:683:5: Error: 'validateId' is imported from both 'package:Flutter_local_notifications_platform_interface/src/helpers.dart' and 'package:Flutter_local_notifications/src/helpers.dart'.
    validateId(id);
    ^^^^^^^^^^
/C:/src/Flutter/.pub-cache/hosted/pub.dartlang.org/Flutter_local_notifications-6.0.0/lib/src/platform_Flutter_local_notifications.dart:705:5: Error: 'validateId' is imported from both 'package:Flutter_local_notifications_platform_interface/src/helpers.dart' and 'package:Flutter_local_notifications/src/helpers.dart'.
    validateId(id);
    ^^^^^^^^^^


FAILURE: Build Failed with an exception.

* Where:
Script 'C:\src\Flutter\packages\Flutter_tools\gradle\Flutter.gradle' line: 1035

* What went wrong:
Execution Failed for task ':app:compileFlutterBuildDebug'.
> Process 'command 'C:\src\Flutter\bin\Flutter.bat'' finished with non-zero exit value 1

* Try:
Run with --stacktrace option to get the stack trace. Run with --info or --debug option to get more log output. Run with --scan to get full insights.

* Get more help at https://help.gradle.org

BUILD Failed in 17s
Exception: Gradle task assembleDebug Failed with exit code 1

这是我的 main.dart 文件

import 'package:chatter_Box/helperServices/auth.dart';
import 'package:chatter_Box/helperServices/profileUpdate.dart';
import 'package:chatter_Box/screens/home.dart';
import 'package:firebase_core/firebase_core.dart';
import 'package:firebase_messaging/firebase_messaging.dart';
import 'package:Flutter/cupertino.dart';
import 'package:Flutter/material.dart';
import 'package:chatter_Box/screens/welcome_screen.dart';
import 'package:chatter_Box/screens/registration_screen.dart';
import 'package:Flutter_local_notifications/Flutter_local_notifications.dart';
import 'package:google_fonts/google_fonts.dart';

const AndroidNotificationChannel channel = AndroidNotificationChannel(
    "high_importance_channel",// id
    "High Importance Notifications",// title
    "This channel is used for important notifications",// description
importance: Importance.high,playSound: true
);

final FlutterlocalnotificationsPlugin FlutterlocalnotificationsPlugin =
    FlutterlocalnotificationsPlugin();
Future<void> _firebaseMessagingBackgroundHandler(RemoteMessage message)async{
  await Firebase.initializeApp();
 print("a bg meesage just came ${message.messageId}");
}

  main() async {
  WidgetsFlutterBinding.ensureInitialized();
  await Firebase.initializeApp();
  FirebaseMessaging.onBackgroundMessage((message) => _firebaseMessagingBackgroundHandler(message));

  await FlutterlocalnotificationsPlugin
      .resolvePlatformSpecificImplementation<AndroidFlutterlocalnotificationsPlugin>()
      ?.createNotificationChannel(channel);
   await FirebaseMessaging.instance.setForegroundNotificationPresentationoptions(
     alert: true,badge: true,sound: true,);
  runApp(ChatterBox());
}

class ChatterBox extends StatelessWidget {

  @override
  Widget build(BuildContext context) {

    return MaterialApp(
      theme: ThemeData(
        textTheme: GoogleFonts.latoTextTheme(
          Theme.of(context).textTheme,),debugShowCheckedModeBanner: false,home: FutureBuilder(
        future:AuthMethods().getCurrentUser(),builder:(context,AsyncSnapshot<dynamic> snapshot)  {
          if(snapshot.hasData){
          return HomeScreen();}
          else{return WelcomeScreen() ;}
          }

      ),routes: {
        WelcomeScreen.id:(context) => WelcomeScreen(),RegistrationScreen.id: (context) => RegistrationScreen(),// LoginScreen.id :(context) => LoginScreen(),HomeScreen.id :(context) => HomeScreen(),SignUpPage.id : (context) => SignUpPage(),},);
  }
}

解决方法

您是否在 default_notification_channel_id 中添加了 high_importance_channel(即此处的 android/app/src/main/AndroidManifest.xml)?

另外,如果你需要显示通知,请参考下面的代码(我是这样做的):

import 'package:firebase_messaging/firebase_messaging.dart';
import 'package:flutter_local_notifications/flutter_local_notifications.dart';

class LocalNotificationService {
  static final FlutterLocalNotificationsPlugin _notificationsPlugin =
      FlutterLocalNotificationsPlugin();

  static void initialize() {
    final initializationSettings = InitializationSettings(
      android: AndroidInitializationSettings('@drawable/ic_notification'),);

    _notificationsPlugin.initialize(initializationSettings);
  }

  static Future<void> displayNotification(RemoteMessage message) async {
    try {
      final id = DateTime.now().millisecondsSinceEpoch ~/ 1000;

      final notificationDetails = NotificationDetails(
        android: AndroidNotificationDetails(
          'high_importance_channel','High Importance Notifications','This channel is used for important notifications',importance: Importance.max,priority: Priority.high,),);

      await _notificationsPlugin.show(
        id,message.notification!.title,message.notification!.body,notificationDetails,);
    } on Exception catch (e) {
      print(e);
    }
  }
}

版权声明:本文内容由互联网用户自发贡献,该文观点与技术仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 dio@foxmail.com 举报,一经查实,本站将立刻删除。