showPushNotification static method
Future<void>
showPushNotification(
- Map notification
)
Implementation
static Future<void> showPushNotification(Map notification) async {
if (!isInitializedPlugin) {
await initializePlugin();
}
DarwinNotificationDetails iosDetails = const DarwinNotificationDetails();
AndroidNotificationDetails androidDetails =
const AndroidNotificationDetails('default', 'channelName');
String title = 'Backendless Title';
String message = 'Backendless Message';
int badge = 0;
String? threadId;
String? subtitle;
int id = Random().nextInt(2147483646);
String? templateName = notification['template_name'];
if (notification.containsKey('data')) {
templateName ??= notification['data']['template_name'];
}
if (templateName == null) {
if (Platform.isAndroid) {
title = notification['android-content-title'];
}
if (Platform.isIOS) {
title = notification['ios-alert-title'];
}
message = notification['message'];
} else {
Map? templateFromStorage;
if (await TemplateStorage.containsTemplate(templateName)) {
String templateAsString =
await TemplateStorage.getTemplate(templateName) as String;
templateFromStorage = jsonDecode(templateAsString);
}
if (Platform.isIOS) {
badge = notification['aps']['badge'] ?? 0;
threadId = notification['thread-id'];
// String? summaryFormat = notification['summary-format'];
message = notification['message'];
title = notification['ios-alert-title'];
subtitle = notification['aps']['alert']['subtitle'];
iosDetails = DarwinNotificationDetails(
badgeNumber: badge, subtitle: subtitle, threadIdentifier: threadId);
} else if (Platform.isAndroid) {
if (templateFromStorage != null) {
badge = templateFromStorage['badge'] ?? 0;
Color? color = Color(templateFromStorage['colorCode']);
if (notification.containsKey('data')) {
message = notification['data']['message'];
title = notification['data']['android-content-title'];
} else {
message = notification['message'];
title = notification['android-content-title'];
subtitle = notification['android-summary-subtext'];
}
androidDetails = AndroidNotificationDetails(
templateName,
templateName,
priority: Priority.max,
importance: Importance.max,
subText: subtitle,
number: badge,
icon: templateFromStorage['icon'],
color: color,
);
}
}
}
notification['flutter_notification_identifier'] = 'identity';
String payload = jsonEncode(notification);
var notificationDetails =
NotificationDetails(iOS: iosDetails, android: androidDetails);
await flutterLocalNotificationsPlugin
.show(id, title, message, notificationDetails, payload: payload);
}