You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
If a notification alert is generated after receiving a push from the current app, the number is exposed on the badge according to the number.
I am using flutter_app_badger to use the badge as I want.
If you set the number through the package, it will be exposed normally, but as soon as the number becomes 0, the number of notification alerts will be exposed again.
How can I prevent badges from being exposed according to the number of notification alerts?
This is my code.
import 'dart:io';
import 'package:firebase_core/firebase_core.dart';
import 'package:firebase_messaging/firebase_messaging.dart';
import 'package:flutter_local_notifications/flutter_local_notifications.dart';
import 'package:get/get.dart';
import 'package:webview_flutter/webview_flutter.dart';
import 'package:flutter_app_badger/flutter_app_badger.dart';
/// Firebase Background Messaging 핸들러
@pragma('vm:entry-point')
Future<void> firebaseMessagingBackgroundHandler(RemoteMessage message) async {
print(message.data['noViewCnt'] as String);
FlutterAppBadger.updateBadgeCount(
int.parse(message.data['noViewCnt'] as String));
}
class AppController extends GetxController {
static AppController get to => Get.find();
final Completer<WebViewController> webViewController =
Completer<WebViewController>();
/// iOS 권한을 요청하는 함수
Future reqIOSPermission(FirebaseMessaging fbMsg) async {
await fbMsg.requestPermission(
alert: true,
announcement: false,
badge: true,
carPlay: false,
criticalAlert: false,
provisional: false,
sound: true,
);
}
/// Firebase Foreground Messaging 핸들러
Future<void> fbMsgForegroundHandler(
RemoteMessage message,
FlutterLocalNotificationsPlugin flutterLocalNotificationsPlugin,
AndroidNotificationChannel channel) async {
print('[FCM - Foreground] MESSAGE : ${message.data}');
if (message.notification != null) {
print('Message also contained a notification: ${message.notification}');
if (Platform.isIOS) {
flutterLocalNotificationsPlugin.show(
message.hashCode,
message.notification?.title,
message.notification?.body,
NotificationDetails(
iOS: const IOSNotificationDetails(
presentAlert: true,
presentBadge: true,
presentSound: true,
)));
} else if (Platform.isAndroid) {
flutterLocalNotificationsPlugin.show(
message.hashCode,
message.notification?.title,
message.notification?.body,
NotificationDetails(
android: AndroidNotificationDetails(
channel.id, channel.name, channel.description,
icon: '@mipmap/app_icon', channelShowBadge: false)));
}
}
FlutterAppBadger.updateBadgeCount(
int.parse(message.data['noViewCnt'] as String));
}
/// FCM 메시지 클릭 이벤트 정의
Future<void> setupInteractedMessage(FirebaseMessaging fbMsg) async {
RemoteMessage initialMessage = await fbMsg.getInitialMessage();
// 종료상태에서 클릭한 푸시 알림 메세지 핸들링
if (initialMessage != null) clickMessageEvent(initialMessage);
// 앱이 백그라운드 상태에서 푸시 알림 클릭 하여 열릴 경우 메세지 스트림을 통해 처리
FirebaseMessaging.onMessageOpenedApp.listen(clickMessageEvent);
}
void clickMessageEvent(RemoteMessage message) async {
print(message.data);
String type = message.data['type'] as String;
switch (type) {
case 'chat':
{
// 해당 채팅방 이동 (url: https://dailyh.aiaracorp.com/chat/{roomId} )
WebViewController controller = await webViewController.future;
String roomId = message.data['roomId'] as String;
controller.loadUrl('https://dailyh.aiaracorp.com/chat/$roomId');
break;
}
case 'notice':
{
// 단순히 앱 실행
// 이미 앱이 실행 된 상태라 여기서 추가로 해줄게 없음
break;
}
}
}
Future<bool> initialize() async {
await Firebase.initializeApp();
FirebaseMessaging fbMsg = FirebaseMessaging.instance;
// 플랫폼 확인후 권한요청 및 Flutter Local Notification Plugin 설정
FlutterLocalNotificationsPlugin flutterLocalNotificationsPlugin =
FlutterLocalNotificationsPlugin();
AndroidNotificationChannel androidNotificationChannel;
if (Platform.isIOS) {
await reqIOSPermission(fbMsg);
} else if (Platform.isAndroid) {
// Android 8 (API 26) 이상부터는 채널설정이 필수.
androidNotificationChannel = const AndroidNotificationChannel(
'dailyh_channel',
'DailyH_Notifications',
'데일리에이치 안드로이드 푸시 채널',
importance: Importance.high,
);
await flutterLocalNotificationsPlugin
.resolvePlatformSpecificImplementation<
AndroidFlutterLocalNotificationsPlugin>()
?.createNotificationChannel(androidNotificationChannel);
}
// Background Handling 백그라운드 메세지 핸들링
FirebaseMessaging.onBackgroundMessage(firebaseMessagingBackgroundHandler);
// Foreground Handling 포어그라운드 메세지 핸들링
FirebaseMessaging.onMessage.listen((message) {
fbMsgForegroundHandler(
message, flutterLocalNotificationsPlugin, androidNotificationChannel);
});
// Message Click Event Implement
await setupInteractedMessage(fbMsg);
return true;
}
void setWebViewController(WebViewController controller) {
webViewController.complete(controller);
}
}
reacted with thumbs up emoji reacted with thumbs down emoji reacted with laugh emoji reacted with hooray emoji reacted with confused emoji reacted with heart emoji reacted with rocket emoji reacted with eyes emoji
-
If a notification alert is generated after receiving a push from the current app, the number is exposed on the badge according to the number.
I am using flutter_app_badger to use the badge as I want.
If you set the number through the package, it will be exposed normally, but as soon as the number becomes 0, the number of notification alerts will be exposed again.
How can I prevent badges from being exposed according to the number of notification alerts?
This is my code.
Beta Was this translation helpful? Give feedback.
All reactions