FCM 설정관련은 여기 참고
어떤 상황(ex ios foregorund) 에서는
앱을 설치 후 한번 껐다가 다시 킨 후에 테스트 해야 fcm이 제대로 들어왔던것 같은데 기억이 안난다 ㅠㅠ
/**
* 안드로이드 terminate 상태의 FCM은 릴리즈 모드에서 테스트 해야함!!
* */
('vm:entry-point')
Future<void> _firebaseMessagingBackgroundHandler(RemoteMessage message) async {
//background, terminated 처리
print('Handling a background messages ${message.notification?.body}');
//background는 _handleMessage를 탐
//terminated는 처음부터 앱이 실행되기 때문에 runApp(MyApp())처음부터 실행됨
}
const AndroidNotificationChannel channel = AndroidNotificationChannel(
'high_importance_channel',
'High Importance Notifications',
description: 'This channel is used for important notifications.',
importance: Importance.max,
);
final FlutterLocalNotificationsPlugin flutterLocalNotificationsPlugin =
FlutterLocalNotificationsPlugin();
final StreamController<String?> selectNotificationStream =
StreamController<String?>.broadcast();
onFcmForegroundMessageListener() {
//foreground 처리
FirebaseMessaging.onMessage.listen(
(RemoteMessage rm) async {
debugPrint('## in onMessage');
RemoteNotification? notification = rm.notification;
AndroidNotification? android = rm.notification?.android;
//안드로이드 forground는 local noti를 이용함
if (notification != null && android != null) {
flutterLocalNotificationsPlugin.show(
notification.hashCode,
notification.title,
notification.body,
NotificationDetails(
android: AndroidNotificationDetails(
channel.id,
channel.name,
channelDescription: channel.description,
icon: '@mipmap/launcher_icon',
),
),
payload: rm.data['click_action'],
);
}
},
);
}
void main() async {
WidgetsFlutterBinding.ensureInitialized();
await Firebase.initializeApp();
FirebaseMessaging.onBackgroundMessage(_firebaseMessagingBackgroundHandler);
runApp(MyApp());
}
class MyApp extends StatefulWidget {
MyApp({Key? key}) : super(key: key);
State<MyApp> createState() => _MyAppState();
}
class _MyAppState extends State<MyApp> {
//fcm init
setupInteractedMessage() async {
//애플리케이션이 종료된 상태에서 열리면 RemoteMessage가 포함된 Future가 반환됩니다. 소비되면 RemoteMessage가 삭제됩니다.
RemoteMessage? initialMessage =
await FirebaseMessaging.instance.getInitialMessage();
if (initialMessage != null) {
_handleMessage(initialMessage);
}
//애플리케이션이 백그라운드 상태에서 열릴 때 RemoteMessage를 게시하는 Stream입니다.
FirebaseMessaging.onMessageOpenedApp.listen(_handleMessage);
}
//안드로이드 포그라운드 제외 클릭 시 모두 여기 들어옴
void _handleMessage(RemoteMessage message) {
debugPrint('## in _handleMessage ${message.notification?.body}');
}
void initState() {
//알림 권한 받기
FirebaseMessaging.instance.requestPermission(
badge: true,
alert: true,
sound: true,
);
//안드로이드 fcm 포그라운드 처리
flutterLocalNotificationsPlugin
.resolvePlatformSpecificImplementation<
AndroidFlutterLocalNotificationsPlugin>()
?.createNotificationChannel(channel);
setupInteractedMessage();
//ios 포그라운드에서 알림 보이도록 설정
FirebaseMessaging.instance.setForegroundNotificationPresentationOptions(
alert: true,
badge: true,
sound: true,
);
onFcmForegroundMessageListener();
onClickAndroidForegroundFcm();
super.initState();
}
//안드 포그라운드 로컬 로티 클릭 처리
void onClickAndroidForegroundFcm() {
selectNotificationStream.stream.listen((String? payload) async {
//Get.offAllNamed(MainRoutes.mainPage.name);
});
}
Widget build(BuildContext context) {}
}