2024-04-04 23:18:24 +00:00
|
|
|
/*
|
2023-05-26 21:21:16 +00:00
|
|
|
* This file is part of Stack Wallet.
|
|
|
|
*
|
|
|
|
* Copyright (c) 2023 Cypher Stack
|
|
|
|
* All Rights Reserved.
|
|
|
|
* The code is distributed under GPLv3 license, see LICENSE file for details.
|
|
|
|
* Generated by Cypher Stack on 2023-05-26
|
|
|
|
*
|
|
|
|
*/
|
|
|
|
|
2022-08-26 08:11:35 +00:00
|
|
|
import 'package:flutter_local_notifications/flutter_local_notifications.dart';
|
2024-05-27 23:56:22 +00:00
|
|
|
|
2024-05-23 00:37:06 +00:00
|
|
|
import '../models/notification_model.dart';
|
|
|
|
import '../utilities/prefs.dart';
|
2024-05-27 23:56:22 +00:00
|
|
|
import 'notifications_service.dart';
|
2022-08-26 08:11:35 +00:00
|
|
|
|
|
|
|
class NotificationApi {
|
|
|
|
static final _notifications = FlutterLocalNotificationsPlugin();
|
2024-04-11 17:21:20 +00:00
|
|
|
// static final onNotifications = BehaviorSubject<String?>();
|
2022-08-26 08:11:35 +00:00
|
|
|
|
|
|
|
static Future<NotificationDetails> _notificationDetails() async {
|
|
|
|
return const NotificationDetails(
|
2024-05-27 23:56:22 +00:00
|
|
|
android: AndroidNotificationDetails(
|
|
|
|
'channel id', 'channel name',
|
|
|
|
channelDescription: 'channel description',
|
|
|
|
// importance: Importance.max,
|
|
|
|
priority: Priority.high,
|
|
|
|
ticker: 'ticker',
|
|
|
|
),
|
2024-04-04 23:18:24 +00:00
|
|
|
iOS: DarwinNotificationDetails(),
|
|
|
|
macOS: DarwinNotificationDetails(),
|
2022-08-26 08:11:35 +00:00
|
|
|
);
|
|
|
|
}
|
|
|
|
|
|
|
|
static Future<void> init({bool initScheduled = false}) async {
|
|
|
|
const android = AndroidInitializationSettings('app_icon_alpha');
|
2024-04-04 23:18:24 +00:00
|
|
|
const iOS = DarwinInitializationSettings();
|
2022-08-26 08:11:35 +00:00
|
|
|
const linux = LinuxInitializationSettings(
|
2024-05-27 23:56:22 +00:00
|
|
|
defaultActionName: "temporary_stack_wallet",
|
|
|
|
);
|
2024-04-04 23:18:24 +00:00
|
|
|
const macOS = DarwinInitializationSettings();
|
2023-07-17 16:36:45 +00:00
|
|
|
const settings = InitializationSettings(
|
|
|
|
android: android,
|
|
|
|
iOS: iOS,
|
|
|
|
linux: linux,
|
|
|
|
macOS: macOS,
|
|
|
|
);
|
2022-08-26 08:11:35 +00:00
|
|
|
await _notifications.initialize(
|
|
|
|
settings,
|
2024-04-11 17:21:20 +00:00
|
|
|
// onDidReceiveNotificationResponse: (payload) async {
|
|
|
|
// onNotifications.add(payload.payload);
|
|
|
|
// },
|
|
|
|
// onDidReceiveBackgroundNotificationResponse: (payload) async {
|
|
|
|
// onNotifications.add(payload.payload);
|
|
|
|
// },
|
2022-08-26 08:11:35 +00:00
|
|
|
);
|
|
|
|
}
|
|
|
|
|
|
|
|
static Future<void> clearNotifications() async => _notifications.cancelAll();
|
|
|
|
|
|
|
|
static Future<void> clearNotification(int id) async =>
|
|
|
|
_notifications.cancel(id);
|
|
|
|
|
|
|
|
//===================================
|
|
|
|
static late Prefs prefs;
|
|
|
|
static late NotificationsService notificationsService;
|
|
|
|
|
|
|
|
static Future<void> showNotification({
|
|
|
|
required String title,
|
|
|
|
required String body,
|
|
|
|
required String walletId,
|
|
|
|
required String iconAssetName,
|
|
|
|
required DateTime date,
|
|
|
|
required bool shouldWatchForUpdates,
|
|
|
|
required String coinName,
|
|
|
|
String? txid,
|
|
|
|
int? confirmations,
|
|
|
|
int? requiredConfirmations,
|
|
|
|
String? changeNowId,
|
|
|
|
String? payload,
|
|
|
|
}) async {
|
|
|
|
await prefs.incrementCurrentNotificationIndex();
|
|
|
|
final id = prefs.currentNotificationId;
|
|
|
|
|
|
|
|
String confirms = "";
|
2023-07-25 19:55:19 +00:00
|
|
|
if (txid != null &&
|
|
|
|
confirmations != null &&
|
|
|
|
requiredConfirmations != null) {
|
|
|
|
confirms = " ($confirmations/$requiredConfirmations)";
|
2022-08-26 08:11:35 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
final NotificationModel model = NotificationModel(
|
|
|
|
id: id,
|
|
|
|
title: title + confirms,
|
|
|
|
description: body,
|
|
|
|
iconAssetName: iconAssetName,
|
|
|
|
date: date,
|
|
|
|
walletId: walletId,
|
|
|
|
read: false,
|
|
|
|
shouldWatchForUpdates: shouldWatchForUpdates,
|
|
|
|
coinName: coinName,
|
|
|
|
txid: txid,
|
|
|
|
changeNowId: changeNowId,
|
|
|
|
);
|
|
|
|
|
|
|
|
await Future.wait([
|
|
|
|
_notifications.show(
|
|
|
|
id,
|
|
|
|
title,
|
|
|
|
body,
|
|
|
|
await _notificationDetails(),
|
|
|
|
payload: payload,
|
|
|
|
),
|
|
|
|
notificationsService.add(model, true),
|
|
|
|
]);
|
|
|
|
}
|
|
|
|
}
|