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