gpt4 book ai didi

android - Flutter 推送通知与 FirebaseMessaging ^8.0.0-dev.14

转载 作者:行者123 更新时间:2023-12-05 00:03:43 51 4
gpt4 key购买 nike

我正在尝试使用 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.....
}

最佳答案

在调用 onMessage() 之前添加这些

 await Firebase.initializeApp(); // necessary for initializing firebase

FirebaseMessaging.instance.getInitialMessage().then((RemoteMessage message) {
print('message recived');
});

尝试使主函数异步并在那里添加前面的行

关于android - Flutter 推送通知与 FirebaseMessaging ^8.0.0-dev.14,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/65911098/

51 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com