我刚刚添加了flutter_local_notifications-6.0.0到我的pubspec.yaml
,之后我在main.dart
中编写了通知代码,它刚刚开始显示这个错误。
这是我的控制台:
Launching libmain.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:srcflutterpackagesflutter_toolsgradleflutter.gradle' line: 1035
* What went wrong:
Execution failed for task ':app:compileFlutterBuildDebug'.
> Process 'command 'C:srcflutterbinflutter.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(),
},
);
}
}
您是否在android/app/src/main/AndroidManifest.xml
中添加了default_notification_channel_id
(即这里的high_importance_channel
) ?
另外,如果您需要显示通知,请参考下面的代码(我就是这样做的):
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);
}
}
}