push_notification_manager

μš©λ„

λ°±κ·ΈλΌμš΄λ“œμ—μ„œ ν‘Έμ‹œ ν•˜λŠ” 것은 μ‰¬μš΄λ° ν¬κ·ΈλΌμš΄λ“œμ—μ„œ ν‘Έμ‹œλ₯Ό μ²˜λ¦¬ν• λ•Œ μ•ˆλ“œλ‘œμ΄λ“œμ—μ„œ μΆ”κ°€μ μœΌλ‘œ ν•΄μ€˜μ•Ό ν•  사항이 μžˆλ‹€. 이 경우의 ν‘Έμ‹œ 처리λ₯Ό μœ„ν•΄μ„œ 좔가적인 μ½”λ“œ κ΅¬ν˜„μ΄ ν•„μš”ν•˜κ³  push_notification_manager λŠ” 이 처리λ₯Ό λ‹΄λ‹Ήν•˜λŠ” 객체이닀.

κ΅¬ν˜„

일단 κ΅¬ν˜„ν–ˆμ—ˆλ˜ μ½”λ“œλ₯Ό κ·ΈλŒ€λ‘œ 뢙인닀. μ•„λž˜ μ½”λ“œ 말고도 κΆŒν•œ μ„€μ •λ“± ν•΄μ€˜μ•Ό ν•  λ‚΄μš©λ“€μ΄ 더 μžˆλ‹€. μ•ˆλ“œλ‘œμ΄λ“œμ˜ 경우 ν•΄μ£Όκ³  μžˆλŠ” 처리λ₯Ό κ°„λ‹¨νžˆ μ„€λͺ…ν•˜μžλ©΄ μ•Œλ¦Ό 채널을 ν•˜λ‚˜ μƒμ„±ν•΄μ„œ μ€‘μš”λ„κ°€ λ†’λ‹€κ³  μ»€μŠ€ν…€ν•˜κ²Œ Importance λ₯Ό μ„€μ •ν•΄μ€€λ‹€.

이 채널을 ν†΅ν•΄μ„œ 둜컬 μ•Œλ¦Όμ„ ν¬κ·ΈλΌμš΄λ“œμ—μ„œ 내보내도둝 μ²˜λ¦¬ν•œλ‹€.

import 'dart:io';

import 'package:firebase_messaging/firebase_messaging.dart';
import 'package:flutter_local_notifications/flutter_local_notifications.dart';

class PushNotificationManager {
  static Future<void> initializeNotification() async {
    if (Platform.isIOS) {
      await FirebaseMessaging.instance
          .setForegroundNotificationPresentationOptions(
        alert: true,
        badge: true,
        sound: true,
      );
      return;
    }

    const AndroidNotificationChannel androidNotificationChannel =
        AndroidNotificationChannel(
      'high_importance_channel', // μž„μ˜μ˜ id
      'High Importance Notifications', // 섀정에 보일 채널λͺ…
      importance: Importance.max,
    );

    final FlutterLocalNotificationsPlugin flutterLocalNotificationsPlugin =
        FlutterLocalNotificationsPlugin();
    await flutterLocalNotificationsPlugin
        .resolvePlatformSpecificImplementation<
            AndroidFlutterLocalNotificationsPlugin>()
        ?.createNotificationChannel(androidNotificationChannel);

    await flutterLocalNotificationsPlugin.initialize(
        const InitializationSettings(
            android: AndroidInitializationSettings('@mipmap/ic_launcher')));

    FirebaseMessaging.onMessage.listen((RemoteMessage remoteMessage) {
      RemoteNotification? notification = remoteMessage.notification;
      AndroidNotification? android = remoteMessage.notification?.android;

      if (notification != null && android != null) {
        flutterLocalNotificationsPlugin.show(
          0,
          notification.title,
          notification.body,
          const NotificationDetails(
            android: AndroidNotificationDetails(
              'high_importance_channel',
              'High Importance Notifications',
            ),
          ),
        );
      }
    });
  }
}

이것 외에 νŒŒμ΄μ–΄λ² μ΄μŠ€ μ½˜μ†”μ—μ„œ μ²˜λ¦¬ν•΄μ€˜μ•Όν•  λ‚΄μš©λ“€μ΄ μžˆλŠ”λ° κ·Έ 뢀뢄은 λ‹€λ₯Έ λΈ”λ‘œκ·Έμ—λ„ 많이 λ‚˜μ™€μžˆμ–΄μ„œ μƒλž΅ν•˜λ„λ‘ ν•œλ‹€.

μ‚¬μš©

environment.dart μ—μ„œ run() ν•  λ•Œ μ•„λž˜μ™€ 같이 ν˜ΈμΆœν•΄μ€˜μ•Όν•œλ‹€.

    await PushNotificationManager.initializeNotification();

Last updated