본문 바로가기
카테고리 없음

Firebase 번역 -Firebase Cloud Messaging(1-4) -flutter

by dev_caleb 2022. 12. 6.
728x90

 

Receive messages in a Flutter app(flutter app message 받기)

 

https://firebase.google.com/docs/cloud-messaging/flutter/receive?hl=en 

 

Flutter 앱에서 메시지 수신  |  Firebase 클라우드 메시징

Firebase Summit에서 발표된 모든 내용을 살펴보고 Firebase로 앱을 빠르게 개발하고 안심하고 앱을 실행하는 방법을 알아보세요. 자세히 알아보기 이 페이지는 Cloud Translation API를 통해 번역되었습니

firebase.google.com

 

Depending on a device's state, incoming messages are handled differently. To understand these scenarios and how to integrate FCM into your own application, it is first important to establish the various states a device can be in:

 

Device state에 따라, 들어오는 메시지는 다르게 handle 된다. 이들 시나리오들을 이해하고 어떻게 FCM을 자신의 FCM에 integrate하는지 알기 위해, 먼저 장치가 있을 수 있는 다양한 상태를 설정하는 것이 중요합니다.

 

There are a few preconditions which must be met before the application can receive message payloads via FCM:

  • The application must have opened at least once (to allow for registration with FCM).
  • On iOS, if the user swipes away the application from the app switcher, it must be manually reopened for background messages to start working again.
  • On Android, if the user force-quits the app from device settings, it must be manually reopened for messages to start working.
  • On web, you must have requested a token (using getToken()) with your web push certificate.

앱이 FCM을 통해서 payload 메시지를 받기 전에 몇 가지 맞추어야 하는 precondition이 있다. 

- Application은 최소한 한 번 켜져야 한다.(FCM registration 을 허락하기 위해)

- IOS 에서, 사용자가 앱 전환기에서 애플리케이션을 스와이프하면 백그라운드 메시지가 다시 작동하려면 수동으로 다시 열려야 합니다.

-android에서, 사용자가 장치 설정에서 앱을 강제로 종료하는 경우 메시지가 작동하려면 앱을 수동으로 다시 열어야 합니다.

 

 

Message handling 메시지 handling

Based on your application's current state, incoming payloads of different message types require different implementations to handle them:

당신의 애플리케이션 상태에 따라, 들어오는 다른 타입의 payloads는 다르게 처리하려면 그들을 다르게 구현하도록 handle해야할 필요가 있다. 

 

Foreground messages

 

To handle messages while your application is in the foreground, listen to the onMessage stream.

application이 foreground에 있을 때 message를 handle하기 위해, onMessage stream을 읽어라.

 

FirebaseMessaging.onMessage.listen((RemoteMessage message) {
  print('Got a message whilst in the foreground!');
  print('Message data: ${message.data}');

  if (message.notification != null) {
    print('Message also contained a notification: ${message.notification}');
  }
});

 

The stream contains a RemoteMessage, detailing various information about the payload, such as where it was from, the unique ID, sent time, whether it contained a notification and more. Since the message was retrieved whilst your application is in the foreground, you can directly access your Flutter application's state and context.

stream은 remoteMessage, payload에 관한 다양한 정보를 상세히 설명한다.( 어디서 온 것인지, unique Id, 보낸시간, notificiation을  포함했는지 여부 등) 메시지는 application이 foreground에 있을 때 받아졌기 때문에, 당신은 Flutter application state와 context에서 바로 access 할 수 있다. 

 

 

Foreground and Notification messages

Notification messages which arrive while the application is in the foreground will not display a visible notification by default, on both Android and iOS. It is, however, possible to override this behavior:

application이 foreground에 있을 때 발생하는 Notification messages는 기본적으로 visible notification을 display하지 않는다. (android와 ios 환경 모두) 그러나 이 행동을 override 할 수 있다.

  • On Android, you must create a "High Priority" notification channel.
  • On iOS, you can update the presentation options for the application.

 

Background messages

The process of handling background messages is different on native (Android and Apple) and web based platforms.

handling background message 프로세스는 native와 웹 베이스 플랫폼이 다르다.

 

Apple platforms and Android

Handle background messages by registering a onBackgroundMessage handler. When messages are received, an isolate is spawned (Android only, iOS/macOS does not require a separate isolate) allowing you to handle messages even when your application is not running.

 

 

There are a few things to keep in mind about your background message handler:

  1. It must not be an anonymous function.
  2. It must be a top-level function (e.g. not a class method which requires initialization).
  3. It must be annotated with @pragma('vm:entry-point') right above the function declaration (otherwise it may be removed during tree shaking for release mode).
@pragma('vm:entry-point')
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() {
  FirebaseMessaging.onBackgroundMessage(_firebaseMessagingBackgroundHandler);
  runApp(MyApp());
}

 

 

 

728x90