javascript 正在从Firebase消息传递接收FCM消息,但未显示通知

zpqajqem  于 2023-09-29  发布在  Java
关注(0)|答案(2)|浏览(117)

我有一个云函数,每当新的预订被添加到集合中时,它会向管理员发送FCM消息,FCM消息正在正确发送,但当我尝试使用我的通知API时,我得到了这个错误:

D/FLTFireMsgReceiver(16858): broadcast received for message
I/flutter (16858): FlutterFire Messaging: An error occurred in your background messaging handler:
I/flutter (16858): Null check operator used on a null value

这是我的通知类:

import 'package:firebase_messaging/firebase_messaging.dart';
import 'package:flutter_local_notifications/flutter_local_notifications.dart';

class NotificationApi {
  static final _notification = FlutterLocalNotificationsPlugin();

  static void init() {
    _notification.initialize(
      const InitializationSettings(
        android: AndroidInitializationSettings('@mipmap/ic_launcher'),
        iOS: DarwinInitializationSettings(),
      ),
    );
  }

  static pushNotification(
    RemoteMessage message,
  ) async {
    var androidPlatformChannelSpecifics = const AndroidNotificationDetails(
      'channed id',
      'channel name',
      channelDescription: 'channel description',
      importance: Importance.max,
      priority: Priority.high,
    );
    var iOSPlatformChannelSpecifics = const DarwinNotificationDetails();
    var platformChannelSpecifics = NotificationDetails(
      android: androidPlatformChannelSpecifics,
      iOS: iOSPlatformChannelSpecifics,
    );
    await _notification.show(0, message.notification!.title,
        message.notification!.body, platformChannelSpecifics);
  }
}

这是我观察fcm消息并处理它们的地方:

Future<void> handleBackgroundMessage(RemoteMessage message) async {
  // Access title and body directly from message.data
  final title = message.data['title'];
  final body = message.data['body'];
  //pushing the message to the notification api to handle it
  await NotificationApi.pushNotification(message);

  //Here we are printing the message instead of passing into the notification class

  print('Title: $title');
  print('Body: $body');
  print('Payload: ${message.data}');
}

class DBHandler {
  //instance of firebase authentication
  final FirebaseAuth auth = FirebaseAuth.instance;

  //instance of firebase messenging
  final _firebaseMessaging = FirebaseMessaging.instance;

  //Function to initialise Notifications
  Future<void> initNotification() async {
    //request persmission from user
    await _firebaseMessaging.requestPermission();

    generateDeviceToken();
//background
    FirebaseMessaging.onBackgroundMessage(handleBackgroundMessage);
//foreground (The event is a Remote Message Arriving)
    FirebaseMessaging.onMessage.listen((event) async {
      await NotificationApi.pushNotification(event);
    });

    //Here instead of passing to the onBackground message we will use notification package

    // Save the FCM token to the user's Firestore document
  }

  generateDeviceToken() async {
    String? fcmToken = await FirebaseMessaging.instance.getToken();
    final userId = FirebaseAuth.instance.currentUser!.uid;
    await FirebaseFirestore.instance
        .collection('users')
        .doc(userId)
        .update({'fcmToken': fcmToken});
  }

是什么导致了我的后台消息中的错误

kcwpcxri

kcwpcxri1#

message.notification仅在从控制台发送通知类型消息时填充。当您从Admin SDK发送时,它是一个数据类型的消息,而不是一个数据负载。
阅读文档中有关消息类型的更多信息,以更好地理解这两种消息类型之间的差异。
如果您需要所创建的数据有效负载的标题和正文字段,则应该只使用message.data

ha5z0ras

ha5z0ras2#

看起来你还没有设置任何代码来显示通知-最流行的方法是使用这个包flutter_local_notifications
在你的handleBackgroundMessage函数中,你可以使用上面的包,例如:

_flutterLocalNotificationsPlugin.show(
    message.data.hashCode,
    message.data['title'],
    message.data['body'],
    NotificationDetails(
      android: AndroidNotificationDetails(
        _androidChannel.id,
        _androidChannel.name,
        _androidChannel.description,
        importance: Importance.max,
        priority: Priority.high,
     ),
  ),
  payload: message.data,
);

你可以check out this tutorial to setup the above

相关问题