Migrating Push notification from firebase_messaging 6 to firebase_messaging 10+
With a little bit of searching I believe the missing piece was flutter_local_notifications: ^5.0.0+4
The changes I have made in
main.dart
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}');
}
const AndroidNotificationChannel channel = const AndroidNotificationChannel(
//for notificaiton initialization
'high_importance_channel', // id
'High Importance Notifications', // title
'This channel is used for important notifications.', // description
importance: Importance.high,
playSound: true,
);
//initialize plugin
final FlutterLocalNotificationsPlugin flutterLocalNotificationsPlugin =
FlutterLocalNotificationsPlugin();
Future<void> main() async {
WidgetsFlutterBinding.ensureInitialized();
await Firebase.initializeApp();
//for background messaging
FirebaseMessaging.onBackgroundMessage(_firebaseMessagingBackgroundHandler);
//Local Notification implementation
await flutterLocalNotificationsPlugin
.resolvePlatformSpecificImplementation<
AndroidFlutterLocalNotificationsPlugin>()
?.createNotificationChannel(channel);
//for firebase plugin and messaging required
await FirebaseMessaging.instance.setForegroundNotificationPresentationOptions(
alert: true, badge: true, sound: true);
runApp(MyApp());
}
In the class where I want the notification
class _ChatScreenState extends State<ChatScreen> {
@override
void initState() {
//foreground messaging
FirebaseMessaging.onMessage.listen((RemoteMessage message) {
RemoteNotification notification = message
.notification; //assign two variables for remotenotification and android notification
AndroidNotification android = message.notification?.android;
if (notification != null && android != null) {
print(message);
flutterLocalNotificationsPlugin.show(
notification.hashCode,
notification.title,
notification.body,
NotificationDetails(
android: AndroidNotificationDetails(
channel.id, channel.name, channel.description,
color: Colors.blue,
playSound: true,
icon: '@mipmap/ic_launcher'),
),
);
print(notification.title);
print(notification.body);
}
});
//Do when the user taps the notification
FirebaseMessaging.onMessageOpenedApp.listen((RemoteMessage message) {
//same as above
});
super.initState();
}
Would be nice if some one update this process for IOS as I don't have a developer Id to test it.
![Pannam T](https://lh5.googleusercontent.com/-yfTYTITybMU/AAAAAAAAAAI/AAAAAAAACF8/1vtqNWvc9tI/photo.jpg?sz=256)
Pannam T
Updated on December 30, 2022Comments
-
Pannam T over 1 year
I am coming from an old Firebase_messaging plugin 6 + to the newer Firebase_messenger plugin 10 +, I am able to do most of the thing but can't get the message data, I want to convert this code from the older plugin to a newer one and use methods like
configure
launch
andonResume
.I can receive the push notifications, foreground and background information about the message but can't read it.class _ChatScreenState extends State<ChatScreen> { @override void initState() { super.initState(); final fbm = FirebaseMessaging(); fbm.requestNotificationPermissions(); fbm.configure(onMessage: (msg) { print(msg); return; }, onLaunch: (msg) { print(msg); return; }, onResume: (msg) { print(msg); return; }); }
What I have done so far Under AndroidManifest.xml added
<meta-data android:name="com.google.firebase.messaging.default_notification_channel_id" android:value="high_importance_channel" />
On
main.dart
Future<void> _firebaseMessagingBackgroundHandler(RemoteMessage message) async { await Firebase.initializeApp(); print('Handling a background message ${message.messageId}'); } Future <void> main() async { WidgetsFlutterBinding.ensureInitialized(); await Firebase.initializeApp(); FirebaseMessaging.onBackgroundMessage(_firebaseMessagingBackgroundHandler); runApp(MyApp()); }
Here is where I want to get the data from the server
class _ChatScreenState extends State<ChatScreen> { @override void initState() { FirebaseMessaging.instance .getInitialMessage() .then((RemoteMessage message) { if (message != null) { print(message); } 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}'); } }); }); super.initState(); }
On Debug while the App is in the foreground
D/FLTFireMsgReceiver(15437): broadcast received for message I/flutter (15437): Got a message whilst in the foreground! I/flutter (15437): Message data: {} I/flutter (15437): Message also contained a notification: Instance of 'RemoteNotification'
On Background
D/FLTFireMsgReceiver(15437): broadcast received for message W/FirebaseMessaging(15437): Missing Default Notification Channel metadata in AndroidManifest. Default value will be used. I/flutter (15437): Handling a background message 0:1624718321445677%ba7e1d8bba7e1d8b
Although the Notification Text and body is fine in the notification window but can't get the same info in the debug screen, it returns empty. Also is my implementation correct?