handleNotification function

Future<void> handleNotification(
  1. RemoteMessage message,
  2. StreamChatClient chatClient
)

Implementation

Future<void> handleNotification(
  RemoteMessage message,
  stream.StreamChatClient chatClient,
) async {
  final Map<String, dynamic> data = message.data;
  final local_notifications.FlutterLocalNotificationsPlugin
      flutterLocalNotificationsPlugin = await setupLocalNotifications();
  const local_notifications.NotificationDetails notificationDetails =
      local_notifications.NotificationDetails(
    android: local_notifications.AndroidNotificationDetails(
      'new_message',
      'New message notifications channel',
    ),
  );

  if (data['type'] == 'message.new') {
    final String messageId = data['id'] as String;
    final stream.GetMessageResponse response =
        await chatClient.getMessage(messageId);

    final String? channelName = response.channel?.extraData['Name'] as String?;

    flutterLocalNotificationsPlugin.show(
      1,
      newChatMessageTitle(
        response.message.user?.name,
        channelName,
      ),
      response.message.text,
      notificationDetails,
    );
    headsUpNotification(
      newChatMessageTitle(
        response.message.user?.name,
        channelName,
      ),
      response.message.text,
    );
  } else {
    final RemoteNotification? notification = message.notification;
    if (notification != null) {
      flutterLocalNotificationsPlugin.show(
        notification.hashCode,
        notification.title ?? newNotificationTitleString,
        notification.body ?? newNotificationMessageString,
        notificationDetails,
      );
      headsUpNotification(
        notification.title ?? newNotificationTitleString,
        notification.body ?? newNotificationMessageString,
      );
    }
  }
}