我尝试在flutter应用程序中通过fcm显示后台通知。然而,我不知道为什么我创建的通知没有显示,但有另一个与消防商店数据库显示相同的值。我是这样创建通知的:
@pragma('vm:entry-point')
Future<void> registerBackgroundMessage(RemoteMessage message) async {
await Firebase.initializeApp();
AppLogger.log("background", message.data);
RemoteNotification? notification = message.notification;
AndroidNotification? android = message.notification?.android;
AppLogger.log(" --- bACKGROUND message received ---", "");
AppLogger.log("Title", message.notification!.title);
AppLogger.log("Body", message.notification!.body);
final String payload = message.data['version'];
String smallIcon = 'drawable/small_launcher_icon';
if (notification != null && android != null) {
flutterLocalNotificationsPlugin.show(
notification.hashCode,
notification.title,
notification.body,
NotificationDetails(
android: AndroidNotificationDetails(
'channel_id',
'channel_name',
importance: Importance.max,
priority: Priority.high,
ticker: 'New message received',
icon: smallIcon,
playSound: true,
enableVibration: true,
largeIcon:
const DrawableResourceAndroidBitmap('mipmap/launcher_icon'),
color: const Color.fromARGB(255, 0, 123, 255),
ledColor: const Color.fromARGB(255, 255, 0, 0),
ledOnMs: 1000,
ongoing: true,
ledOffMs: 500,
),
));
}
return Future.value();
}
这就是我在main()中调用它的方式:
void main() async {
WidgetsFlutterBinding.ensureInitialized();
await EasyLocalization.ensureInitialized();
await Firebase.initializeApp(options: DefaultFirebaseOptions.currentPlatform);
MessagingService _msgService = MessagingService();
await _msgService.init();
FirebaseMessaging.onBackgroundMessage(_msgService.registerBackgroundMessage);
这是messagingService中的init方法:
Future<void> init() async {
await _requestPermission();
_deviceId = await getDeviceId();
AppLogger.log("DeviceId", _deviceId);
_firebaseMessaging.getToken().then((token) async {
AppLogger.log("FCM TOKEN", token);
saveTokens(token!);
});
await flutterLocalNotificationsPlugin
.resolvePlatformSpecificImplementation<
AndroidFlutterLocalNotificationsPlugin>()
?.createNotificationChannel(channel);
FirebaseMessaging.instance.getInitialMessage().then((message) {
print("FirebaseMessaging.getInitialMessage");
});
Ps:当我收到通知时,我在cosole中得到了这些错误:W/FLTFireMsgService(30433):无法在Dart中处理后台消息,因为尚未注册onBackgroundMessage处理程序。W/FirebaseMessaging(30433):应用尚未创建AndroidManifest.xml中设置的通知通道。将使用默认值。这是AndroidManifest.xml:
<meta-data
android:name="com.google.firebase.messaging.default_notification_channel_id"
android:value="@string/default_notification_channel_id" />
这是strings.xml:
<?xml version="1.0" encoding="utf-8"?>
<resources>
<string name="default_notification_channel_id" translatable="false">fcm_default_channel</string>
</resources>
提前感谢您提出一些解决方案来解决我的问题。
1条答案
按热度按时间myzjeezk1#