大家好,我正在构建一个应用程序,我想实现 firebase 消息传递,但我无法让它工作。直到这行代码一切都很顺利
void getToken() async {
String? token = await FirebaseMessaging.instance.getToken();
print("this is the token" + token!);
}
应用程序请求发送消息的权限,但当它尝试获取设备的令牌时,会抛出此错误。
E/flutter ( 5116): [ERROR:flutter/runtime/dart_vm_initializer.cc(41)] Unhandled Exception: [firebase_messaging/unknown] java.io.IOException: java.util.concurrent.ExecutionException: java.io.IOException: AUTHENTICATION_FAILED
E/flutter ( 5116): #0 StandardMethodCodec.decodeEnvelope (package:flutter/src/services/message_codecs.dart:653:7)
E/flutter ( 5116): #1 MethodChannel._invokeMethod (package:flutter/src/services/platform_channel.dart:296:18)
E/flutter ( 5116): <asynchronous suspension>
E/flutter ( 5116): #2 MethodChannel.invokeMapMethod (package:flutter/src/services/platform_channel.dart:499:43)
E/flutter ( 5116): <asynchronous suspension>
E/flutter ( 5116): #3 MethodChannelFirebaseMessaging.getToken (package:firebase_messaging_platform_interface/src/method_channel/method_channel_messaging.dart:224:11)
E/flutter ( 5116): <asynchronous suspension>
E/flutter ( 5116): #4 _LobbyState.getToken (package:myfuji/screens/Lobby.dart:61:21)
E/flutter ( 5116): <asynchronous suspension>
E/flutter ( 5116):
W/DynamiteModule( 5116): Local module descriptor class for com.google.android.gms.providerinstaller.dynamite not found.
I/DynamiteModule( 5116): Considering local module com.google.android.gms.providerinstaller.dynamite:0 and remote module com.google.android.gms.providerinstaller.dynamite:0
W/ProviderInstaller( 5116): Failed to load providerinstaller module: No acceptable module com.google.android.gms.providerinstaller.dynamite found. Local version is 0 and remote version is 0.
D/EGL_emulation( 5116): app_time_stats: avg=84.66ms min=17.81ms max=417.64ms count=11
任何人都可以指出我正确的方向吗?非常感谢。我还包含了该部分的完整代码
import 'package:cloud_firestore/cloud_firestore.dart';
import 'package:firebase_auth/firebase_auth.dart';
import 'package:firebase_messaging/firebase_messaging.dart';
import 'package:flutter/material.dart';
import 'package:myfuji/fetchInfo/userCart.dart';
import 'package:myfuji/screens/Chat.dart';
import 'package:myfuji/screens/News.dart';
import 'package:myfuji/screens/mindYoBis.dart';
import 'package:myfuji/screens/Shop.dart';
import '../fetchInfo/my_Drawer.dart';
import 'SignIn.dart';
class Lobby extends StatefulWidget {
const Lobby({Key? key}) : super(key: key);
@override
State<Lobby> createState() => _LobbyState();
}
class _LobbyState extends State<Lobby> {
String? mToken = " ";
FirebaseMessaging messaging =
FirebaseMessaging.instance; //getting firebase messaging ready for use
List<String> docIDs = [];
Future getDocId() async {
await FirebaseFirestore.instance.collection('user').get().then(
(snapshot) => snapshot.docs.forEach(
(document) {
print(document.reference);
docIDs.add(document.reference.id);
},
),
);
}
late final CollectionReference usersCollection = FirebaseFirestore.instance
.collection('user'); //gets 'user' collection from firebase
final FirebaseAuth auth = FirebaseAuth.instance;
int index = 0;
final screen = [
const News(),
const mindYoBis(),
const Shop(),
const Chat()
]; // lower drawer
@override
void initState() {
super.initState();
requestPermission();
getToken();
}
// get device token and print
void getToken() async {
String? token = await FirebaseMessaging.instance.getToken();
print("this is the token" + token!);
}
// end
// request permission to get message from app
void requestPermission() async {
NotificationSettings settings = await messaging.requestPermission(
alert: true,
announcement: false,
badge: true,
carPlay: false,
criticalAlert: false,
provisional: false,
sound: true,
);
if (settings.authorizationStatus == AuthorizationStatus.authorized) {
print('User granted permission');
} else if (settings.authorizationStatus ==
AuthorizationStatus.provisional) {
print('User granted provisional permission');
} else {
print('User declined or has not accepted permission');
}
}
// end of permission
@override
Widget build(BuildContext context) {
return MaterialApp(
debugShowCheckedModeBanner: false,
theme: ThemeData.dark(),
home: Scaffold(
endDrawer: my_Drawer(documentId: auth.currentUser!.uid),
appBar: AppBar(
// cart bottom/icon
actions: [
IconButton(
padding: EdgeInsets.only(right: 10.0),
icon: const Icon(
Icons.shopping_cart,
),
onPressed: () => {
Navigator.pushReplacement(
context,
MaterialPageRoute(
builder: (context) =>
const userCart())) //takes user to cart
},
),
Builder(builder: (context) {
return IconButton(
padding: const EdgeInsets.only(right: 10.0),
icon: const Icon(
Icons.menu,
),
onPressed: () => {Scaffold.of(context).openEndDrawer()},
);
}),
],
title: const Text('Welcome'),
leading: Image.asset('src/logo.png'),
backgroundColor: Colors.deepPurple,
),
//body
body: screen[index], // screen navi
//body
bottomNavigationBar: NavigationBarTheme(
data: NavigationBarThemeData(
backgroundColor: Colors.deepPurple,
indicatorColor: Colors.deepPurple[100],
labelTextStyle: MaterialStateProperty.all(
const TextStyle(fontSize: 18, fontWeight: FontWeight.w500))),
//lower bottom navigation
child: NavigationBar(
labelBehavior: NavigationDestinationLabelBehavior.onlyShowSelected,
selectedIndex: index,
onDestinationSelected: (index) =>
setState(() => this.index = index),
destinations: const [
NavigationDestination(icon: Icon(Icons.home), label: 'Home'),
NavigationDestination(
icon: Icon(Icons.emoji_food_beverage_rounded),
label: 'Local Bis'),
NavigationDestination(
icon: Icon(Icons.shopping_bag), label: 'Shop'),
NavigationDestination(icon: Icon(Icons.chair), label: 'Games')
],
),
),
),
);
}
//sign out
signOut() async {
await auth.signOut();
Navigator.pushReplacement(
context, MaterialPageRoute(builder: (context) => const SignIn()));
}
}
```code
当 Flutter 无法完全访问导入的
firebase_messaging
包时,就会出现此错误。如果您通过命令 flutter pub add firebase_messaging 添加了包,请再次单击 pub get from pubspec.yaml
文件关闭应用程序并重新运行整个项目,它在我的情况下修复了此错误。