This repository has been archived on 2024-10-18. You can view files and clone it, but cannot push or open issues or pull requests.
ftc_patient_app/lib/utilities/notification_util.dart

184 lines
6.1 KiB
Dart

import 'dart:convert';
import 'dart:io';
import 'package:flutter/foundation.dart';
import 'package:flutter_local_notifications/flutter_local_notifications.dart';
import 'fcm_notifications.dart';
import 'extensions/custom_extensions.dart';
const String packageName = "com.inajam.app";
const notificationChannelId = packageName;
const notificationChannelName = "inajam";
const notificationChannelDescription = "In A Jam";
final FlutterLocalNotificationsPlugin flutterLocalNotificationsPlugin =
FlutterLocalNotificationsPlugin();
//for SDK version 33
Future<void> requestNotificationPermissions() async {
if (Platform.isIOS || Platform.isMacOS) {
await flutterLocalNotificationsPlugin
.resolvePlatformSpecificImplementation<
IOSFlutterLocalNotificationsPlugin>()
?.requestPermissions(
alert: true,
badge: true,
sound: true,
critical: true,
);
} else if (Platform.isAndroid) {
final AndroidFlutterLocalNotificationsPlugin? androidImplementation =
flutterLocalNotificationsPlugin.resolvePlatformSpecificImplementation<
AndroidFlutterLocalNotificationsPlugin>();
await androidImplementation?.requestNotificationsPermission();
}
}
const AndroidInitializationSettings initializationSettingsAndroid =
AndroidInitializationSettings('@drawable/notification_icon');
const DarwinInitializationSettings initializationSettingsDarwin =
DarwinInitializationSettings(
// requestAlertPermission: true,
// requestBadgePermission: true,
// requestSoundPermission: true,
// onDidReceiveLocalNotification:
// (int id, String? title, String? body, String? payload) async {
// didReceiveLocalNotificationStream.add(
// ReceivedNotification(
// id: id,
// title: title,
// body: body,
// payload: payload,
// ),
// );
// },
// notificationCategories: darwinNotificationCategories,
);
const InitializationSettings initializationSettings = InitializationSettings(
android: initializationSettingsAndroid,
iOS: initializationSettingsDarwin,
macOS: initializationSettingsDarwin,
);
void selectNotification(String? payload) async {
debugPrint('selected notification payload: $payload');
debugPrint(
'is selected notification payload null or empty: ${payload.isNotNullOrEmpty()}');
if (payload.isNotNullOrEmpty()) {
var messageData = jsonDecode(payload!);
handleAppNotification(messageData);
}
}
//-----------------------
//-----------------------
//-----------------------
class NotificationUtils {
NotificationUtils._();
static NotificationDetails? platformChannelSpecifics;
static AndroidNotificationChannel? channel;
static AndroidNotificationDetails? androidPlatformChannelSpecifics;
static Future init() async {
try {
await flutterLocalNotificationsPlugin.initialize(initializationSettings,
onDidReceiveNotificationResponse: (response) =>
selectNotification(response.payload)
// onDidReceiveNotificationResponse:
// (NotificationResponse notificationResponse) {
// switch (notificationResponse.notificationResponseType) {
// case NotificationResponseType.selectedNotification:
// selectNotificationStream.add(notificationResponse.payload);
// break;
// case NotificationResponseType.selectedNotificationAction:
// if (notificationResponse.actionId == navigationActionId) {
// selectNotificationStream.add(notificationResponse.payload);
// }
// break;
// }
// },
// onDidReceiveBackgroundNotificationResponse: notificationTapBackground,
);
platformChannelSpecifics = await getChannelSpecifics();
} catch (e) {
debugPrint("NotificationUtils: ${e.toString()}");
}
}
static Future<NotificationDetails> getChannelSpecifics(
[int? badgeNumber]) async {
if (Platform.isAndroid) {
channel = await _getAndroidDefaultChannel(notificationChannelId,
notificationChannelName, notificationChannelDescription);
}
androidPlatformChannelSpecifics ??= AndroidNotificationDetails(
notificationChannelId, notificationChannelName,
channelDescription: notificationChannelDescription,
importance: channel?.importance ?? Importance.max,
priority: Priority.high,
enableVibration: true,
playSound: false);
int badgeNum = 0;
//Todo
// try {
// badgeNum =
// badgeNumber ?? AppProviderController.instance.notificationCount();
// print("badgeNum: $badgeNum");
// } catch (e) {}
var iOSPlatformChannelSpecifics = DarwinNotificationDetails(
presentSound: true,
presentBadge: true,
presentAlert: true,
badgeNumber: badgeNum,
);
return NotificationDetails(
android: androidPlatformChannelSpecifics,
iOS: iOSPlatformChannelSpecifics,
);
}
static Future<void> showDefaultNotification(
{required String title,
required String body,
Map<String, dynamic>? payload,
int? badgeNumber}) async {
platformChannelSpecifics = await getChannelSpecifics(badgeNumber);
await flutterLocalNotificationsPlugin.show(
10,
title,
body,
platformChannelSpecifics,
payload: (payload.isNullOrEmpty()) ? null : jsonEncode(payload!),
);
}
static Future<AndroidNotificationChannel> _getAndroidDefaultChannel(
String channelId,
String channelName,
String? channelDescription,
) async {
AndroidNotificationChannel channel = AndroidNotificationChannel(
channelId,
channelName,
description: channelDescription,
importance: Importance.max,
enableVibration: true,
playSound: false,
);
await flutterLocalNotificationsPlugin
.resolvePlatformSpecificImplementation<
AndroidFlutterLocalNotificationsPlugin>()
?.createNotificationChannel(channel);
return channel;
}
}