didvan-app/lib/services/notification/awsome/awsome_notification_handler...

150 lines
5.4 KiB
Dart

import 'package:awesome_notifications/awesome_notifications.dart';
import 'package:firebase_messaging/firebase_messaging.dart';
import 'package:flutter/material.dart';
import '../../../models/notification_message.dart';
import 'awsome_notification_controller.dart';
class AwsomeNotificationHandler {
main() async {
late ReceivedAction? initialAction;
AwesomeNotifications().initialize(
// set the icon to null if you want to use the default app icon
null,
[
NotificationChannel(
channelKey: 'alerts',
channelName: 'Alerts',
channelDescription: 'Notification tests as alerts',
playSound: true,
onlyAlertOnce: true,
groupAlertBehavior: GroupAlertBehavior.Children,
importance: NotificationImportance.High,
defaultPrivacy: NotificationPrivacy.Public,
defaultColor: const Color(0xFF007EA7),
criticalAlerts: true,
ledColor: Colors.white)
],
// Channel groups are only visual and are not required
// channelGroups: [
// NotificationChannelGroup(
// channelGroupKey: 'basic_channel_group',
// channelGroupName: 'Basic group')
// ],
debug: true);
initialAction = await AwesomeNotifications()
.getInitialNotificationAction(removeFromActionEvents: false);
AwesomeNotifications().setListeners(
onActionReceivedMethod: NotificationController.onActionReceivedMethod,
onNotificationCreatedMethod:
NotificationController.onNotificationCreatedMethod,
onNotificationDisplayedMethod:
NotificationController.onNotificationDisplayedMethod,
onDismissActionReceivedMethod:
NotificationController.onDismissActionReceivedMethod);
AwesomeNotifications().isNotificationAllowed().then((isAllowed) {
//It would be more appropriate if you can show your own dialog
//to the user before requesting the notifications permissons.
if (!isAllowed) {
AwesomeNotifications().requestPermissionToSendNotifications(
permissions: [
NotificationPermission.Alert,
NotificationPermission.Sound,
NotificationPermission.Badge,
NotificationPermission.Vibration,
NotificationPermission.Light,
NotificationPermission.FullScreenIntent,
],
);
}
});
}
show(RemoteMessage message) async {
NotificationMessage notificationMessage =
NotificationMessage.fromJson(message.data);
switch (notificationMessage.type) {
case "1":
await showNotificationTypeNews(notificationMessage);
break;
case "2":
await showNotificationTypeMessage(notificationMessage);
break;
case "3":
await showNotificationTypeEmoji(notificationMessage);
break;
}
}
showNotificationTypeNews(NotificationMessage message) async {
AwesomeNotifications().createNotification(
content: NotificationContent(
id: DateTime.now().millisecondsSinceEpoch ~/ 1000,
channelKey: 'alerts',
actionType: ActionType.Default,
title: "\u200f ${message.title} \u200f",
body: message.body.toString(),
notificationLayout: NotificationLayout.BigText,
largeIcon: message.image.toString(),
color: const Color(0xFF007EA7)),
);
}
showNotificationTypeMessage(NotificationMessage message) async {
AwesomeNotifications().createNotification(
content: NotificationContent(
id: DateTime.now().millisecondsSinceEpoch ~/ 1000,
channelKey: 'alerts',
actionType: ActionType.Default,
title: message.title.toString(),
body: message.body.toString(),
largeIcon: message.image.toString(),
roundedLargeIcon: true,
notificationLayout: NotificationLayout.Inbox,
color: const Color(0xFF007EA7)),
actionButtons: [
NotificationActionButton(
key: 'REPLY',
label: 'جواب دادن',
requireInputText: true,
actionType: ActionType.SilentAction),
NotificationActionButton(
key: 'DISMISS',
label: 'خوانده شده',
actionType: ActionType.DismissAction,
isDangerousOption: true)
]);
}
showNotificationTypeEmoji(NotificationMessage message) async {
AwesomeNotifications().createNotification(
content: NotificationContent(
id: DateTime.now().millisecondsSinceEpoch ~/ 1000,
channelKey: 'alerts',
title: 'Emojis are awes'
'ome too! ${Emojis.animals_lady_beetle}${Emojis.activites_balloon}${Emojis.emotion_red_heart}',
body:
'Simple body with a bunch of Emojis! ${Emojis.transport_police_car} ${Emojis.animals_dog} ${Emojis.flag_UnitedStates} ${Emojis.person_baby}',
largeIcon:
'https://cdn.britannica.com/72/232772-050-4E3D86CC/mind-blown-emoji-head-exploding-emoticon.jpg',
notificationLayout: NotificationLayout.BigPicture,
payload: {
'title': 'Notification Title',
'body': 'Notification Body',
'image': 'path/to/smallImage.png', // Path to small image
'largeImage': 'path/to/largeImage.png', // Path to large image
}));
}
}