'how to Make Notification Service in flutter and method channel(kotlin)
i want to make a notification service without any click of a button,just like other apps such as whatsapp,messaging apps do,whenever message comes,it should show in android status bar.
Solution 1:[1]
I can tell the option if you want to get the message when app is open.Firebase messaging can be handled in 3 states in flutter
//onLaunch(completely closed - not in background)
_firebaseMessaging.getInitialMessage().then((RemoteMessage? message) {
_sendFromNotificationClick(message);
});
//onMessage(app in open)
FirebaseMessaging.onMessage.listen((RemoteMessage? message) {
_sendFromNotificationClick(message, isNavigate: false);
});
//onResume(app in background)
FirebaseMessaging.onMessageOpenedApp.listen((RemoteMessage? message) {
_sendFromNotificationClick(message);
});
_sendFromNotification is a method to handle the state wise click event.You can use onMessage method if you want to show in appbar when the app is open.
void _sendFromNotificationClick(RemoteMessage? message, {bool isNavigate = true}) {
var data = message?.data;
if(data != null) {
var body = data['body'];
var title = data['title'];
var tradeCode = data['tradeCode'];
var companyName = data['companyName'];
var unreadCounterData = data['unreadCount'];
if(unreadCounterData != null && unreadCounterData != "") {
var unreadCounter = int.parse(unreadCounterData);
SharedPref.read(keyJwtToken).then((jwt) {
if(jwt != null) {
if(unreadCounter > 0) {
_notificationController.unreadCounter.value = unreadCounter;
}
if(isNavigate) {
//do your operation on onClick
} else if(title != null && body != null) { //your purpose,if you want to show message in appbar,set the message text as you want
Get.snackbar(
title,
body,
isDismissible: true,
backgroundColor: fadeAshColor,
snackPosition: SnackPosition.BOTTOM,
icon: const Icon(
Icons.notifications,
color: colorPrimary,
),
);
}
} else {
EasyLoading.showError("Something went wrong!");
}
}).catchError((onError) {
EasyLoading.showError(onError.toString());
});
}
}
}
Sources
This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.
Source: Stack Overflow
| Solution | Source |
|---|---|
| Solution 1 | Taqi Tahmid Tanzil |
