我猜 Firebase 禁用了遗留 API,对于新用户来说。他们希望我们使用 v1 API。当我尝试使用 Postman 发送通知时,我得到了这个错误信息。
"error": {
"code": 401,
"message": "Request had invalid authentication credentials. Expected OAuth 2 access token, login cookie or other valid authentication credential. See https://developers.google.com/identity/sign-in/web/devconsole-project.",
"status": "UNAUTHENTICATED"
}
我有服务账户、服务器密钥等,但不知道如何迁移。 我使用的是Dart-shelf后端和Flutter。
对于Postman,这是我的配置。
Authorization = Bearer +AAAAA..token
这是我发送的正文内容。
{
"message":{
"token":"<generated token by flutter>",
"notification":{
"body":"This is an FCM notification message!",
"title":"FCM Message"
}
}
}
这就是我在flutter中为特定设备生成令牌的方法。
@override
void initState() {
super.initState();
WidgetsBinding.instance.addPostFrameCallback((timeStamp) async {
_init();
});
var initializationSettingsAndroid =
const AndroidInitializationSettings('ic_launcher');
var initialzationSettingsAndroid =
const AndroidInitializationSettings('@mipmap/ic_launcher');
var initializationSettings =
InitializationSettings(android: initialzationSettingsAndroid);
flutterLocalNotificationsPlugin.initialize(initializationSettings);
FirebaseMessaging.onMessage.listen((RemoteMessage message) {
RemoteNotification? notification = message.notification;
AndroidNotification? android = message.notification?.android;
if (notification != null && android != null) {
flutterLocalNotificationsPlugin.show(
notification.hashCode,
notification.title,
notification.body,
NotificationDetails(
android: AndroidNotificationDetails(
channel.id,
channel.name,
color: Colors.blue,
// TODO add a proper drawable resource to android, for now using
// one that already exists in example app.
icon: "@mipmap/ic_launcher",
),
));
}
});
FirebaseMessaging.onMessageOpenedApp.listen((RemoteMessage message) {
RemoteNotification? notification = message.notification;
AndroidNotification? android = message.notification?.android;
if (notification != null && android != null) {
showDialog(
context: context,
builder: (_) {
return AlertDialog(
title: Text(notification.title!),
content: SingleChildScrollView(
child: Column(
crossAxisAlignment: CrossAxisAlignment.start,
children: [Text(notification.body!)],
),
),
);
});
}
});
getToken();
}
我的后端托管在谷歌云计算上。