push_notification_manager
μ©λ
ꡬν
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',
),
),
);
}
});
}
}
μ¬μ©
Last updated