showLocalNotification method

  1. @override
Future<void> showLocalNotification(
  1. Map<String, dynamic> params
)
override

Implementation

@override
Future<void> showLocalNotification(Map<String, dynamic> params) async {
  try {
    // 在 iOS 上显示通知前先请求权限
    if (io.Platform.isIOS) {
      await permissionRequest('notifications');
    }

    final id = params['id'] as String? ?? '0';
    final title = params['title'] as String? ?? '';
    final body = params['body'] as String? ?? '';
    final subtitle = params['subtitle'] as String?;
    final payload = params['payload'] as Map<String, dynamic>?;
    final at = params['at'] as int?;
    final sound = params['sound'] as bool?;
    final badge = params['badge'] as int?;
    final channelId = params['channelId'] as String?;

    final AndroidNotificationDetails androidPlatformChannelSpecifics =
        AndroidNotificationDetails(
      channelId ?? 'appbridge_channel',
      'AppBridge Notifications',
      channelDescription: 'Notifications from AppBridge H5 SDK',
      importance: Importance.max,
      priority: Priority.high,
      showWhen: false,
      sound: sound == true
          ? const RawResourceAndroidNotificationSound('notification')
          : null,
    );
    final DarwinNotificationDetails iOSPlatformChannelSpecifics =
        DarwinNotificationDetails(
      subtitle: subtitle,
      badgeNumber: badge,
      presentSound: sound,
    );
    final NotificationDetails platformChannelSpecifics = NotificationDetails(
      android: androidPlatformChannelSpecifics,
      iOS: iOSPlatformChannelSpecifics,
    );

    if (at != null) {
      await _flutterLocalNotificationsPlugin.zonedSchedule(
        int.tryParse(id) ?? 0,
        title,
        body,
        tz.TZDateTime.fromMillisecondsSinceEpoch(tz.local, at),
        platformChannelSpecifics,
        payload: payload != null ? jsonEncode(payload) : null,
        androidScheduleMode: AndroidScheduleMode.exactAllowWhileIdle,
        uiLocalNotificationDateInterpretation:
            UILocalNotificationDateInterpretation.absoluteTime,
      );
    } else {
      await _flutterLocalNotificationsPlugin.show(
        int.tryParse(id) ?? 0,
        title,
        body,
        platformChannelSpecifics,
        payload: payload != null ? jsonEncode(payload) : null,
      );
    }
  } catch (e) {
    debugPrint('Error showing local notification: $e');
  }
}