To display stream chat notification background with the use of FirebaseMessaging.onBackgroundMessage, follow these steps:
Set up Firebase Cloud Messaging (FCM) in your Stream Chat application by adding the necessary SDKs and registering your app with FCM.
Create a new file in your project named firebase_messaging.dart
and add the following code:
import 'dart:io';
import 'dart:convert';
import 'package:firebase_messaging/firebase_messaging.dart';
FirebaseMessaging messaging = FirebaseMessaging.instance;
Future<void> firebaseMessagingBackgroundHandler(RemoteMessage message) async {
print('Handling a background message ${message.messageId}');
// TODO: Handle the message - this can be anything you want to do with it.
}
void initializeFirebaseMessaging() {
if (Platform.isIOS) {
messaging.requestPermission();
}
FirebaseMessaging.onBackgroundMessage(firebaseMessagingBackgroundHandler);
}
Call initializeFirebaseMessaging()
on app startup to register your background message handler with Firebase Messaging.
When receiving a chat notification, make sure to include a data
payload in your notification message that contains the necessary information to display the chat notification. For example:
{
"to": "your-device-token",
"data": {
"title": "New Message from John",
"body": "Hey, how's it going?",
"chat_id": "1234"
}
}
firebaseMessagingBackgroundHandler()
, retrieve the necessary information from the data
payload and use it to display the chat notification background. For example:Future<void> firebaseMessagingBackgroundHandler(RemoteMessage message) async {
print('Handling a background message ${message.messageId}');
final data = jsonDecode(message.data['data']);
final chatId = data['chat_id'];
final title = data['title'];
final body = data['body'];
// TODO: Use the chatId, title, and body to display the chat notification here.
}
Please start posting anonymously - your entry will be published after you log in or create a new account. This space is reserved only for answers. If you would like to engage in a discussion, please instead post a comment under the question or an answer that you would like to discuss
Asked: 2022-11-09 11:00:00 +0000
Seen: 7 times
Last updated: Jul 04 '22
In a new Flutter project on Android Studio, is it possible to not have any libs or main.dart file?
How can Google calendar events be generated on the server-side using Flutter?
How can I retrieve an SVG image from a URL in Flutter?
How can I combine streams of different types in Dartlang using rxdart?
What are the steps to disable FlutterFire configuration?
How can zooming similar to desktop be achieved in HTML webview browser using Flutter?
How to imitate Http Client calls during flutter testing?
What is the process of invoking a Cloud Function in Firebase through Flutter?