Untitled

mail@pastecode.io avatar
unknown
plain_text
2 years ago
1.7 kB
1
Indexable
Never

import 'dart:convert';

import 'package:epl/ui/game_notification/main.dart';
import 'package:firebase_messaging/firebase_messaging.dart';
import 'package:flutter/material.dart';
import 'package:flutter_local_notifications/flutter_local_notifications.dart';

class LocalNotificationService {
  static final FlutterLocalNotificationsPlugin _flutterLocalNotificationsPlugin = FlutterLocalNotificationsPlugin();

  static void initialize(BuildContext context) {
    const  InitializationSettings initializationSettings =  InitializationSettings(android: AndroidInitializationSettings("@mipmap/ic_launcher"));
    _flutterLocalNotificationsPlugin.initialize(initializationSettings,onSelectNotification: (String? payload) async {
        if(payload != null){
            final Map<String, dynamic> notificationData = json.decode(payload);
            

            // TODO
            print(notificationData);


        }
    });
  }

  static void displayLocalNotification(RemoteMessage message) async {

    try {
      final id = DateTime.now().millisecondsSinceEpoch ~/1000;

      const  NotificationDetails notificationDetails = NotificationDetails(
        android: AndroidNotificationDetails(
          "high_importance_channel",
          "high importance channel",
          importance: Importance.max,
          priority: Priority.high,
        )
      );


      await _flutterLocalNotificationsPlugin.show(
        id,
        message.notification!.title,
        message.notification!.body,
        notificationDetails,
        payload: json.encode(message.data),
      );
    } on Exception catch (e) {
      debugPrint('Exception error: $e');

    }
  }
}