android - 带有 FirebaseMessaging 的 Flutter 推送通知 ^8.0.0-dev.14
问题描述
我正在尝试使用 FirebaseMessaging ^8.0.0-dev.14 向我的应用程序用户发送推送通知,但我遇到了一些麻烦。我能够在 IOS 上接收通知,但我的 Android 模拟器上没有显示任何内容。我在 init 上设置了侦听器,但他们只能在 IOS 设备上看到通知。有没有人遇到过类似的问题或知道如何让通知在 Android 上显示?这是我的代码:
import 'package:firebase_messaging/firebase_messaging.dart';
import 'package:flutter/material.dart';
import 'package:flutter_local_notifications/flutter_local_notifications.dart'
as locNots;
import 'package:flutter_local_notifications/flutter_local_notifications.dart';
import 'package:url_launcher/url_launcher.dart';
import '../widgets/mentorActionsButton.dart';
import '../widgets/list_of_events.dart';
import 'userPage.dart';
import 'loginPage.dart';
class MentorMainPage extends StatefulWidget {
@override
_MentorMainPageState createState() => _MentorMainPageState();
}
class _MentorMainPageState extends State<MentorMainPage> {
@override
void initState() {
super.initState();
final fbm = FirebaseMessaging.instance;
// IOS Configurations
fbm.setForegroundNotificationPresentationOptions(
alert: true, badge: true, sound: true);
fbm.requestPermission();
FirebaseMessaging.onMessage.listen((RemoteMessage message) {
print('IOS Listener');
print('Got a message whilst in the foreground!');
print('Message data: ${message.data}');
if (message.notification != null) {
print('Message also contained a notification: ${message.notification}');
}
});
//Android Configurations
const AndroidNotificationChannel channel = AndroidNotificationChannel(
'high_importance_channel', // id
'High Importance Notifications', // title
'This channel is used for important notifications.', // description
importance: Importance.max,
);
final FlutterLocalNotificationsPlugin flutterLocalNotificationsPlugin =
FlutterLocalNotificationsPlugin();
flutterLocalNotificationsPlugin
.resolvePlatformSpecificImplementation<
AndroidFlutterLocalNotificationsPlugin>()
?.createNotificationChannel(channel);
FirebaseMessaging.onMessage.listen((RemoteMessage message) {
print('Android Listener');
RemoteNotification notification = message.notification;
AndroidNotification android = message.notification?.android;
print('Android Notification:');
if (notification != null && android != null) {
flutterLocalNotificationsPlugin.show(
notification.hashCode,
notification.title,
notification.body,
NotificationDetails(
android: AndroidNotificationDetails(
channel.id, channel.name, channel.description,
icon: android?.smallIcon,
showWhen: false,
importance: Importance.max,
priority: locNots.Priority.high),
));
}
});
}
var userLoggedIn = true;
@override
Widget build(BuildContext context) {
Code for screen view.....
}
解决方案
add these before calling onMessage()
await Firebase.initializeApp(); // necessary for initializing firebase
FirebaseMessaging.instance.getInitialMessage().then((RemoteMessage message) {
print('message recived');
});
try making main function async and adding the previous lines there
推荐阅读
- excel - 如果文件已存在,则覆盖该文件
- c# - 在 VS 中构建 Angular/Core 项目时,文本“i´¢ówdm´¢ú:编译成功”指的是什么?
- android - 从 fragmentA 打开 FragmentB + 方法
- mysql - SQL 查询中的位移位
- here-api - 反向地理编码未正确显示区域
- jenkins - 如何限制 Jenkins 中的代理只运行一项工作?
- java - 如何使用 Spark 和 Java 为以下示例输入获取动态数据集的转置
- javascript - 为什么我们使用延迟加载,是否有必要在像SPA这样的小项目中使用它?
- python - psycopg2.extras.DictCursor 没有给我列名
- constraints - 如何在 Mathematica 中最小化具有约束的函数