cake_wallet/lib/entities/push_notifications_service.dart

49 lines
1.6 KiB
Dart
Raw Normal View History

2021-06-04 15:25:17 +00:00
import 'dart:io';
import 'package:cake_wallet/main.dart';
import 'package:cake_wallet/utils/show_bar.dart';
import 'package:firebase_messaging/firebase_messaging.dart';
class PushNotificationsService {
PushNotificationsService._();
factory PushNotificationsService() => _instance;
static final PushNotificationsService _instance = PushNotificationsService._();
static Future<dynamic> _onBackgroundMessage(Map<String, dynamic> message) async {}
static Future<void> _showNotification(Map<String, dynamic> message) async {
Map<dynamic, dynamic> alert = <dynamic, dynamic>{};
String msg = '';
String title = '';
if (Platform.isIOS) {
alert = message['aps']['alert'] as Map<dynamic, dynamic> ?? <dynamic, dynamic>{};
msg = alert['body'] as String ?? '';
title = alert['title'] as String ?? '';
}
if (Platform.isAndroid) {
msg = message['notification']['body'] as String ?? '';
title = message['notification']['title'] as String ?? '';
}
await showBar<void>(navigatorKey.currentContext, msg, titleText: title, duration: null);
}
2021-06-04 15:25:17 +00:00
final _firebaseMessaging = FirebaseMessaging();
bool _initialized = false;
Future<void> init() async {
if (_initialized) {
return;
}
_firebaseMessaging.requestNotificationPermissions();
_firebaseMessaging.configure(
onMessage: (message) async => _showNotification(message),
onLaunch: (message) async => _showNotification(message),
onResume: (message) async => _showNotification(message),
2021-06-04 15:25:17 +00:00
onBackgroundMessage: _onBackgroundMessage);
_initialized = true;
}
}