Flutter FirebaseMessaging setup - flutter

In initState() of the _PushMessagingExampleState class in the example app for Flutter firebase_messaging 6.0.13, _firebaseMessaging.configure is called first, followed by _firebaseMessaging.requestNotificationPermissions.
However, in the firebase_messaging 6.0.13 API reference for the FirebaseMessaging class, the first thing it states is:
Your app should call requestNotificationPermissions first and then
register handlers for incoming messages with configure.
Which is correct?
I'm guessing the API reference is correct and the example app could be improved?

Technically it doesn't matter but I have it set up as request permissions first, then configure. As an example of a working app:
configureFcm() {
if (!isConfigured) {
if (Platform.isIOS) {
_fcm.onIosSettingsRegistered.listen((IosNotificationSettings data) {
});
_fcm.requestNotificationPermissions(IosNotificationSettings(sound: true, badge: true, alert: true));
}
_fcm.configure(
// in foreground
onMessage: (Map<String, dynamic> message) async {
_handleNotification(message);
},
// onBackgroundMessage: (Map<String, dynamic> message) async {
// print('on background message $message');
// },
// in background
onResume: (Map<String, dynamic> message) async {
_handleNotification(message);
},
// terminated
onLaunch: (Map<String, dynamic> message) async {
_handleNotification(message);
},
);
isConfigured = true;
}
}

Related

The method 'configure' isn't defined for the type 'FirebaseMessaging'. Can anyone retype my code correctly? Thanks

Push Notification code.
I don't understand how to implement the new method of implementing a notification in flutter using firebase
import 'dart:io' show Platform;
import 'package:google_maps_flutter/google_maps_flutter.dart';
class PushNotificationService
{
final firebaseMessaging = FirebaseMessaging.instance;
Future initialize(context) async
{
firebaseMessaging.configure(
onMessage: (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 {
retrieveRideRequestInfo(getRideRequestId(message), context);
},
);
}
Seems like you are using the old method. Here is the updated version. Link for your references
FirebaseMessaging.onMessage.listen((RemoteMessage message) {
RemoteNotification notification = message.notification;
showNotification(notification);});
FirebaseMessaging.onMessageOpenedApp.listen((RemoteMessage message) {
print("onMessageOpenedApp: $message");});
FirebaseMessaging.onBackgroundMessage((RemoteMessage message) {
print("onBackgroundMessage: $message");});

when I wrote this code in my project but the version of firebase_messaging is ^9.1.2 so anyone can tell me what i can change to run correctly

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

onBackgroundMessage not getting called flutter messaging

Am getting a frustrating experience with firebase messaging on my flutter app as onBackgroundmessage is not getting triggered while app is in background after sending request. I would like to know why as this is a core feature my app needs.
Future initialize(context) async{
_firebaseMessaging.configure(
onBackgroundMessage: Platform.isIOS ? null:_myBackgroundMessageHandler,
);
}
Future<dynamic> _myBackgroundMessageHandler
(Map<String, dynamic> message) async {
print("onBackground Message called");
fetchRideInfoInBackground();
return Future<void>.value();
}
}
Notification Payload
const message = {
notification: {
title: 'New Incoming Request',
body: `New incoming ${service} request`,
},
data: { orderId: orderId },
android: {
ttl: 30,
priority: 'high',
notification: {
click_action: 'FLUTTER_NOTIFICATION_CLICK',
},
},
tokens: driversRegistrationToken,
};
I don't see the whole file, but I assume your _myBackgroundMessageHandler is not a top level function.
Make it top level or static, after that it should work.
Edit: example
Future<dynamic> _myBackgroundMessageHandler (Map<String, dynamic> message) async {
print("onBackground Message called");
fetchRideInfoInBackground();
return Future<void>.value();
}
class PushHandler {
final FirebaseMessaging _firebaseMessaging = FirebaseMessaging();
Future initialize(context) async{
_firebaseMessaging.configure(
onBackgroundMessage: Platform.isIOS ? null:_myBackgroundMessageHandler,
);
}
}

Broadcast fcm notification throughout the app in flutter

The question is I want to receive FCM notification from different view because I have notification bell icon which is updated whenever the notification is received.I have Profile View, Landing View, Dashboard View and others. So I have badge in there which needs update. However, I cannot receive the broadcast either using Stream or Notifier. The only option I have is to add a bulky line to each of these pages. i.e
_firebaseMessaging.configure(
onMessage: (Map<String, dynamic> message) async {
print("onMessage: $message");
setState(() {
counter++;
});
_newNotification = true;
_showNotificationWithDefaultSound(message.toString());
},
onBackgroundMessage: myBackgroundMessageHandler,
onLaunch: (Map<String, dynamic> message) async {
setState(() {
counter++;
});
print("onLaunch: $message");
},
onResume: (Map<String, dynamic> message) async {
setState(() {
counter++;
});
print("onResume: $message");
},
);
And also I need to add couple of functions to get it working.So my question is how do you receive it with the minimum possible lines.As I have tried Stream broadcast which only works for single page and I cant listen it from another page.Anyone if they had this issue please share.All in the internet I am getting this solution and which is not applicable for me .
void initState() {
super.initState();
print("Creating a sample stream...");
Stream<String> stream = new Stream.fromFuture(getData());
print("Created the stream");
stream.listen((data) {
print("DataReceived: "+data);
}, onDone: () {
print("Task Done");
}, onError: (error) {
print("Some Error");
});
print("code controller is here");
}
From this link which is good example

How to open specific activity on click of push notification from Firebase flutter

I'm getting push notification using firebase messaging service in my flutter application.I need to redirect the app to specific activity when I clicks the notification. Could you people suggest some ideas please? Thanks in advance.
This is the code I tried.
var initializationSettingsAndroid = new AndroidInitializationSettings(
'#mipmap/ic_launcher'); // Notification Initialization for android
var initializationSettingsIOS = new IOSInitializationSettings(); // Notification Initialization for IOS
var initializationSettings = new InitializationSettings(
initializationSettingsAndroid,
initializationSettingsIOS); //Initialization done according to their platform
flutterLocalNotificationsPlugin = new FlutterLocalNotificationsPlugin();
flutterLocalNotificationsPlugin.initialize(initializationSettings);
firebaseMessaging
.configure( // handling notification while app is in foreground
onLaunch: (Map<String, dynamic> message) async {
print('onlaunch');
//navigateTo(message);
Navigator.push(context, new MaterialPageRoute(builder: (BuildContext context)=> new Credits()));
},
onResume: (Map<String, dynamic> message) async {
print('onmesage $message');
print('onResume');
// navigateTo(message);
Navigator.push(context, new MaterialPageRoute(builder: (BuildContext context)=> new Credits()));
},
onMessage: (Map<String, dynamic> message) async {
print('onmesage $message');
body = message['notification']['body'];
title = message['notification']['title'];
Timer(Duration(seconds: 1), (){});
showNotificationWithDefaultSound(message);
print("title: $title");
print("body: $body");
//print("${message['data']['screen']}");
//Navigator.of(context).pushNamed(message['data']['screen']);
},
);
//Permission for IOS
firebaseMessaging.requestNotificationPermissions(
const IosNotificationSettings(
alert: true,
sound: true,
badge: true
)
);
firebaseMessaging.onIosSettingsRegistered.listen((
IosNotificationSettings settings) {
print("IOS");
});
//geting token from the app
firebaseMessaging.getToken().then((token) {
tokenId = token;
print(tokenId);
});
//
Firebase messaging object provide you a configure method in you have onMessage, onResume and onLaunch where you can run code of your will. For your requirement I believe you have to call new page/activity such that when your user clicks on notification (when app is terminated i.e not in foreground and background) it will launch that page instead of main page. Implement this code in initState() of the root page
final FirebaseMessaging _firebaseMessaging = FirebaseMessaging();
_firebaseMessaging.configure(
onMessage: (Map<String, dynamic> message) async{
Print('on message $message');
},
onResume: (Map<String, dynamic> message) async {
print('on resume $message');
},
onLaunch: (Map<String, dynamic> message) async {
print('on launch $message');
//Call your pageRoute here
},
);