I used postman for Creating a Notification.Also I used Firebase Cloud Messaging as backend.
All works, even Notification pops in the android. But I can't read the data in my debug console ,
the Following errors are shown in console:
D/FLTFireMsgReceiver(23051): broadcast received for message
W/FLTFireMsgService(23051): A background message could not be handled in Dart as no onBackgroundMessage handler has been registered.
W/FirebaseMessaging(23051): Missing Default Notification Channel metadata in AndroidManifest. Default value will be used.
The Codes of the Push Notification class:
import 'dart:io';
import 'package:cholachol_drive/globalvariables.dart';
import 'package:firebase_database/firebase_database.dart';
import 'package:firebase_messaging/firebase_messaging.dart';
class PushNotificationService {
// final FirebaseMessaging fcm = FirebaseMessaging.instance;
Future initialize(context) async {
FirebaseMessaging.onMessage.listen((event) {
(Map<String, dynamic> message) async {
// retrieveRideRequestInfo(getRideRequestId(message), context);
print("onMessage: $message");
if (Platform.isAndroid) {
String rideID = message["data"]['ride_id'];
if (rideID != null) {
print("Fucked");
} else {
print('ride_id: $rideID');
}
}
};
});
FirebaseMessaging.onMessageOpenedApp.listen((event) {
(Map<String, dynamic> message) async {
// retrieveRideRequestInfo(getRideRequestId(message), context);
print("onLaunch: $message");
if (Platform.isAndroid) {
String rideID = message["data"]['ride_id'];
if (rideID != null) {
print("Fucked");
} else {
print('ride_id: $rideID');
}
}
};
});
}
Future<String> getToken() async {
String token = await FirebaseMessaging.instance.getToken();
print("token: $token");
DatabaseReference tokerRef = FirebaseDatabase.instance
.ref()
.child("drivers/${currentFirebaseUser.uid}/token");
FirebaseMessaging.instance.subscribeToTopic('alldrivers');
FirebaseMessaging.instance.subscribeToTopic('allusers');
}
}
It seems you don't have onBackgroundMessage implemented in your app. You need to use onBackgroundMessage in order to handle background messages. onMessage will only handle foreground messages.
Related
I'm on creating a flutter application with Firebase Cloud Messaging and cloud-functions I'm trying to send notification via FCM with flutter But all what I goit is broadcast received for message in the terminal. and Exception from a finished function: Error: tokens must be a non-empty array in cloud-functions debug
this is my code :
Future<void> configure() async {
await _firebaseMessaging.getToken().then((token) async {
print("FCM: $token");
await _firestore
.collection("flutterTokens")
.doc()
.set({'token': token});
});
}
Future init() async {
final settings = await _requestPermission();
//await _getToken();
await configure();
//await _getToken();
// _registerForegroundMessageHandler();
}
Future<void> _messageHandler(RemoteMessage message) async {
print(
'background message ${message.notification!.body} + ${message.notification!.title}');
}
in the initState():
await _msgService.init();
FirebaseMessaging.onBackgroundMessage(_messageHandler);
Since the firebase_messaging get ^14.2.1 version I can't find an answer to how can I resolve the problem.
When I send a post from the Postman everything is fine, I get a notification on the blocked phone, but I have an error in the console:
W/FLTFireMsgService(13437): A background message could not be handled in Dart as no onBackgroundMessage handler has been registered.
W/FirebaseMessaging(13437): Missing Default Notification Channel metadata in AndroidManifest. Default value will be used.
**The Code is implemented **
import 'dart:io';
import 'package:cholachol_drive/globalvariables.dart';
import 'package:cholachol_drive/main.dart';
import 'package:firebase_core/firebase_core.dart';
import 'package:firebase_database/firebase_database.dart';
import 'package:firebase_messaging/firebase_messaging.dart';
import 'package:flutter/material.dart';
class PushNotificationService {
FirebaseMessaging fcm = FirebaseMessaging.instance;
Future initialize(context) async {
FirebaseMessaging.onMessage.listen((RemoteMessage message) {
print('Got a message whilst in the foreground!');
print('Message data: ${message.data}');
print('Message also contained a notification: ${message.notification}');
// if (message.notification != null) {}
});
//Background
Future<void> _firebaseMessagingBackgroundHandler(
RemoteMessage message) async {
await Firebase.initializeApp();
print("Handling a background message: ${message.messageId}");
}
}
Future<String> getToken() async {
String token = await FirebaseMessaging.instance.getToken();
print("token: $token");
DatabaseReference tokerRef = FirebaseDatabase.instance
.ref()
.child("drivers/${currentFirebaseUser.uid}/token");
FirebaseMessaging.instance.subscribeToTopic('alldrivers');
FirebaseMessaging.instance.subscribeToTopic('allusers');
}
String getRideID(Map<String, dynamic> message) {
print("onLaunch: $message");
String rideID = "";
if (Platform.isAndroid) {
rideID = message["data"]['ride_id'];
if (rideID != null) {
print("Broken");
} else {
print('ride_id: $rideID');
}
}
return rideID;
}
}
Future initialize(context) async {
FirebaseMessaging.instance.getNotificationSettings(
FirebaseMessaging.onMessage.listen((Map<String, dynamic> message)) async {
retrieveRideRequestInfo(getRideRequestId(message), context);
},
onLaunch: (Map<String, dynamic> message) async {
retrieveRideRequestInfo(getRideRequestId(message), context);
},
onResume: (Map<String, dynamic> message) async {
print("onResume: $message");
getRideRequestId(message);
retrieveRideRequestInfo(getRideRequestId(message), context);
},
);
}
Instead of on Message use :
FirebaseMessaging.onMessage.listen((RemoteMessage message) {
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}');
}
});
for background message
Future<void> _firebaseMessagingBackgroundHandler(RemoteMessage message) async {
// If you're going to use other Firebase services in the background, such as Firestore,
// make sure you call `initializeApp` before using other Firebase services.
await Firebase.initializeApp();
print("Handling a background message: ${message.messageId}");
}
void main() {
FirebaseMessaging.onBackgroundMessage(_firebaseMessagingBackgroundHandler);
runApp(MyApp());
}
Note background message is handled in separate isolate if you are using firebase you have to initialise it first to use it.
Reference:
Foreground:https://firebase.flutter.dev/docs/messaging/usage#foreground-messages
Background:https://firebase.flutter.dev/docs/messaging/usage#background-messages
I have successfully setup background notifications and tested it using postman and all is good.
Now I need to access Provider.of(context) in my backgroundHandler which must be a static method where there is no context.
All I need to do is to perform an action according to the data in the background notification.
Here is my code for initializing FCM (I do it in Splash screen)
Future<void> initFcm() async {
_firebaseMessaging.configure(
onBackgroundMessage: myBackgroundMessageHandler,
onMessage: (msg) async {
print('this is ONMESSAGE $msg');
},
onLaunch: (msg) async {
print('ON LAUNCH');
},
onResume: (msg) async {
print('ON RESUME');
},
);
// For testing purposes print the Firebase Messaging token
deviceToken = await _firebaseMessaging.getToken();
print("FirebaseMessaging token: $deviceToken");
}
static Future<dynamic> myBackgroundMessageHandler(
Map<String, dynamic> message) async {
print(message);
if (message.containsKey('data')) {
// Handle data message
final dynamic data = message['data'];
final orderId = data['order_id'];
//THE PROBLEM IS THAT HERE I DON'T HAVE CONTEXT (coz static method)
Provider.of<Orders>(context, listen: false).setBackgroundStatus(orderId);
}
if (message.containsKey('notification')) {
// Handle notification message
final dynamic notification = message['notification'];
}
// Or do other work.
}
My question is how can I handle tasks in provider in my backgroundHandler which is a static method that doesn't have context?
I am using firebase cloud messaging to push notification on my app that contains a chat page.
I defined my firebase push functions on my main.dart as below:
_firebaseMessaging.configure(
onMessage: (Map<String, dynamic> message) async {
print("onMessage: $message");
//_showItemDialog(message);
},
onBackgroundMessage: myBackgroundMessageHandler,
onLaunch: (Map<String, dynamic> message) async {
print("onLaunch: $message");
//_navigateToItemDetail(message);
},
onResume: (Map<String, dynamic> message) async {
print("onResume: $message");
//_navigateToItemDetail(message);
},
);
When chat widget opened and i receive a push notification, my OnMessage method is reached normally.
The question is: what is the best way to refresh my chat page considering that the opened page is not the same one where the reached OnMessage function is declared?
I have used following peice of code for different question on StackOverflow. But the problem there is entirely different from yours, so pasting relevant code.
You can use BLOC here. FCM/NotificationService will send notifications to BLOC/NotificationsBloc and all the widgets that need notifications can subscribe for the notifications. Sample implementation
BLOC
import 'package:rxdart/rxdart.dart';
class LocalNotification {
final String type;
final Map data;
LocalNotification(this.type, this.data);
}
class NotificationsBloc {
NotificationsBloc._internal();
static final NotificationsBloc instance = NotificationsBloc._internal();
final BehaviorSubject<LocalNotification> _notificationsStreamController = BehaviorSubject<LocalNotification>();
Stream<LocalNotification> get notificationsStream {
return _notificationsStreamController;
}
void newNotification(LocalNotification notification) {
_notificationsStreamController.sink.add(notification);
}
void dispose() {
_notificationsStreamController?.close();
}
}
FCM Listener (NotificationService)
import 'package:firebase_messaging/firebase_messaging.dart';
import 'notifications_bloc.dart';
class LocalNotificationService {
final FirebaseMessaging _firebaseMessaging = FirebaseMessaging();
bool _started = false;
LocalNotificationService._internal();
static final LocalNotificationService instance = LocalNotificationService._internal();
// ********************************************************* //
// YOU HAVE TO CALL THIS FROM SOMEWHERE (May be main widget)
// ********************************************************* //
void start() {
if (!_started) {
_start();
_started = true;
_refreshToken();
}
}
void _refreshToken() {
_firebaseMessaging.getToken().then(_tokenRefresh, onError: _tokenRefreshFailure);
}
void _start() {
_firebaseMessaging.requestNotificationPermissions();
_firebaseMessaging.onTokenRefresh.listen(_tokenRefresh, onError: _tokenRefreshFailure);
_firebaseMessaging.configure(
onMessage: _onMessage,
onLaunch: _onLaunch,
onResume: _onResume,
);
}
void _tokenRefresh(String newToken) async {
print(" New FCM Token $newToken");
}
void _tokenRefreshFailure(error) {
print("FCM token refresh failed with error $error");
}
Future<void> _onMessage(Map<String, dynamic> message) async {
print("onMessage $message");
if (message['notification'] != null) {
final notification = LocalNotification("notification", message['notification'] as Map);
NotificationsBloc.instance.newNotification(notification);
return null;
}
if (message['data'] != null) {
final notification = LocalNotification("data", message['data'] as Map);
NotificationsBloc.instance.newNotification(notification);
return null;
}
}
Future<void> _onLaunch(Map<String, dynamic> message) {
print("onLaunch $message");
return null;
}
Future<void> _onResume(Map<String, dynamic> message) {
print("onResume $message");
return null;
}
}
Finally in your Widget
Stream<LocalNotification> _notificationsStream;
#override
void initState() {
super.initState();
_notificationsStream = NotificationsBloc.instance.notificationsStream;
_notificationsStream.listen((notification) {
// TODO: Implement your logic here
print('Notification: $notification');
});
}
#override
void dispose() {
super.dispose();
}
Hope this is what you are looking for.