λ°±κ·ΈλΌμ΄λμμ νΈμ νλ κ²μ μ¬μ΄λ° ν¬κ·ΈλΌμ΄λμμ νΈμλ₯Ό μ²λ¦¬ν λ μλλ‘μ΄λμμ μΆκ°μ μΌλ‘ ν΄μ€μΌ ν μ¬νμ΄ μλ€. μ΄ κ²½μ°μ νΈμ μ²λ¦¬λ₯Ό μν΄μ μΆκ°μ μΈ μ½λ ꡬνμ΄ νμνκ³ 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();