i want to check if user viewed notification in background or terminated.
if user viewed, show empty container.
if user didn’t view, show like this.
I am using the getx controller variable with a bool value.
And update the screen according to the variable.
This is home.dart. It shows read or unread mark.
// Controller to check read or unread
class UnreadNotiController extends GetxController {
RxBool isUnread = false.obs;
}
Widget menuBox(screenSize) {
final unreadNotiController = Get.put(UnreadNotiController());
return Container(
child: Column(
children: [
Row(...),
Stack(
children: [
Row(... ),
// Show read or unread mark
Obx(() => Positioned(
left: screenSize.width / 2 - 75,
child: unreadNotiController.isUnread.value
? Container(
width: 20,
height: 20,
decoration: BoxDecoration(
color: Colors.red,
shape: BoxShape.circle,
border: Border.all(
color: Colors.white,
width: 2,
),
)
)
: Container()
)
)
],
),
],
),
);
}
This is backgroundHandler
@pragma('vm:entry-point')
Future<void> firebaseMessagingBackgroundHandler(RemoteMessage message) async {
// When receiving a message in background or terminated,
// change the value to an unread message.
final unreadNotiController = Get.put(UnreadNotiController());
unreadNotiController.isUnread.value = true;
// Android channel
const channel = AndroidNotificationChannel(...);
await flutterLocalNotificationsPlugin
.resolvePlatformSpecificImplementation<
AndroidFlutterLocalNotificationsPlugin>()
?.createNotificationChannel(channel);
await flutterLocalNotificationsPlugin.initialize(...);
showNotification(message);
}
Other than setting this notification badge, all notifications work perfectly.
terminated to foreground -> UI is updated normally.
background to foreground -> UI isn’t updated normally.
I think the screen updates normally when the app is completely restarted, but it doesn’t update otherwise. If you have any good opinions, please help