앱에 푸시 알림 붙였을 때, 안드로이드는 잘 동작하는데 iOS에서는 알림이 두 번씩 오는 문제가 생길 때가 있다. 특히 Firebase Cloud Messaging(FCM) 쓰고 있다면 이거 한 번쯤 겪었을 이슈다.
왜 이런 일이 생기는지, 그리고 어떻게 해결하는지 정리해봤다.
이중 표시의 원인은 다음 두 가지 알림 처리 방식이 동시에 작동하기 때문이다:
Firebase iOS SDK에서 제공하는 기능으로, setForegroundNotificationPresentationOptions를 통해 포그라운드 상태에서도 시스템이 알림을 띄울 수 있다.
await FirebaseMessaging.instance.setForegroundNotificationPresentationOptions(
alert: true,
badge: true,
sound: true,
);
FirebaseMessaging.onMessage.listen에서 flutter_local_notifications 등을 사용해 알림을 수동으로 띄우는 방식.
FirebaseMessaging.onMessage.listen((RemoteMessage message) {
flutterLocalNotificationsPlugin.show(
...
);
});
이 두 방식이 동시에 작동하면 동일한 알림이 2번 노출되는 것이다.
await FirebaseMessaging.instance.setForegroundNotificationPresentationOptions(
alert: false, // ← 이 부분이 핵심
badge: true,
sound: true,
);
이렇게 하면 iOS 시스템에서 알림을 자동으로 띄우지 않으므로, 개발자가 onMessage.listen 안에서 수동으로만 표시하면 된다.
onMessage.listen 내부에서 iOS인지 확인 후 처리하지 않도록 조건을 둘 수 있다.
if (Platform.isAndroid) {
flutterLocalNotificationsPlugin.show(
...
);
}