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