Flutter 2.2로의 마이그레이션을 하게 되면서 기존의 방법으로는 FCM을 사용할 수 없게 되었다.
공식문서
Cloud Messaging | FlutterFire
To start using the Cloud Messaging package within your project, import it at the top of your project files:
firebase.flutter.dev
예제
FirebaseExtended/flutterfire
🔥 A collection of Firebase plugins for Flutter apps. - FirebaseExtended/flutterfire
github.com
main.dart 일부분
Future<void> _firebaseMessagingBackgroundHandler(RemoteMessage message) async {
// If you're going to use other Firebase services in the background, such as Firestore,
// make sure you call `initializeApp` before using other Firebase services.
await Firebase.initializeApp();
print("Handling a background message: ${message.messageId}");
}
void main() async {
WidgetsFlutterBinding.ensureInitialized();
await Firebase.initializeApp();
// Set the background messaging handler early on, as a named top-level function
FirebaseMessaging.onBackgroundMessage(_firebaseMessagingBackgroundHandler);
runApp(MyApp());
}
IOS 권한 설정
final FirebaseMessaging _firebaseMessaging = FirebaseMessaging.instance;
Future.microtask(() async {
await _firebaseMessaging
.requestPermission(
alert: true,
announcement: false,
badge: true,
carPlay: false,
criticalAlert: false,
provisional: false,
sound: true,
)
.then((settings) {
print('User granted permission: ${settings.authorizationStatus}');
});
await FirebaseMessaging.instance
.setForegroundNotificationPresentationOptions(
alert: true,
badge: true,
sound: true,
);
});
핸들링 메시지
@override
void initState() {
super.initState();
// Firebase
FirebaseMessaging.instance
.getInitialMessage()
.then((RemoteMessage? initialMessage) {
print('initialMessage data: ${initialMessage?.data}');
});
FirebaseMessaging.onMessageOpenedApp.listen((RemoteMessage message) {
print('onMessageOpenedApp data: ${message.data}');
});
FirebaseMessaging.onMessage.listen((RemoteMessage message) {
print('Got a message whilst in the foreground!');
print('onMessage data: ${message.data}');
if (message.notification != null) {
print('Message also contained a notification: ${message.notification}');
}
});
// Firebase
}
'아카이브 > Flutter' 카테고리의 다른 글
[Local Storage] SQFLite, Bloc Pattern활용 (0) | 2021.07.20 |
---|---|
[FCM] Firebase Cloud Messaging 설정 (0) | 2021.07.16 |
[Migration] Flutter 1.22.6에서 2.2.3으로 마이그레이션 하기 (0) | 2021.07.15 |
[GetX] Obx로 Chip 활용하기 (0) | 2021.06.02 |
[위젯] Chip, shape (0) | 2021.05.28 |